本文主要记录一下Android SQLiteDatabase中如何判断数据库中表是否存在,以及测试SQLiteDatabase数据库的基本操作。有关SQLite的详细说明,可以查看转载的Android 数据存储——SQLite使用详解
例子:
AndroidManifest.xml——没有做任何修改,创建工程默认
<?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="19" /> <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="android.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); //参数二是数据库文件名 myDatabaseUtil = new MyDatabaseUtil(this, "db1.db", null, 1); db1 = this.openOrCreateDatabase("db1.db", Context.MODE_PRIVATE, null); /*/data/data/com.example.sqlite/databases必须存在,否则创建出错,最后是databases而不是database,不要忘了加's'*/ // db2 = SQLiteDatabase.openOrCreateDatabase("/data/data/com.example.sqlite/databases/db2.db3",null); /*创建表,并判断是否已经存在此表,没创建,则创建并初始化*/ if (!myDatabaseUtil.tabIsExist("person")) { db1.execSQL("CREATE TABLE person (_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("+++++++++++", "已经创建了,无需再创建"); } /*更新数据库*/ ContentValues values =new ContentValues(); values.put("name", "wxl"); db1.update("person", values, "_id=1", null); db1.update("person", values, "_id=?", new String[]{"5"}); /*查询数据*/ 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 person", 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 } @Override public void onCreate(SQLiteDatabase arg0) { // TODO Auto-generated method stub } @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { // TODO Auto-generated method stub } /** * 判断某张表是否存在 * @param tabName 表名 * @return */ public boolean tabIsExist(String tabName){ boolean result = false; if(tabName == null){ return false; } SQLiteDatabase db = null; Cursor cursor = null; try { db = this.getReadableDatabase();//此this是继承SQLiteOpenHelper类得到的 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; } } }
其中getReadableDatabase()方法是SQLiteOpenHelper类中的
创建表并初始化得到的表数据如下:
更新数据得到的表数据:
查询数据表的数据显示结果如下:
时间: 2024-10-15 15:09:36