Android 使用 SQLite数据库一般操作有:创建数据库、创建表、打开数据库、添加数据、删除数据、修改数据、查询数据、关闭数据库。
其中内嵌一个DatabaseHelper类继承SQLiteOpenHelper。SQLiteOpenHelper需要实现两个方法onCreate(SQLiteDatabase)用于创建数据库,onUpgrade(SQLiteDatabase,int,int)升级数据库。
SQLiteDatabase,用来管理数据库,它提供了insert、delete、query等方法。
private static final String CREATE_DATA_NAME = "create table LatLon (id long primary key, start_time timestamp not null, end_time timestamp not null, interval long not null, start_x integer, start_y integer, min_x integer, min_y integer, max_x integer, max_y integer,history varchar(100))"; /** * 创建更新数据库 */ private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } /** * 创建 */ @Override public void onCreate(SQLiteDatabase db) { Log.i(TAG, "Creating DataBase: " + CREATE_DATA_NAME); db.execSQL(CREATE_DATA_NAME); } /** * 数据库版本变化时调用 */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion); } }
这样就创建了一个数据库,表名为LatLon,字段有id , start_time , end_time , interval , start_x , start_y , min_x , min_y , max_x , max_y , history
public DatabaseUtil(Context ctx) { this.mCtx = ctx; } /** * 打开数据库 */ public DatabaseUtil open() throws SQLException { mDbHelper = new DatabaseHelper(mCtx); mDb = mDbHelper.getWritableDatabase(); return this; }
调用时:
DatabaseUtil dbUtil = new DatabaseUtil(getApplicationContext());
dbUtil.open();
DBUtil中insert方法:
/** * 添加数据 */ public long insert(long time, long start_time, long end_time, long interval, double start_x, double start_y, double min_x, double min_y, double max_x, double max_y, String history) { ContentValues initialValues = new ContentValues(); initialValues.put("id", time); initialValues.put("start_time", start_time); initialValues.put("end_time", end_time); initialValues.put("interval", interval); initialValues.put("start_x", start_x); initialValues.put("start_y", start_y); initialValues.put("min_x", min_x); initialValues.put("min_y", min_y); initialValues.put("max_x", max_x); initialValues.put("max_y", max_y); initialValues.put("history", history); return mDb.insert(DATABASE_TABLE, null, initialValues); }
调用方法时:
public void recordLocation(Location loc) { DatabaseUtil dbUtil = new DatabaseUtil(getApplicationContext()); dbUtil.open(); dbUtil.insert(System.currentTimeMillis(), System.currentTimeMillis(), System.currentTimeMillis(), 5, loc.getLongitude(), loc.getAltitude(), loc.getLongitude(), loc.getAltitude(), loc.getLongitude(), loc.getAltitude(), "[asdf{sdf:sdf,sdf}]"); dbUtil.close(); }
删除某一行的数据:
/** * 根据时间删除数据 * */ public boolean deleteBytime(long time) { return mDb.delete(DATABASE_TABLE, "id" + "=" + time, null) > 0; }
调用方法时:
DatabaseUtil dbUtil = new DatabaseUtil(getApplicationContext());
dbUtil.open();
dbUtil.deleteBytime(1359363721903L);
dbUtil.close();
暂无修改数据需求。
如有需求:
利用ContentValues 更新数据
mDb.update(表名,ContentValues,条件,null);
方法返回值为int类型。
查询出所有数据,返回的是一个Cursor:
/** * 查询所有数据 * * @return Cursor */ public Cursor queryAll() { return mDb.query(DATABASE_TABLE, new String[] { "id", "start_time", "end_time", "interval", "start_x", "start_y", "min_x", "min_y", "max_x", "max_y", "history" }, null, null, null, null, null); }
调用方法时:
DatabaseUtil dbUtil = new DatabaseUtil(getApplicationContext()); dbUtil.open(); Cursor cursor = dbUtil.queryAll(); if (cursor != null) { while (cursor.moveToNext()) { System.out.println(cursor.getInt(0) + " " + cursor.getDouble(2) + " " + cursor.getDouble(1)); } } dbUtil.close();
关闭数据库方法:
/** * 关闭数据库 */ public void close() { mDbHelper.close(); }
调用方法时:
dbUtil.close();