Using SQLite to create a database on android, sqliteandroid
Use SQLite to create a database on android
Method:
1. Create a database class that inherits the SQLiteOpenHelper class
Rewrite related functions and initialize database objects.
Public MySQLiteOpenHelper (Context context, int version)
Super (context, "fanfan2.db", null, version );
Helper. getReadableDatabase ();
Override onCreate and onUpgrade
2. Create a database object and obtain the database object
MySQLiteOpenHelper helper = new MySQLiteOpenHelper (getContext (), 1 );
Helper. getReadableDatabase ();
3. To create a new table, type execSQL in oncreate.
In this way, a new student table is created.
public void onCreate(SQLiteDatabase db) { Log.d("fanfan", "onCreate"); db.execSQL("CREATE TABLE student(id INTEGER,name TEXT)"); }
4. oncreate is called only when a new database is created. If you have created a database before, it will not be called now.
OnUpgrade is called only after the database version is updated.
Code:
Com. fry. MySQLiteOpenHelper
1 package com. fry; 2 3 import android. content. context; 4 import android. database. sqlite. SQLiteDatabase; 5 import android. database. sqlite. SQLiteDatabase. cursorFactory; 6 import android. database. sqlite. SQLiteOpenHelper; 7 import android. util. log; 8 9 public class MySQLiteOpenHelper extends SQLiteOpenHelper {10 11/* 12*1. context 13*2. Database Name 14*3. factory15 * 4. version database file version 16*/17 18 public MySQLiteOpenHelper (Context context, int version) {19 super (context, "fanfan2.db", null, version ); 20 // TODO Auto-generated constructor stub21} 22 23/* call 24 * (non-Javadoc) 25 * @ see android after the database file is created successfully. database. sqlite. SQLiteOpenHelper # onCreate (android. database. sqlite. SQLiteDatabase) 26 */27 @ Override28 public void onCreate (SQLiteDatabase arg0) {29 Log. d ("fanfan", "onCreate"); 30} 31 32/* Call 33 * (non-Javadoc) 34 * @ see android after the database file is updated. database. sqlite. SQLiteOpenHelper # onUpgrade (android. database. sqlite. SQLiteDatabase, int, int) 35 */36 @ Override37 public void onUpgrade (SQLiteDatabase arg0, int arg1, int arg2) {38 Log. d ("fanfan", "onUpgrade"); 39} 40 41}Database
Com. fanfan. test. Test
1 package com.fanfan.test; 2 3 4 import com.fry.Activity01; 5 import com.fry.MySQLiteOpenHelper; 6 7 import android.annotation.SuppressLint; 8 import android.test.AndroidTestCase; 9 import android.util.Log;10 11 public class Test extends AndroidTestCase{12 @SuppressLint("NewApi") public void test1(){13 MySQLiteOpenHelper helper=new MySQLiteOpenHelper(getContext(), 1);14 helper.getReadableDatabase();15 String name=helper.getDatabaseName();16 Log.d("test1", name);17 }18 19 20 }Java Unit Testing
/CreateDatabase2/AndroidManifest. xml
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.example.createDatabase_2" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-sdk 7 android:minSdkVersion="8" 8 android:targetSdkVersion="19" /> 9 10 <application11 android:allowBackup="true"12 android:icon="@drawable/ic_launcher"13 android:label="@string/app_name"14 android:theme="@style/AppTheme" >15 16 <uses-library android:name="android.test.runner"/>17 18 <activity19 android:name="com.fry.MainActivity"20 android:label="@string/app_name" >21 <intent-filter>22 <action android:name="android.intent.action.MAIN" />23 24 <category android:name="android.intent.category.LAUNCHER" />25 </intent-filter>26 </activity>27 <activity android:name="com.fry.Activity01" android:exported="true"></activity>28 </application>29 <instrumentation 30 android:name="android.test.InstrumentationTestRunner"31 android:targetPackage="com.example.createDatabase_2" android:label="Test for My app"32 ></instrumentation>33 34 </manifest>
Configuration File