Sqllite is similar to Microsoft Access. The following describes how to create a database file in Android.
1. dbopenhelper
Package COM. njupt. SQLite; import android. content. context; import android. database. SQLite. sqlitedatabase; import android. database. SQLite. sqliteopenhelper; public class dbopenhelper extends sqliteopenhelper {public dbopenhelper (context) {super (context, "njupt1.db", null, 2);} public void oncreate (sqlitedatabase dB) {// if the file does not exist, run system. out. println ("oncreate... Zhang Zetian is my goddess 1" mongomongodb.exe csql ("create table person1 (ID integer primary key autoincrement, name varchar (20 ))");} @ overridepublic void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {// The file exists. When the version is upgraded, run system. out. println ("onupdate · my goddess is Zhang Zetian 1" mongomongodb.exe csql ("alter table person1 add balance integer ");}}
2. dbtest
Package COM. njupt. SQLite; import android. test. androidtestcase; public class dbtest extends androidtestcase {public void test1 () {dbopenhelper helper = new dbopenhelper (getcontext (); helper. getwritabledatabase (); // get writable database connections }}
3. The result is as follows:
1)
Explanation: The first log is output at the first execution. The second log is output when the database version is upgraded.
2)
Explanation: After the database is successfully created, you will find a new directory named databases in the/data/project, the database file you created is saved in the databases directory.