?
Convention: Three typical local data storage methods for Android
- Sharedpreferences
- Save in local storage in a file format
- SQL Database
This section is about the use of sqlitedatabase, and in terms of usage it just shows how to create a database, create a table, write data. And then query it out.
This section does not write a usage method because you have encountered a condition that cannot be highlighted and is ambiguous when you insert multiple lines of code in a tabular form before in Word.
For more information, please visit my GitHub project directly: https://github.com/Chenstyle/AndroidTraining
Specific comments are also written in the code. So, on the code:
PackageCom.Zhizhao.androidtraining. Save_data import android.content.contentvalues; import android.content.context ; import android.database.cursor ; import android.database.sqlite.sqlitedatabase; import android.util.log ; /** * Created by Zhizhao on 2015/11/2 0002 in 10:53. * Ps: This class is written in the end and I think I can't go on like this. So go on and just add some Android basic operation to use * and I am more inclined to improve my project experience faster. * in terms of project experience, there's nothing more obvious than actually doing a project upgrade yourself. * so next, learn the open source project and publish it. * the project will continue to be written and submitted. Blog will also continue to follow up. */ public classUsingdatabase{ PrivateString DbName; PrivateString TableName; PrivateContext DbContext; PrivateString UserName; PrivateString UserPassword;
PublicUsingdatabase(StringDbName,StringTableName, ContextContext,StringData1,StringData2){ This.DbName=DbName; This.TableName=TableName; this. DbContext = context this. UserName = data1 this. UserPassword = data2} public void createusertable () { //openorcreatedatabase (String name, int mode, Cursorfactory factory); // The first parameter is the database name, the second is a permission, and the third iscursorfactoryobjects, // returns when used for queriesCursorthe child class object. or incomingNULLUse the defaultFactoryConstruction SqlitedatabaseDb=DbContext.Openorcreatedatabase(DbName,DbContext.Mode_private,Null); StringCmd="CREATE TABLE IF not EXISTS"+TableName+"(name varchar, password varchar)"; Db.Execsql(cmd); contentvalues CV = Span style= "COLOR: #93c763" >new contentvalues (); cv. Put ( "name" , username); cv. Put ( "password" , userpassword); // This is killing me. Remember, it is the name of the table. Db.Insert(TableName,Null,Cv); Db.Close(); }
public voidInsertusertable(){ SqlitedatabaseDb=DbContext.Openorcreatedatabase(DbName,DbContext.mode_privatenull } public void queryusertable () { sqlitedatabase db = dbcontext. Openorcreatedatabase (dbname dbcontext.mode_private, null // the results of the query are passedCursorreturn. cursors that represent datasets cursor cursor = db. Rawquery("select * from"+tableName, null); Log. V(" Results of database queries===", "----------"); While(cursor. MoveToNext()) { //query based on the column sequence of the database table. String str = cursor. GetString(0); Log. V(" Results of database queries= = = ", " "+str); }
Log. V(" Results of database queries===", "+++"); Cursor. Close(); DB. Close(); }
} |
?
Recently a lot of things have happened, because the internship of the company side of some problems, there is an urgent transfer of the company's ideas. So upgrading your skills has become the most pressing thing at the moment. Future blogs will be on the Android Training side in succession.
Of course, if you encounter any good ideas or skills in learning open source projects, you will share them. Basically keep it to the point of the day.
Sqlitedatabase of Android data storage