|
主要思路:
1.DatabaseHelper 继承sqliteOpenHelper
2. helper = new DatabaseHelper(); 数据库助手的实例
3. 如果改数据库,先通过数据库助手得到数据库(有可修改权限的) SQLiteDatabase db = mOpenHelper.getWritableDatabase();
3.1如果查询,SQLiteDatabase db = mOpenHelper.getReadableDatabase();
4.使用数据库对象操作数据库:
db.execSQL(sql); //这个似乎万能,推荐只使用这个 ,下面的是对sql的封装
db.delete(TABLE_NAME, " title = 'haiyang'", null);
Cursor cur = db.query(TABLE_NAME, col, null, null, null, null, null);
注意:SimpleCursorAdapter是和数据库有关的东西,而sqlite数据库所要求的 自动增长的 id 必须是 _id这中形式
//===================常用sql 备忘====================================
String sql = "CREATE TABLE " + TABLE_NAME + " (" + TITLE
+ " text not null, " + BODY + " text not null " + ");";
String sql1 = "insert into " + TABLE_NAME + " (" + TITLE + ", " + BODY
+ ") values('haiyang', 'android的发展真是迅速啊');";
String tracks_sql = "CREATE TABLE " + TrackDbAdapter.TABLE_NAME + " ("
+ TrackDbAdapter.ID+ " INTEGER primary key autoincrement, "
+ TrackDbAdapter.NAME+ " text not null, "
+ TrackDbAdapter.DESC + " text ,"
+ TrackDbAdapter.DIST + " LONG ,"
+ TrackDbAdapter.TRACKEDTIME + " LONG ,"
+ TrackDbAdapter.LOCATE_COUNT + " INTEGER, "
+ TrackDbAdapter.CREATED + " text, "
+ TrackDbAdapter.AVGSPEED + " LONG, "
+ TrackDbAdapter.MAXSPEED + " LONG ,"
+ TrackDbAdapter.UPDATED + " text "
+ ");";
Log.i(TAG, tracks_sql);
db.execSQL(tracks_sql);
String locats_sql = "CREATE TABLE " + LocateDbAdapter.TABLE_NAME + " ("
+ LocateDbAdapter.ID+ " INTEGER primary key autoincrement, "
+ LocateDbAdapter.TRACKID+ " INTEGER not null, "
+ LocateDbAdapter.LON + " DOUBLE ,"
+ LocateDbAdapter.LAT + " DOUBLE ,"
+ LocateDbAdapter.ALT + " DOUBLE ,"
+ LocateDbAdapter.CREATED + " text "
+ ");";
Log.i(TAG, locats_sql);
db.execSQL(locats_sql);
Cursor c = managedQuery(Dairy.DairyColumns.DAIRY_URI,
PROJECTION, null, null, Dairy.DairyColumns.ORDER);
等效于:
mDiaryCursor = db.query(.....);//查询
startManagingCursor(mDiaryCursor); //绑定activity和cursor的生命周期
//==================================================================
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_NAME + " (" + TITLE
+ " text not null, " + BODY + " text not null " + ");";
Log.i("haiyang:createDB=", sql);
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
} |
|
|