SQLite 2个主要的类 A)__SQLiteOpenHelper 这个类是一个抽象类 实现他的类必须包含一个构造方法: public **(Context context,表的名字,CursorFactory,版本){spuer(**,**,**)} 参数注意: version(版本):必须是大于0的整数 方法: getReadableDatabase() 返回:可读的SQLiteDatabase getWritableDatabase() 返回:可写的SQLiteDatabase onCreate(SQLiteDatabase db) 第一次调用这个类的时候会调用,是个回调函数 onUpgradel(SQLiteDatabase db,int oldVersion,int new Version) 升级数据库的时候调用,sans-serif; font-size:14px; line-height:25px">claose() 当关闭数据库时,sans-serif; font-size:14px; line-height:25px">B)__SQLiteDatabase SQLite操作的类 实例: //Databasehelper 是一个继承自SQLiteOpenHelper的类 Databasehelper dbHeper = new DatabaseHelper(上下文环境,"数据库名称",null,版本); SQLiteDatabase db = dbHeper.getReadableDatabase();//或 getWritableDatabase() //查询 返回 Cursor(一个游标类型,可以用 curosr.moveToNext()执行下一步操作) Curosr cu = db.query(table,columns,selection,selectionArgs,groupBy,having,orderBy); while(cu.moveToNext()){ String name =cu.getString(cu.getColumnINdex("列名")); } //增加 ContentValues cv = new ContentValues(); cv.put("列名","值"); long row = db.insert("表名",cv); //删除 String where ="ID = ?"; String [] whereValue = {Integer.toString(id)};//这里如果where有多少个? 就要有多少个值 db.delete("表名",where,whereValue); //修改 String where ="id =?"; String [] whereValue = {Integer.toString(id)};
db.update("表名",cv,whereValue);
- importandroid.content.ContentValues;
- importandroid.content.Context;
- importandroid.database.Cursor;
- importandroid.database.sqlite.SQLiteDatabase;
- importandroid.database.sqlite.SQLiteOpenHelper;
- importandroid.util.Log;
-
- publicclassDatabaseAdapter{
- privatestaticfinalStringDB_NAME="Test.db";
- finalintDB_VERSION=1;
- finalStringDB_TABLE="my_order";
- finalStringKEY_ID="_id";
- finalStringKEY_ORDER_ID="order_id";
- finalStringKEY_TYPE="_type";
- finalStringKEY_STATE="_state";
- privateContextcontext;
- privateDatabaseHelpermDatabaseHelper;
- privateSQLiteDatabasemSQLiteDatabase;
- classDatabaseHelperextendsSQLiteOpenHelper{
-
- finalStringDB_CREAT="CREATETABLE"
- +DB_TABLE
- +"("+KEY_ID+"INTEGERPRIMARYKEY,"
- +KEY_ORDER_ID+"TEXT,250); margin:0px 0px 0px 38px; padding-left:10px; padding-right:0px; font-size:1em; padding-top:0px"> +KEY_TYPE+"INTEGER,250); margin:0px 0px 0px 38px; padding-left:10px; padding-right:0px; font-size:1em; padding-top:0px"> +KEY_STATE+"INTEGER)";
- publicDatabaseHelper(Contextcontext){
- super(context,DB_NAME,null,DB_VERSION);
- }
- @Override
- voidonCreate(SQLiteDatabasedb){
- //TODOAuto-generatedmethodstub
- db.execSQL(DB_CREAT);
- voidonUpgrade(SQLiteDatabasedb,85); font-weight:bold">intoldVersion,85); font-weight:bold">intnewVersion){
- db.execSQL("DROPTABLEIFEXISTS"+DB_TABLE);
- onCreate(db);
- publicDatabaseAdapter(Contextcontext){
- this.context=context;
- //开启
- voidopen(){
- mDatabaseHelper=newDatabaseHelper(context);
- mSQLiteDatabase=mDatabaseHelper.getWritableDatabase();
- //关闭
- voidclose(){
- mSQLiteDatabase.close();
- mDatabaseHelper.close();
- //增
- longinsertData(StringorderId,85); font-weight:bold">inttype){
- ContentValuesvalues=newContentValues();
- values.put(KEY_ORDER_ID,orderId);
- values.put(KEY_TYPE,type);
- values.put(KEY_STATE,Config.STATE_APPLY);
- longid=mSQLiteDatabase.insert(DB_TABLE,KEY_ID,values);
- returnid;
- //删
- booleandeleteData(Contextcontext,85); font-weight:bold">longid){
- booleandelete=mSQLiteDatabase.delete(DB_TABLE,KEY_ID+"="+id,85); font-weight:bold">null)>0;
- returndelete;
- //改
- booleanupdateData(longid,85); font-weight:bold">intstate){
- ""+state);
- booleanupdate=mSQLiteDatabase.update(DB_TABLE,values,85); font-weight:bold">returnupdate;
- //查
- publicCursorfetchData(Stringselection){
- CursormCursor=mSQLiteDatabase.query(DB_TABLE,newString[]{KEY_ID,KEY_ORDER_ID,KEY_TYPE,KEY_STATE},85); font-weight:bold">null);
- if(mCursor!=null)
- mCursor.moveToFirst();
- returnmCursor;
- }
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|