Android Data Storage--sqlite Database

Source: Internet
Author: User

This article integrates the use of the Android database to save an entity class as an example.

First look at the database statement:

ORM: Relational Object mapping

Add Data:

new ContentValues(); values.put("name""小丽"); values.put("phone""110"); mDB.insert("student",//表名             null//规避插入语句的错误             values );//插入的值 

Delete data:

mDB.delete("student"//表名           "name =?",//条件语句           newString[]{"小丽"});//条件语句的占位符

To modify the data:

new ContentValues();  values.put("phone""10086");  mDB.update("student",//表名              //要修改的值              "name =?",//条件语句              newString[]{"小丽"});

Query data:

cursor cursor = Mdb.query ("Student",//Table name                NULL,//query fields                NULL,//Conditional statements                NULL,placeholder for the//conditional statement                NULL,//Grouping statements                NULL,//Group conditions                NULL);//SortBoolean tofirst = Cursor.movetofirst (); while(Tofirst) {StringName = Cursor.getstring (Cursor.getcolumnindex ("Name"));StringPhone = cursor.getstring (Cursor.getcolumnindex ("Phone")); MyData MyData =NewMyData (name, phone);            Datalist.add (MyData);        Tofirst = Cursor.movetonext (); }

Here are the steps to create and use a database

1. Method invocation

private DBMang_Order dbMang_Order; dbMang_Order=DBMang_Order.getInstance(getApplicationContext());//查询ArrayList<OrderUncheck> uncheckList = dbMang_Order.query();//删除dbMang_Order.delete(orderNo);//增加dbMang_Order.insert(new OrderUncheck(orderNo, account, action));//修改dbMang_Order.update(new OrderUncheck(orderNo, account, action));

2. First create the entity class to store

 Public classOrderuncheck {String OrderNo;DoubleAccountintAction PublicStringGetorderno() {returnOrderNo; } Public void Setorderno(String OrderNo) { This. OrderNo = OrderNo; } Public Double Getaccount() {returnAccount } Public void Setaccount(Doubleaccount) { This. account = account; } Public int getaction() {returnAction } Public void setaction(intAction) { This. Action = action; } Public Orderuncheck(String OrderNo,DoubleAccountintAction) {super (); This. OrderNo = OrderNo; This. account = account; This. Action = action; }}

3. Create a Database

ImportAndroid.content.Context;ImportAndroid.database.sqlite.SQLiteDatabase;ImportAndroid.database.sqlite.SQLiteOpenHelper; Public  class mysql_order extends sqliteopenhelper {     Private Static FinalString name ="Count";//Database name     Private Static Final intVersion =1;//Database version     The third parameter, cursorfactory, specifies the factory class that obtains a cursor instance when the query is executed, set to NULL, which represents the use of the system 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 ($), account VARCHAR (19100), action VARCHAR) ";    Db.execsql (SQL); }@Override     Public void Onupgrade(Sqlitedatabase DB,intOldversion,intNewVersion) {//TODO auto-generated method stub}}

4, the code is called in the method in this class: adding and deleting changes

 Public classDbmang_order {Private StaticDbmang_order mdbmanager=NewDbmang_order ();Private StaticSqlitedatabase mDb;Private StaticContext context;Private  Dbmang_order() {    } Public StaticSynchronized Dbmang_ordergetinstance(Context context) {Dbmang_order.context = context;if(mdb==NULL) {Mysql_order MySQL =NewMysql_order (context);        MDb = Mysql.getwritabledatabase (); }returnMdbmanager; }Interfacenewtable{String table_name="Neworder"; String table_column_orderno="OrderNo"; String table_column_account="Account"; String table_column_action="Action"; }//Increase     Public void Insert(Orderuncheck info) {Contentvalues values=NewContentvalues ();        Values.put (Newtable.table_column_orderno, Info.getorderno ());        Values.put (Newtable.table_column_account, Info.getaccount ());        Values.put (Newtable.table_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+"=?",NewString[]{orderno}); }//Change     Public void Update(Orderuncheck info) {Contentvalues values =NewContentvalues ();        Values.put (Newtable.table_column_account, Info.getaccount ()); Mdb.update (Newtable.table_name, values, newtable.table_column_orderno+"=? and "+newtable.table_column_orderno+"=?",NewString[]{string.valueof (Info.getorderno ()),""+info.getaccount ()}); }//Check     PublicArraylist<orderuncheck>Query() {Cursor cursor = Mdb.query (Newtable.table_name,NULL,NULL,NULL,NULL,NULL,NULL); Arraylist<orderuncheck> list=NewArraylist<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));DoubleAccountd = double.parsedouble (account);intActiond=integer.parseint (action); Orderuncheck order =NewOrderuncheck (OrderNo, Accountd,actiond);            List.add (order);        Tofirst=cursor.movetonext (); } cursor.close ();returnList }       }

Android Data Storage--sqlite Database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.