More content Welcome to my personal website: www.qingshuimonk.com view
SQLite is a lightweight relational database that currently supports five types of null,integer,real (floating point), text (string literal), BLOB (binary text).
The created database file is located in the/data/data/package-name/databases directory.
Files can be accessed through the File Explorer in Eclipse.
Steps for the SQLite database operation:
1. Establish the Sqliteopenhelper class for initializing and upgrading the database.
1 PackageCom.qingshuimonk.radarmonitor;2 3 ImportAndroid.content.Context;4 Importandroid.database.sqlite.SQLiteDatabase;5 Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;6 ImportAndroid.database.sqlite.SQLiteOpenHelper;7 8 Public classMysqlitehelperextendssqliteopenhelper{9 Ten PublicMysqlitehelper (context context, String name, Cursorfactory factory,intversion) { One Super(context, name, Factory, version); A } - - @Override the Public voidonCreate (Sqlitedatabase db) { - //TODO auto-generated Method Stub -Db.execsql ("CREATE table DataTable (name text, value real)"); - } + - @Override + //when the database version is inconsistent, the database version on the disk needs to be upgraded to the current version when called A Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { at //TODO auto-generated Method Stub - //Delete old table, create new table - //db.execsql ("DROP TABLE IF IT EXISTS" + database_table); - //onCreate (db); - } - in - to}
which
Db.execsql ("CREATE table DataTable (name text, value real)");
Directives are used to create databases and tables, the database name is a DataTable, and the contents of the table have a string literal name, a floating-point number value. Typically in use, you will also define a
_id Integer primary key autoincrement field
The line number used to represent the table.
2. Create or open a database
To create a variable:
1 Public Mysqlitehelper Mysqlitehelper; 2 Public Sqlitedatabase datadb;
To create a database:
1 // Create a database 2 New Mysqlitehelper (regionactivity. thisnull, 1); 3 datadb = Mysqlitehelper.getreadabledatabase ();
The Mysqlitehelper parameters are: Context, database name, factory (default null), version
3. Basic operation of the database:
Created: Datadb.execsql ("CREATE TABLE MyTable (_id integer primary key autoincrement, name text, value real)");
Close: datadb.close;
Increase:
Contentvalues CV = new Contentvalues ();
Cv.put ("_id", 1);
Cv.put ("name", "Length");
Cv.put ("value", 2);
Datadb.inset ("MyTable", NULL,CV);
Inquire:
You need to create a pointer to the data table cursor, and then pass the pointer to the index of the column indexed, pass the index to the appropriate method, and get the desired data.
cursor cursor = datadb.query ("MyTable", New string[]{"_id", "name", "value"},null,null,null,null,null);
while (Cursor.movetonext ()) {
int idindex = Cursor.getcolumnindex ("_id");
int id = cursor.getint (idindex);
int nameindex = Cursor.getcolumnindex ("name");
String name = cursor.getstring (Nameindex);
int valueindex = Cursor.getcolumnindex ("value");
Float value = cursor.getfloat (ValueIndex);
}
Some of the cursor's corresponding methods are:
MoveToNext ();
Movetoprevious ();
Movetofirst ();
Movetolast ();
Movetoposition ();
GetCount ();
GetPosition ();
Getcolumnindexorthrow ();
getColumnName ();
Getcolumnnames ();
See Android developers for details on how to use each method.
Modify:
String updatestring = "Update mytable set name= ' length ', value= ' where _id=1";
Datadb.execsql (updatestring);
Delete:
String deletestring= "Delete from mytable where _id=1";
Datadb.execsql (deletestring);