1. Features
Compared with other data types. It can store any type, except the primary key. If the primary key is Integer primary key, only 64-bit integers can be stored.
1) the following statement can be used to create table:
Create Table person (personid integer primary key autocrement, name varchar (20 ))
We recommend that you write varchar (20) later.
2) Get the id value after auto-increment: Select last_insert_rowid ()
Case:
1. Create a database
Sqliteopenhelper. getreadabledatabase () or getwritabledatabase ()
Automatically create a database.
Package COM. tong. service; import android. content. context; import android. database. SQLite. sqlitedatabase; import android. database. SQLite. sqlitedatabase. cursorfactory; import android. database. SQLite. sqliteopenhelper; public class dbopen extends sqliteopenhelper {public dbopen (context) {// database name Tong. DB uses the system's default cursor. the version number of the factory database file is super (context, "tong. DB ", null, 1) ;}@ overridepublic void oncreate (sqlitedatabase dB) {// The database table generated when the data is first created // sqlitedatabase is encapsulated with db.exe csql ("create table person (personid integer primary key autoincrement, name varchar (20 )) ") ;}@ overridepublic void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {// call when the version number changes // execute the update operation // use a field db.exe csql ("alter table person add phone varchar (12) null ");}}
Write a test class to create a database.
Package COM. tong. test; import COM. tong. service. dbopen; import android. test. androidtestcase; public class dbopentest extends androidtestcase {public void testcreatedb () throws exception {dbopen = new dbopen (getcontext (); // automatically create a database dbopen. getreadabledatabase ();}}
Do not forget to add the configuration file to the unit test:
<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. example. dbapp"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<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">
<Activity
Android: Name = "com. Tong. dbapp. 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>
<Uses-library Android: Name = "android. Test. Runner"/>
</Application>
<Instrumentation Android: Name = "android. Test. instrumentationtestrunner"
Android: targetpackage = "com. example. dbapp" Android: Label = "test Tong app">
</Instrumentation>
</Manifest>
Run the test case. The database has been created!
Android_metadata is an automatically created table that records language information.