数据存储之SQLite实例
程序员文章站
2022-12-02 21:44:03
1.创建SQLiteDatabase
@Override
protected void onCreate(Bundle savedInstanceState)...
1.创建SQLiteDatabase
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DemoSQlite demoSQlite=new DemoSQlite(this,"somedb",null,1); // demoSQlite.getReadableDatabase(); } public class DemoSQlite extends SQLiteOpenHelper { private static final String CREATE_DEMO="create table demo(" +"id integer primary key autoincrement,"+"date text," +"some real,"+"thing integer,"+"llis blob)"; public DemoSQlite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL(CREATE_DEMO); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { } }
2.insert
SQLiteDatabase database=demoSQlite.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("date","xx"); database.insert("demo",null,values); // db.execSQL("insert into Demo(xx,sss) values (?,?)",new String[]{"xx","ss"})
3.update
SQLiteDatabase database=demoSQlite.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("date","ss"); database.update("demo",values,"xxx=?",new String[]{"xx"}); // db.execSQL("update Demo set xx =? where ss
4.delete
SQLiteDatabase database=demoSQlite.getWritableDatabase(); database.delete("demo","xxx>?",new String[]{"xx"}); // db.execSQL("delete from Demo where xx>?",new String[]{"xx"});
5.query
SQLiteDatabase database=demoSQlite.getWritableDatabase(); database.query("demo",columns[],selection,values[],groupby,having ,orderby) // db.rawQuery("select * from Demo",null)