最近工作需要,把用户检测的一些数据存到本地,因为检测完用户可能不想上传,或者上传失败,网络断了。这时候我们可以提供一个历史数据上传的功能,方便用户选择合适的数据上传,避免浪费开销,话不多说直接贴代码。
package cp.sample.com.fetalheartmonitor_demo.db; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.sqliteDatabase; import android.database.sqlite.sqliteOpenHelper; import cp.sample.com.fetalheartmonitor_demo.AppContext; public class DBServices extends sqliteOpenHelper{ public final static int version = 1; public final static String dbName = "MonitorData"+ AppContext.getInstance().getProperty("UsrName");//不同数据库名,登陆后显示不同列表 public DBServices(Context context){ super(context,dbName,null,version); } @Override public void onCreate(sqliteDatabase db) { // TODO Auto-generated method stub db.beginTransaction(); //创建邮件表 String create_mail_sql = "CREATE TABLE if not exists [MonitorData]"+ "(_id integer primary key autoincrement,object object)"; db.execsql(create_mail_sql); db.setTransactionSuccessful(); db.endTransaction(); } @Override public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { // TODO Auto-generated method stub } //增 public void insert(String table,String nullColumnHack,ContentValues values){ sqliteDatabase db = this.getWritableDatabase(); db.beginTransaction(); db.insert(table,nullColumnHack,values); db.setTransactionSuccessful(); db.endTransaction(); } //删 public void delete(String table,String whereClause,String[] whereArgs){ sqliteDatabase db = this.getWritableDatabase(); db.beginTransaction(); db.delete(table,whereClause,whereArgs); db.setTransactionSuccessful(); db.endTransaction(); } //改 public void update(String table,ContentValues values,String[] whereArgs){ sqliteDatabase db = this.getWritableDatabase(); db.beginTransaction(); db.update(table,values,whereArgs); db.setTransactionSuccessful(); db.endTransaction(); } //查 public Cursor read(String sql,String[] args){ sqliteDatabase db = this.getReadableDatabase(); db.beginTransaction(); Cursor cursor = db.rawQuery(sql,args); db.setTransactionSuccessful(); db.endTransaction(); return cursor; } }
然后在需要使用的activity或者fragment里:
DBServices db = new DBServices(this.getContext());
db = new DBServices(this.getContext());
/** * 保存数据 */ public void saveData(MonitorDataObject object) { ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream(); try { ObjectOutputStream objectOutputStream = new ObjectOutputStream(arrayOutputStream); objectOutputStream.writeObject(object); objectOutputStream.flush(); byte data[] = arrayOutputStream.toByteArray(); objectOutputStream.close(); arrayOutputStream.close(); sqliteDatabase database = db.getWritableDatabase(); database.execsql("insert into MonitorData (object) values(?)",new Object[] { data }); database.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }这就直接存了类到数据表了。
怎么拿去类呢,下面是拿去这个类的相关代码:
DBServices db = new DBServices(this);//定义变量 db = new DBServices(this);//初始化 //拿到主键_id的列表方便查询,通过object是查不到的 public ArrayList<Integer> getAllInt() { ArrayList<Integer> persons = new ArrayList<Integer>(); sqliteDatabase database = db.getReadableDatabase(); Cursor cursor = database.rawQuery("select * from MonitorData",null); if (cursor != null) { while (cursor.moveToNext()) { //如果你代码中有用它从数据库中取一个整型值,需要注意:如果数据库中的这一个数据恰好是null,而null又不是int,此时并不会抛出NumberFormatException 异常,而是直接返回0! int person =cursor.getInt(0); persons.add(person); } } Log.d("Persons-Count",Integer.toString(persons.size())); return persons; } //拿去数据 public ArrayList<MonitorDataObject> getAllObject() { ArrayList<MonitorDataObject> persons = new ArrayList<MonitorDataObject> (); sqliteDatabase database = db.getReadableDatabase(); Cursor cursor = database.rawQuery("select * from MonitorData",null); if (cursor != null) { while (cursor.moveToNext()) { Log.d("data-id",cursor.getString(0)); byte data[] = cursor.getBlob(cursor.getColumnIndex("object")); ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(data); try { ObjectInputStream inputStream = new ObjectInputStream (arrayInputStream); MonitorDataObject person = (MonitorDataObject) inputStream.readObject(); persons.add(person); inputStream.close(); arrayInputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } Log.d("Persons-Count",Integer.toString(persons.size())); return persons; }下面举一个删除的例子:
DBServices db = new DBServices(this); db = new DBServices(this); //index需要删除的_id,上面代码有拿到id的列表 String[] args = {String.valueOf(index)}; db.delete("MonitorData","_id=?",args);记录一下,方便以后查询。 原文链接:https://www.f2er.com/sqlite/198101.html