Android sqlite (1)
The Android operating system uses the SQLite database. There are two ways to obtain database objects:
1. Obtain an existing database
SQLiteDatabase dbbrndi=SQLiteDatabase.openDatabase("/sdcard/zhycheng.db3", null,SQLiteDatabase.OPEN_READONLY);
The first String parameter is the location of the database in the file system, the second parameter is generally null, and the third parameter controls the way the database is opened.
In this way, the database object is obtained.
2. Create a database by yourself
Create a new class, inherit from SQLiteOpenHelper, and add unimplemented Methods
The Code is as follows:
Package your. zhycheng; import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteDatabase. cursorFactory; import android. database. sqlite. SQLiteOpenHelper; public class MyHelper extends SQLiteOpenHelper {public MyHelper (Context context, String name) {this (context, name, 1);} public MyHelper (Context context, String name, int version) {this (context, name, null, version);} public MyHelper (Context context, String name, // database name CursorFactory factory, int version) {super (context, name, factory, version) ;}@ Overridepublic void onCreate (SQLiteDatabase db) mongodb.exe cSQL ("create table user (id int, name text)") ;}@ Overridepublic void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ){}}
The obtained database is located in/data/yourpackage/databases/zhycheng. db3.
Then, call
MyHelper mh=new MyHelper(this,"zhycheng");SQLiteDatabase db=mh.getReadableDatabase();SQLiteDatabase db=mh.getWritableDatabase();
Read-only and writable databases are obtained respectively.
After obtaining the database, you can perform database operations in the following two ways:
1. Execute SQL statements
db.rawQuery(sql,Args)db.execSQL(sql, Args)db.execSQL(sql)
The preceding SQL is a String-type database language, and Args is a String array. If "? "Corresponds to the following value.
2. Use specific methods
Insert Method
MyHelper mhz = new MyHelper (this, "zhycheng", 2); SQLiteDatabase dbz = mhz. getWritableDatabase (); ContentValues value = new ContentValues (); value. put ("id", 1); value. put ("name", "zhangyicheng"); dbz. insert ("user", null, value); // The second parameter must be null.
Delete Method
MyHelper mhsc=new MyHelper(this,"zhycheng",2); SQLiteDatabase dbsc=mhsc.getWritableDatabase(); dbsc.delete("user", "id=?", new String[]{"1"}); dbsc.close();
Change Method
MyHelper mhg=new MyHelper(this,"zhycheng",2);SQLiteDatabase dbg=mhg.getWritableDatabase();ContentValues vs=new ContentValues();vs.put("id", 1);dbg.update("user", vs, //set XXX=xx"id=?", new String[]{"4"}//id=4);//
Query Method
MyHelper mhcs = new MyHelper (this, "zhycheng", 2); SQLiteDatabase dbc = mhcs. getReadableDatabase (); Cursor c = dbc. query ("user", // table name new String [] {"id", "name"}, // query column "id =? ", New String [] {" 2 "}, // wherenull, null, null); while (c. moveToNext () {System. out. println (c. getString (c. getColumnIndex ("name ")));}
Finally, a function is very useful.
insert into user values(7,datetime(CURRENT_TIMESTAMP,'localtime'))