The best way to store data under Android: relational database SQLite.
Database creation: Using the Sqliteopenhelper class
Combined with the help of the Sqliteopenhelper class and the Sqlitedatabase class, we can perform curd operations on the database conveniently.
Creation of the database:
We primarily create database tables in the OnCreate method of the Sqliteopenhelper class.
So, when did the OnCreate method execute?
Let's write the code first:
Package Com.whathecode.db;import Android.content.context;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqliteopenhelper;public class Databasehelper extends sqliteopenhelper{public static final String create_table = "CREATE TABLE student (" + "ID integer primary key," + "Age Integer," + "name char ())";p ublic Databa Sehelper (Context context) {Super (context, "dbsample.db", NULL, 1);} @Overridepublic void OnCreate (Sqlitedatabase db) {db.execsql (create_table);} @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {}}
With the code above, we can create a dbsample.db database.
In fact, when we create an instance of this class with the new keyword, the database file is not created.
In fact, the database was created the first time we connected.
That is, it is created through the Sqliteopenhelper getreadabledatabase () method or the Getwritabledatabase method .
Sqliteopenhelper's OnCreate method is also performed at this time.
Therefore, we can create the required tables in the OnCreate method.
Here is the code for mainactivity:
Package Com.whathecode.sqlitedemo;import Android.app.activity;import android.database.sqlite.SQLiteDatabase; Import Android.os.bundle;import Com.whathecode.db.databasehelper;public class Mainactivity extends Activity{@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);//The database is not created databasehelper DBH = new Databasehelper (this) when performing to this line;// The data is created when the line is executed sqlitedatabase readabledatabase = Dbh.getreadabledatabase ();}}
Once the database is created, we can perform curd operations on the database.
To insert data into the database:
Using the method of Execsql in the Sqlitedatabase class, we can easily manipulate the database:
Note the database object returned by Getreadabledatabase is read-only.
Now that we need to write to the database, we need to call the Getwritabledatabase method
Updated mainactivity code:
package Com.whathecode.sqlitedemo;import android.app.activity;import Android.database.sqlite.sqlitedatabase;import Android.os.bundle;import Android.view.view;import Android.widget.toast;import Com.whathecode.db.databasehelper;public class Mainactivity extends Activity{ Sqlitedatabase readabledatabase = Null;databasehelper DBH = null, @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);// The database was not created when the row was executed DBH = new Databasehelper (this),///////////When the data is executed to this line readabledatabase = Dbh.getreadabledatabase ();} public void InsertData (view view) {Sqlitedatabase writabledatabase = Dbh.getwritabledatabase (); Writabledatabase.isopen ()) {for (int i = 0; i <; i++) {//Perform data insert operation Writabledatabase.execsql ("INSERT into student values (?,?,?); ", new object[] {i + 2, + I," Android "+ i});} Writabledatabase.close (); Toast.maketext (this, "Data insertion succeeded", Toast.length_short). Show ();}}}
Look at the results of the operation:
Now, let's use the Sqlite3 tool to see if the data is actually plugged in.
See, a lot of data has been inserted.
The Sqlitedatabase class has other methods to manipulate the database in addition to the Execsql method:
- Create (Sqlitedatabase.cursorfactory Factory)
- Delete (string table, String Whereclause, string[] whereargs)
- Query (string table, string[] columns, string selection, string[] Selectionargs, String groupBy, String having, string Orde Rby)
- Rawquery (String sql, string[] selectionargs)
There is no return result when using Execsql to execute any of the enquiry statements.
if you want to return the results, you need to use the Rawquery method.
Of course, these are only some of the methods, others can go to the official website to view the API
Other database search, update, delete operations are only the gourd painting. We can study it on our own.
How to store data under Android (iii)