欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

数据存储之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)