Data storage in Android (3) and android Storage
The best data storage method in Android: relational database sqlite.
Create a database: Use the SqliteOpenHelper class
IntegrationSqliteOpenHelperClass andSQLiteDatabaseClass help, we can easily perform CURD operations on the database.
Database creation:
We mainly create a database table in the onCreate method of the SqliteOpenHelper class.
So when is the onCreate method executed?
Let's first write the code:
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(20))";public DatabaseHelper(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){}}
Through the above code, we can create a dbsample. db database.
In fact, when we create an instance of this class using the new keyword, the database file is not created.
In fact, the database was created during the first connection.
That is, it is created through the getReadableDatabase () method of SqliteOpenHelper or the getWritableDatabase method..
The onCreate method of SqliteOpenHelper is also executed at this time.
Therefore, we can create the required table in the onCreate method.
The following is the MainActivity code:
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); // when this row is executed, the database is not created DatabaseHelper dbh = new DatabaseHelper (this ); // when this row is executed, the data is created. SQLiteDatabase readableDatabase = dbh. getReadableDatabase ();}}
After creating the database, we can perform the CURD operation on the database.
Insert data to the database:
UseSQLiteDatabaseClassExecSQLMethods, we can easily operate the database:
Note that the database objects returned by getReadableDatabase are read-only.
To write data to the database, 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. o NCreate (savedInstanceState); setContentView (R. layout. activity_main); // when this row is executed, dbh = new DatabaseHelper (this) is not created for the database; // when this row is executed, the data is created readableDatabase = dbh. getReadableDatabase ();} public void insertData (View view) {SQLiteDatabase writableDatabase = dbh. getWritableDatabase (); if (writableDatabase. isOpen () {for (int I = 0; I <20; I ++) {// execute the data import operation writabledatabase.exe cSQL ("insert into student values (? ,?,?); ", New Object [] {I + 2, 10 + I," android "+ I});} writableDatabase. close (); Toast. makeText (this, "data inserted successfully", Toast. LENGTH_SHORT ). show ();}}}
Let's take a look at the running result:
Now, we use sqlite3 to check whether the data is actually inserted.
As you can see, many data entries are inserted.
In addition to the execSQL method, the SqliteDatabase class also has other methods for operating the database:
- 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 orderBy)
- RawQuery (String SQL, String [] selectionArgs)
When execSQL is used to execute any query statement, no results are returned.
To return the result, use the rawQuery method.
Of course, the above is only part of the method. Others can go to the official website to view the API
Other database search, update, and delete operations are just based on images. You can study it on your own.