Android SQlite - 使用动态表

时间:2017-03-27 14:07:19

标签: android sqlite android-sqlite sqliteopenhelper

我正在构建一个Android原生移动应用程序,我需要在我的应用程序中使用用户输入动态创建表。通过阅读文档,看起来我不能使用 SQLiteOpenHelper ,因为Table只需要在构造函数或onCteate()方法中创建。所以我创建了自己的实用工具类,初始化为 SQLiteDatabase 。通过这个实例,我创建了一个数据库和一个表。当我查询数据库中的表时,我收到一条错误,指出 java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase

如何在不扩展 SQLiteOpenHelper 类的情况下正确打开和关闭SQlite数据库。我需要查询来自不同活动的数据取决于对数据的需求。有没有符合我需要的教程?

创建数据库

public static SQLiteDatabase phiDatebase;

public void createOrOpenDatabase(String dbNmae, Context context){
        this.dbName = dbNmae;
        try{
            uuiDatebase = context.openOrCreateDatabase(dbNmae, Context.MODE_PRIVATE, null);
            Log.i("UUI_LOG","DB CREATED");
        }
        catch(Exception e){
            e.printStackTrace();
        }

    }

创建表格,

public void createSurveyTable(String tableName, String queryString){
    try{

        uuiDatebase.execSQL("CREATE TABLE IF NOT EXISTS " + tableName + "(" + queryString + ");");
        Log.i("UUI_LOG", "TABLE CREATED");

    }
    catch (Exception e){
        e.printStackTrace();
    }

}

但是当我查询表格列表时,我收到错误。

Cursor res =  uuiDatebase.rawQuery( "SELECT name FROM sqlite_master WHERE type='table'", null );
res.moveToFirst();

以下是我对Craete DB的完整课程,创建表并获取表格列表。

public class UUIDBHelper {

    public static SQLiteDatabase uuiDatebase;
    public static String dbName = "";


    public void createOrOpenDatabase(String dbNmae, Context context){
        this.dbName = dbNmae;
        try{
            uuiDatebase = context.openOrCreateDatabase(dbNmae, Context.MODE_PRIVATE, null);
            Log.i("PHI_LOG","DB CREATED");
        }
        catch(Exception e){
            e.printStackTrace();
        }

    }

    public void createSurveyTable(String tableName, String queryString , boolean isCreate){
        try{
            if(isCreate) {
                uuiDatebase.execSQL("CREATE TABLE IF NOT EXISTS " + tableName + "(" + queryString + ");");
                Log.i("PHI_LOG", "TABLE CREATED");
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }

    }

    public Cursor getAllTables() {

        //SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  uuiDatebase.rawQuery( "SELECT name FROM sqlite_master WHERE type='table'", null );
        res.moveToFirst();

        return res;
    }

    public Cursor getSurveyNames(String tableName) {

        //SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  uuiDatebase.rawQuery( "SELECT survey_name FROM "+tableName+" WHERE id=0", null );
        res.moveToFirst();

        return res;
    }


}

这是Logcat结果

03-28 13:28:12.922: E/SQLiteDatabase(2837): **android.database.sqlite.SQLiteDatabaseLockedException: database is locked** (code 5): , while compiling: PRAGMA journal_mode
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:634)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:320)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:294)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:215)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:1142)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:1131)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:261)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at surveytool.uui.com.uuisurveytool.UUIDBHelper.createOrOpenDatabase(UUIDBHelper.java:24)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at surveytool.uui.com.uuisurveytool.ListCreatedSurvey.onResume(ListCreatedSurvey.java:54)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1241)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.app.Activity.performResume(Activity.java:6023)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2940)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2982)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1322)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.os.Handler.dispatchMessage(Handler.java:102)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.os.Looper.loop(Looper.java:135)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at android.app.ActivityThread.main(ActivityThread.java:5221)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at java.lang.reflect.Method.invoke(Native Method)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at java.lang.reflect.Method.invoke(Method.java:372)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-28 13:28:12.922: E/SQLiteDatabase(2837):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1 个答案:

答案 0 :(得分:0)

基本上,如果在不使用SQLiteOpenHelper类的情况下执行SQLITE操作,则需要手动打开和关闭数据库。

public static SQLiteDatabase dbInstance ;

dbInstance = context.openOrCreateDatabase(DB_NAME, Context.MODE_PRIVATE, null);

交易完成后关闭。

dbInstance .close();