标签:oncreate mysql override 条件 family user database 默认值 ali
1、SqliteOpenHelper为数据库抽象类,需创建一个类继承他,其中有onCreate创建数据库的抽象方法和onUpgrade升级数据库方法(一般用不上)。
1 //创建数据库的类 2 //SQLiteOpenHelper抽象类 onCreate和onUpgrade抽象方法 3 public class MySqliteHelper extends SQLiteOpenHelper { 4 5 //新建数据库以及表 6 private String SqlCpData="create table TableCpData(id integer primary key autoincrement," 7 + "cp_num varchar(20),time varchar(20));"; 8 9 10 public MySqliteHelper (Context context) { 11 this(context, "tableCpData.db", null, 1); 12 } 13 14 public MySqliteHelper(Context context, String name, CursorFactory factory, int version) { 15 super(context, name, factory, version); 16 // TODO Auto-generated constructor stub 17 } 18 19 //创建数据库抽象方法 20 @Override 21 public void onCreate(SQLiteDatabase db) { 22 // TODO Auto-generated method stub 23 db.execSQL(SqlCpData); //执行新建数据库操作 24 Log.i(SqlCpData, "数据库创建成功"); 25 } 26 27 //升级数据库 28 @Override 29 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 30 // TODO Auto-generated method stub 31 32 } 33 34 35 }
2、然后创建一个方法类,用来定义获取操作以及时间的方法
//定义获取操作以及时间的方法 public class Rule { public int id; public String cp_num; public String time; //获取ID public int getid() { return id; } //设置ID public void setId(int id) { this.id=id; } public String getCpNum() { return cp_num; } public void setCpNum(String cp_num){ this.cp_num=cp_num; } public String getTime() { return time; } public void setTime(String time) { this.time=time; } //该类的方法 public Rule(int id, String cp_num, String time) { super(); this.id = id; this.cp_num = cp_num; this.time = time; } public Rule() { super(); } }
3、然后创建数据库的一个增删查改类
//数据库的增删查改操作 public class CpDataDao { private MySqliteHelper mySqliteHelper; //定义数据库操作函数 public CpDataDao(Context context) { mySqliteHelper=new MySqliteHelper(context); //实例化数据库操作函数 } //插入数据库 public long insert(String cp_num,String time) { //实例化SQLiteDatabase对象 SQLiteDatabase database=mySqliteHelper.getWritableDatabase(); ContentValues contentValues=new ContentValues(); contentValues.put("cp_num", cp_num); contentValues.put("time", time); return database.insert("TableCpData", null, contentValues); //表明 、空列的默认值、插入封装的列名称和列值 } private Rule rule=null; //存储数据的集合 private List<Rule> dataList = null; /** * 查询所有数据 * * @return 数据集合 */ public List<Rule> queryAll() { SQLiteDatabase db = mySqliteHelper.getReadableDatabase(); Cursor cursor = db.query("TableCpData", new String[] { "id", "cp_num", "time" }, null, null, null, null, null); if (cursor != null && cursor.getCount() > 0) { dataList = new ArrayList<Rule>(); while (cursor.moveToNext()) { rule = new Rule(); rule.setId(cursor.getInt(0)); rule.setCpNum(cursor.getString(1)); rule.setTime(cursor.getString(2)); dataList.add(rule); } } return dataList; } /** * 删除所有数据 * @return 0 删除成功,1删除失败 */ public int deleteAll() { try { SQLiteDatabase db = mySqliteHelper.getWritableDatabase(); // return db.delete("student", null, null); String sql = "delete from TableCpData"; db.execSQL(sql); return 0; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return 1; } }
还有一些按条件进行查询的操作,注意修改数据库名和表明要和上面的一致
/** * 查询时间 * * @return 数据集合 */ //查询数据的集合 // List<Rule> timeList=null; public List<Rule> queryTimerecord(final String time) { SQLiteDatabase db = dsOpenHelper.getReadableDatabase(); String sqluser = "select * from ruledata where time like ? "; String [] Sqlselect = new String[] { "%" + time + "%", }; Cursor cursor =db.rawQuery(sqluser, Sqlselect); if (cursor != null && cursor.getCount() > 0) { // UserPassward = new ArrayList<User>(); while (cursor.moveToNext()) { Rule rule = new Rule(); rule.setId(cursor.getInt(0)); rule.setOperation(cursor.getString(1)); rule.setTime(cursor.getString(2)); //UserPassward.add(user); dataList.add(rule); } } return dataList; }
4、最后在自己的类中对这些方法进行调用操作,就可以实现Sqlite的增删查改操作了
比方说对下位机收到的数据进行插入数据
(1)先在类全局变量中声明类
private CpDataDao cpDataDao=new CpDataDao(this); //数据库增删查改类
(2)然后调用该类的方法进行插入数据
cpDataDao.insert(device.getName(), time()); //插入一个设备的名称和当前系统时间
(3) (2)中time获取当前系统时间方法
//获取系统时间函数 private String time() { SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); Date curDate = new Date(System.currentTimeMillis());//获取当前时间 String str_time = formatter.format(curDate); return str_time; }
5、查询数据的操作也是类似,通过类来调用方法,这里是将查询到的数据显示在一个textView界面中
(1) 声明
private CpDataDao cpDataDao=new CpDataDao(this);
private CpDataDao deleDataDao=new CpDataDao(this);
private LinearLayout llcontent;
private TextView tv;
(2)查询所有的方法
//查询所有充电记录 private void queryAll() { //mListView.removeAllViews(); llcontent.removeAllViews(); List<Rule> dataList=cpDataDao.queryAll(); if (dataList!=null) { for(Rule rule:dataList) { tv=new TextView(this); //mListView=new ListView(mContext); // list.add(new deviceListItem("充电桩:"+rule.getCpNum()+"时间:"+rule.getTime(), true)); tv.setText("\t"+rule.getid()+"."+"充电桩:"+rule.getCpNum()+" "+"Time:"+rule.getTime()+"\n"); tv.setTextColor(Color.BLUE); tv.setTextSize(16); llcontent.addView(tv); Log.i("数据库数据",""+rule.getCpNum()+rule.getTime()); } Log.i("数据库查询","查询数据库成功"); } }
(3)删除所有
btnDelected.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub llcontent.removeAllViews(); //deleDataDao.deleteAll(); int line=deleDataDao.deleteAll(); if (line==0) { Log.i("所有数据删除成功", ""+line); Toast.makeText(mContext, "所有数据成功删除", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(mContext, "当前没有数据", Toast.LENGTH_SHORT).show(); } } });
标签:oncreate mysql override 条件 family user database 默认值 ali
原文地址:https://www.cnblogs.com/guanglili/p/9078665.html