Create an SQLite database in Android, androidsqlite
Database: SQLite (lightweight, embedded database)
Store large amounts of data with similar structures and perform quick queries. Special files (generated in a certain format)
Database creation
Create a file
1. Declare the object. The object will not be created.
File file = new File ("File name ");
2. Write the file (the file will be created)
FileOutputStream fos = new FileOutputStream (file );
Fos. write ("hdahfdsaklh". getbytes ());
Create a database
1. Implement the sub-class PersonSQLiteOpenHelper of SQLiteOpenHelper
public class PersonSQLiteOpenHelper extends SQLiteOpenHelper { public PersonSQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase arg0) { // TODO Auto-generated method stub } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub }}
Several parameters of SQLiteOpenHelper are described as follows:
android.database.sqlite.SQLiteOpenHelper.SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version)Create a helper object to create, open, and/or manage a database. This method always returns very quickly. The database is not actually created or opened until one of getWritableDatabase or getReadableDatabase is called.Parameters:context to use to open or create the databasename of the database file, or null for an in-memory databasefactory to use for creating cursor objects, or null for the defaultversion number of the database (starting at 1); if the database is older, onUpgrade will be used to upgrade the database; if the database is newer, onDowngrade will be used to downgrade the database
2. getWritableDatabase/getReadableDatabase ()
Create TestPersonDB. java
package com.wuyudong.db.test;import com.wuyudong.db.PersonSQLiteOpenHelper;import android.test.AndroidTestCase;public class TestPersonDB extends AndroidTestCase { public void testCreateDB() throws Exception { PersonSQLiteOpenHelper helper = new PersonSQLiteOpenHelper(getContext()); helper.getWritableDatabase(); }}
3. The method of executing the first onCreate () database creation is suitable for initializing the database table structure.
// Called when the database is created for the first time. // method executed when the database is created for the first time // if the database has been created, it will not be executed again. // Suitable for initializing the database table structure // db represents the current database @ Override public void onCreate (SQLiteDatabase db) {// initialize the database table structure db.exe cSQL ("create table person (id integer primary key autoincrement, name varchar (20), number varchar (20 ))");}
Note:
Sqlite database is an embedded lightweight database that does not distinguish data types internally and does not check the data length.
Create AndroidTest Project test
Copy the following code from AndroidManifest. xml to AndroidManifest. xml of the project.
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wuyudong.db.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.wuyudong.db" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <uses-library android:name="android.test.runner" /> </application></manifest>
The copied code is as follows:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wuyudong.db" android:versionCode="1" android:versionName="1.0" > <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.wuyudong.db" /> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <uses-library android:name="android.test.runner" /> <activity android:name="com.wuyudong.db.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
The project structure is as follows:
The complete code of PersonSQLiteOpenHelper. java is as follows:
Package com. wuyudong. db; import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteOpenHelper; public class PersonSQLiteOpenHelper extends SQLiteOpenHelper {/*** defines a help class for database creation, there are two methods to implement the equivalent of the file class ** @ author Administrator **/public PersonSQLiteOpenHelper (Context context) {// context Context // person. db database file name/factory cursor factory/version Database The version number starts from 1. super (context, "person. db ", null, 1); // TODO Auto-generated constructor stub} // Called when the database is created for the first time. // method executed when the database is created for the first time // if the database has been created, it will not be executed again. // Suitable for initializing the database table structure // db represents the current database @ Override public void onCreate (SQLiteDatabase db) {// initialize the database table structure db.exe cSQL ("create table person (id integer primary key autoincrement, name varchar (20), number varchar (20 ))");} // called when the database version is upgraded. // The database can only be upgraded and cannot be downgraded. @ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stub }}
Run the program
Open person. db