技术栈

主页 > 数据库 >

数据存储之SQLite实例

技术栈 - 中国领先的IT技术门户

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)
责任编辑:admin  二维码分享:
本文标签: SQLiteDatabadatabasevaluesxxnewdemoSQlite