This article mainly records how to determine the existence of tables in the database in Android Sqlitedatabase and the basic operation of testing the Sqlitedatabase database. For a detailed description of SQLite, you can view the reproduced Android data storage--sqlite use detailed
Example :
androidmanifest.xml--did not make any changes to create the project by default
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "/http Schemas.android.com/apk/res/android "package=" Com.wxl.handlerthread "android:versioncode=" 1 "android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <application" Android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" Android:theme= "@style/apptheme" > <activity android:name= "com.wxl.handlerthread.MainActivity" Android:label= "@string/app_name" > <intent-filter> <action android:name= "a Ndroid.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter> </activity> </application></manifest>
Mainactivity.java
Package Com.example.sqlite;import Android.os.bundle;import Android.util.log;import android.app.activity;import Android.content.contentvalues;import Android.content.context;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqlitedatabase.cursorfactory;import Android.database.sqlite.sqliteopenhelper;public class Mainactivity extends Activity {private Sqlitedatabase db1;// Private Sqlitedatabase db2;private Mydatabaseutil mydatabaseutil; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Parameter two is the database file name Mydatabaseutil = new Mydatabaseutil (This, "db1.db", NULL, 1); DB1 = This.openorcreatedatabase ("Db1.db", context.mode_private, NULL); /*/data/data/com.example.sqlite/databases must exist, otherwise create an error, and finally databases instead of database, do not forget to add ' s ' *///DB2 = Sqlitedatabase.openorcreatedatabase ("/data/data/com.EXAMPLE.SQLITE/DATABASES/DB2.DB3 ", NULL); /* Create a table and determine if this table already exists, create and initialize the */if (!mydatabaseutil.tabisexist ("person")) {Db1.execsql ("CREATE TABLE" (_id Integer primary key autoincrement, name varchar (20)) "); Contentvalues values =new contentvalues (); for (int i=0;i<10;i++) {values.put ("name", "name" + i); Db1.insert ("Person", "_id", values); }}else {log.i ("+++++++++++", "already created, no need to create"); }/* Update database */contentvalues values =new contentvalues (); Values.put ("name", "WXL"); Db1.update ("person", Values, "_id=1", null); Db1.update ("person", Values, "_id=", New string[]{"5"}); /* Query data */Cursor C = db1.query ("person", NULL, NULL, NULL, NULL, NULL, NULL); C.movetofirst (); while (!c.isafterlast ()) {int index = c.getcolumnindex ("name"); LOG.D ("SQLite", C.getstring (index)); C.movetonext (); } c = Db1.rawquery ("SELECT * from", NULL); C.movetofirst (); while (!c.isafterlast ()) {int index = c.getcolumnindex ("name"); LOG.D ("SQLite", C.getstring (index)); C.movetonext (); }} public class Mydatabaseutil extends Sqliteopenhelper{public mydatabaseutil (context context, String Name,cursorfactory Factory, int version) {Super (context, name, Factory, version);//TODO auto-generated constructor stub} @Overridepublic void OnCreate (Sqlitedatabase arg0) {//TODO auto-generated method stub} @Overridepublic void Onupgrade ( Sqlitedatabase arg0, int arg1, int arg2) {//TODO auto-generated Method stub}/** * Determine if a table exists * @param tabn AME Table name * @return */public boolean tabisexist (String tabname) {Boolean result = Fals E if (tabname = = null) {return false; } sqlitedatabase db = null; cursor cursor = NULL; try {db = This.getreadabledatabase ();//This is inherited from the Sqliteopenhelper class String sql = "SELECT COUNT (*) as C from sqlite_master where type = ' table ' and name = '" +tabname.trim () + "'"; cursor = db.rawquery (sql, NULL); if (Cursor.movetonext ()) {int count = cursor.getint (0); if (count>0) {result = true; }}} catch (Exception e) {//TODO: Handle exception} return result; } } }
where the Getreadabledatabase () method is in the Sqliteopenhelper class
Create the table and initialize the resulting table data as follows :
table data from the updated data :
the data for the query data table shows the following results :
Android data storage--sqlite instances, determine whether tables exist in the database