The Android operating system uses the SQLite database, which uses two methods to obtain database objects:
1. Get a database that already exists
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 how the database is opened.
This obtains the database object.
2. Create your own database
Create a new class, Inherit Sqliteopenhelper, add a method that is not implemented
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) {db.execsql ("CREATE table user (id int,name text)");} @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {}}
The obtained database is located in/DATA/DATA/YOURPACKAGE/DATABASES/ZHYCHENG.DB3
Then, by generating an object of the Myhelper class, call the
Myhelper mh=new Myhelper (This, "Zhycheng"); Sqlitedatabase db=mh.getreadabledatabase (); Sqlitedatabase db=mh.getwritabledatabase ();
Get a read-only and writable database, respectively.
Database operations can be performed after the database has been obtained the following two ways to manipulate the database
1. Execute SQL statements
Db.rawquery (Sql,args) db.execsql (SQL, Args) db.execsql (SQL)
The SQL above is a string-type database language, and args is a string array. If there is "?" in the preceding string Corresponds to the value that follows.
2. Use a specific method
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");d bz.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 ();
Modification method
Myhelper mhg=new Myhelper (This, "Zhycheng", 2); Sqlitedatabase dbg=mhg.getwritabledatabase (); Contentvalues vs=new contentvalues () vs.put ("id", 1);d bg.update ("User", VS,//set xxx=xx "Id=?", New string[]{"4"}//id= 4);//
Check method
Myhelper mhc=new Myhelper (This, "Zhycheng", 2); Sqlitedatabase dbc=mhc.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, there is a function that is useful
Insert into user values (7,datetime (current_timestamp, ' localtime '))