Android data storage-SQLite database (Template), androidsqlite
This article integrates the use of the Android database to save an object class as an example.
Let's take a look at the database statements:
ORM: relational object ing
Add data:
ContentValues values = new ContentValues (); values. put ("name", "Lili"); values. put ("phone", "110"); mDB. insert ("student", // The table name is null, // avoid the wrong values of the insert statement); // The inserted Value
Delete data:
MDB. delete ("student", // table name "name =? ", // Condition Statement new String [] {" Lili "}); // placeholder for the Condition Statement
Modify data:
ContentValues values = new ContentValues (); values. put ("phone", "10086"); mDB. update ("student", // table name values, // value to be modified "name =? ", // Condition Statement new String [] {" Lili "});
Query data:
Cursor cursor = mDB. query ("student", // table name null, // query field null, // Condition Statement null, // Condition Statement placeholder null, // grouping statement null, // grouping condition null); // sorting boolean toFirst = cursor. moveToFirst (); while (toFirst) {String name = cursor. getString (cursor. getColumnIndex ("name"); String phone = cursor. getString (cursor. getColumnIndex ("phone"); MyData myData = new MyData (name, phone); dataList. add (myData); toFirst = cursor. moveToNext ();}
The steps for creating and using a database are as follows:
1. method call
Private DBMang_Order dbMang_Order; dbMang_Order = DBMang_Order.getInstance (getApplicationContext (); // query ArrayList <OrderUncheck> uncheckList = dbMang_Order.query (); // Delete dbMang_Order.delete (orderNo ); // Add dbMang_Order.insert (new OrderUncheck (orderNo, account, action); // modify dbMang_Order.update (new OrderUncheck (orderNo, account, action ));
2. Create the object class to be stored
public class OrderUncheck { String orderNo; double account; int action; public String getOrderNo() { return orderNo; } public void setOrderNo(String orderNo) { this.orderNo = orderNo; } public double getAccount() { return account; } public void setAccount(double account) { this.account = account; } public int getAction() { return action; } public void setAction(int action) { this.action = action; } public OrderUncheck(String orderNo, double account, int action) { super(); this.orderNo = orderNo; this.account = account; this.action = action; }}
3. Create a database
Import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteOpenHelper; public class MySQL_Order extends SQLiteOpenHelper {private static final String name = "count"; // database name private static final int version = 1; // database version // The third parameter, CursorFactory, specifies the factory class for obtaining a cursor instance during query execution. Setting it to null indicates using the default factory class public MySQL_Order (Context context) {super (context, name, null, version) ;}@ Override public void onCreate (SQLiteDatabase db) {String SQL = "CREATE TABLE neworder (_ id INTEGER PRIMARY KEY AUTOINCREMENT, orderNo VARCHAR (200), account VARCHAR (19100), action VARCHAR (200) "; db.exe cSQL (SQL) ;}@ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stub }}
4. The method called in the Code is added, deleted, modified, and queried in this class.
Public class DBMang_Order {private static DBMang_Order mDbManager = new DBMang_Order (); private static SQLiteDatabase mDb; private static Context context; private DBMang_Order () {} public static synchronized DBMang_Order getInstance (Context context) {DBMang_Order.context = context; if (mDb = null) {MySQL_Order mySQL = new MySQL_Order (context); mDb = mySQL. getWritableDatabase ();} return mDbManager ;} Interface NewTable {String TABLE_NAME = "neworder"; String TABLE_COLUMN_orderNo = "orderNo"; String TABLE_COLUMN_account = "account"; String TABLE_COLUMN_action = "action ";} // Add public void insert (OrderUncheck info) {ContentValues values = new ContentValues (); values. put (NewTable. TABLE_COLUMN_orderNo, info. getOrderNo (); values. put (NewTable. TABLE_COLUMN_account, info. getAccount (); values. put (NewTable. TAB LE_COLUMN_action, info. getAction (); Log. w ("mDb. insert "," "+ info); Log. w ("mDb. insert "," "+ values); mDb. insert (NewTable. TABLE_NAME, null, values);} // delete public void delete (String orderNo) {Log. w ("delete", orderNo); mDb. delete (NewTable. TABLE_NAME, NewTable. TABLE_COLUMN_orderNo + "=? ", New String [] {orderNo});} // change public void update (OrderUncheck info) {ContentValues values = new ContentValues (); values. put (NewTable. TABLE_COLUMN_account, info. getAccount (); mDb. update (NewTable. TABLE_NAME, values, NewTable. TABLE_COLUMN_orderNo + "=? And "+ NewTable. TABLE_COLUMN_orderNo +" =? ", New String [] {String. valueOf (info. getOrderNo (), "" + info. getAccount ()});} // query public ArrayList <OrderUncheck> query () {Cursor cursor = mDb. query (NewTable. TABLE_NAME, null, null); ArrayList <OrderUncheck> list = new ArrayList <OrderUncheck> (); boolean toFirst = cursor. moveToFirst (); while (toFirst) {String orderNo = cursor. getString (cursor. getColumnIndex (NewTable. TABLE_COLUMN_orderNo); String account = cursor. getString (cursor. getColumnIndex (NewTable. TABLE_COLUMN_account); String action = cursor. getString (cursor. getColumnIndex (NewTable. TABLE_COLUMN_account); double accountd = Double. parseDouble (account); int actiond = Integer. parseInt (action); OrderUncheck order = new OrderUncheck (orderNo, accountd, actiond); list. add (order); toFirst = cursor. moveToNext ();} cursor. close (); return list ;}}