Original address: http://blog.csdn.net/wirelessqa/article/details/8583101
Java provides a considerable number of encapsulated classes to perform operations on Sqllite, such as:
Sqlitedatabase is a class of database operations, similar to what we write in C # Helper,ps (Java is really highly encapsulated)
Sqliteopenhelper as the base class for maintaining and managing databases
Example: Member Information management
Functions: 1. View database 2. Clear Database 3. Add Member 4. Delete Member 5. Update members 6. Find a Member
DBHelper class inherits from Sqliteopenhelper
PackageCom.wirelessqa.sqlite;ImportAndroid.content.Context;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.database.sqlite.SQLiteOpenHelper;ImportAndroid.util.Log;/*** DBHelper inherits Sqliteopenhelper as the base class for maintaining and managing databases *@authorBixiaopeng 2013-2-16 pm 3:05:52*/ Public classDBHelperextendssqliteopenhelper{//Final member variable is a constant can only be assigned once Public Static FinalString db_name = "Wirelessqa.db"; Public Static FinalString db_table_name = "Info"; Private Static Final intDb_version=1; PublicDBHelper (Context context) {//context Context, String name, Cursorfactory factory, int version//Factory Enter NULL, use default value Super(Context, Db_name,NULL, db_version); } //OnCreate is called when the data is first created@Override Public voidonCreate (Sqlitedatabase db) {//Create a tableDb.execsql ("CREATE TABLE IF not EXISTS info" + "(_id INTEGER PRIMARY KEY autoincrement, name VARCHAR, Age INTEGER, website String,weibo STRING) "); LOG.I (Wirelessqa.tag,"CREATE TABLE"); } //The first time the database is created, the OnCreate method is called, and we can execute the statement that creates the table.//when the system discovers the version changes, it calls the Onupgrade method, and we can execute statements such as modifying the table structure .@Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { //add a column of other in table info//db.execsql ("ALTER TABLE info ADD COLUMN other STRING"); LOG.I ("WIRELESSQA", "Update SQLite" +oldversion+ "---->" +newversion); } }
Rely on DBHelper and sqlitedatabase for business operations:
PackageCom.wirelessqa.sqlite;Importjava.util.ArrayList;Importjava.util.List;Importandroid.content.ContentValues;ImportAndroid.content.Context;ImportAndroid.database.Cursor;Importandroid.database.sqlite.SQLiteDatabase;ImportAndroid.util.Log;/***dbmanager is built on top of dbhelper, encapsulating common business methods *@authorBixiaopeng 2013-2-16 pm 3:06:26*/ Public classDBManager {PrivateDBHelper Helper; PrivateSqlitedatabase DB; PublicDBManager (Context context) {Helper=NewDBHelper (context); //get database operation from helper the helper has encapsulated the databases.db =helper.getwritabledatabase (); } /*** Add multiple member information to table info * *@paramMemberInfo*/ Public voidAdd (list<memberinfo>memberInfo) {db.begintransaction ();//Start a transaction Try { for(MemberInfo info:memberinfo) {log.i (Wirelessqa.tag,"------Add MemberInfo----------"); LOG.I (Wirelessqa.tag, Info.name+ "/" + Info.age + "/" + Info.website + "/" +Info.weibo); //inserting data into table infoDb.execsql ("INSERT into info VALUES (null,?,?,?,?)",Newobject[] {info.name, info.age, Info.website, Info.weibo}); } db.settransactionsuccessful ();//Transaction Success}finally{db.endtransaction ();//End Transaction } } /**add a member information to table info *@param_id *@paramname *@paramAge *@paramwebsite *@paramWeibo*/ Public voidAddint_id, String name,intAge , String website, string Weibo) {log.i (Wirelessqa.tag,"------Add Data----------"); Contentvalues CV=Newcontentvalues (); //cv.put ("_id", _id);Cv.put ("name", name); Cv.put ("Age", age); Cv.put ("Website", website); Cv.put ("Weibo", Weibo); Db.insert (Dbhelper.db_table_name,NULL, CV); LOG.I (Wirelessqa.tag, name+ "/" + Age + "/" + website + "/" +Weibo); } /*** Delete Data by name * *@paramname*/ Public voiddeldata (String name) {//Execsql ("DELETE from info WHERE name =" + "'" +name+ "'");string[] args ={name}; Db.delete (Dbhelper.db_table_name,"Name=?", args); LOG.I (Wirelessqa.tag,"Delete data by" +name); } /*** Erase Data*/ Public voidClearData () {Execsql ("DELETE from Info"); LOG.I (Wirelessqa.tag,"Clear Data"); } /*** Search information by name, return all data * *@paramname*/ PublicArraylist<memberinfo> Searchdata (FinalString name) {String SQL= "SELECT * FROM info WHERE name =" + "'" + Name + "'"; returnexecsqlformemberinfo (SQL); } PublicArraylist<memberinfo>Searchalldata () {String SQL= "SELECT * FROM Info"; returnexecsqlformemberinfo (SQL); } /*** Change the value by name * *@paramRaw *@paramRawValue *@paramWherename*/ Public voidUpdateData (String Raw, String RawValue, String wherename) {String sql= "UPDATE info SET" + raw + "=" + "" + "'" + RawValue + "' +" WHERE name = "+" ' "+Wherename+ "'"; Execsql (SQL); LOG.I (Wirelessqa.tag, SQL); } /*** Execute SQL command to return list * *@paramSQL *@return */ PrivateArraylist<memberinfo>execsqlformemberinfo (String sql) {ArrayList<MemberInfo> list =NewArraylist<memberinfo>(); Cursor C=execsqlforcursor (SQL); while(C.movetonext ()) {MemberInfo Info=NewMemberInfo (); info._id= C.getint (C.getcolumnindex ("_id")); Info.name= C.getstring (C.getcolumnindex ("name")); Info.age= C.getint (C.getcolumnindex ("Age")); Info.website= C.getstring (C.getcolumnindex ("website")); Info.weibo= C.getstring (C.getcolumnindex ("Weibo")); List.add (info); } c.close (); returnlist; } /*** Execute an SQL statement without returning any data * *@paramSQL*/ Private voidexecsql (String sql) {Try{db.execsql (SQL); LOG.I ("Execsql:", SQL); } Catch(Exception e) {LOG.E ("Execsql Exception", E.getmessage ()); E.printstacktrace (); } } /*** Execute SQL, return a cursor * *@paramSQL *@return */ Privatecursor execsqlforcursor (String sql) {cursor C= Db.rawquery (SQL,NULL); returnC; } Public voidClosedb () {db.close (); }}
Javabean–memberinfo.java of member informationHome Show –mainactivity.java
Show results page –displayactivity.java
source Download: http://download.csdn.net/detail/wirelessqa/5066148
"Android data Storage" SQLite use example (with source) (reproduced)