继续获取数据库未打开错误

时间:2011-07-02 00:46:41

标签: android database sqlite

所以我很好地调整我的sqlclass.class文件并尝试使用onCreate方法,但是我的应用程序在logcat中出现以下错误并且“数据库未打开”时仍然崩溃。

这是我的主要类启动应用程序:

package com.anthony.myApp;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class appMain extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        View newData = findViewById(R.id.mainMenu_newBtn);
        newData.setOnClickListener(this);
        View continueBtn = findViewById(R.id.mainMenu_continueBtn);
        continueBtn.setOnClickListener(this);
        View exitBtn = findViewById(R.id.mainMenu_exitBtn);
        exitBtn.setOnClickListener(this);
        View btnRegister = findViewById(R.id.mainMenu_register);
        btnRegister.setOnClickListener(this);
        Log.d("MYTAG", "Loaded");

        sqlclass helper2 = new sqlclass(this);
        SQLiteDatabase db = helper2.getWritableDatabase();         

    }
    @Override
    public void onWindowFocusChanged (boolean hasFocus)
    {

    }


    @Override
    public void onClick(View v){
        Log.d("MYTAG", "Switch");
        switch(v.getId()){
        case R.id.mainMenu_newBtn:
            Log.d("MYTAG", "New Button");
            Intent in = new Intent(this, createNewBook.class);
            startActivity(in);
            break;
        case R.id.mainMenu_continueBtn:
            Intent ic= new Intent(this, continueData.class);
            startActivity(ic);
            break;
        case R.id.mainMenu_register:
            Intent register = new Intent(this, loginSettings.class);
            startActivity(register);
            break;
        case R.id.mainMenu_exitBtn:
            finish();
            break;

        }
    }
}

这就是我的sqlclass onCreate:

og.d("MYTAG", "onCreate");
        try{
            String sql = "CREATE TABLE if not exists _Data (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, _tableName TEXT NOT NULL, _isFavorites TEXT NOT NULL, _cdgDiscs TEXT NOT NULL, _binderNumber TEXT NOT NULL, _discNumber TEXT NOT NULL, _discStatus TEXT NOT NULL, _discNotes TEXT NOT NULL, _mp3g TEXT NOT NULL, _mp3Path TEXT NOT NULL);";
            db.execSQL(sql);
            String sql2 = "CREATE TABLE if not exists _artistNames (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, Initials TEXT NOT NULL, Artist TEXT NOT NULL);";
            db.execSQL(sql2);
            String sql3 = "CREATE TABLE if not exists _genres (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, genre TEXT NOT NULL);";
            db.execSQL(sql3);
            String sql4 = "CREATE TABLE if not exists _artistInitials (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, Initials TEXT NOT NULL);";
            db.execSQL(sql4);
            String sql5 = "CREATE TABLE if not exists _songInitials (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, bookName TEXT NOT NULL, initials TEXT NOT NULL);";
            db.execSQL(sql5);
            String sql6 = "CREATE TABLE if not exists _cdProperties ("+_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, tableName TEXT NOT NULL, created TEXT NOT NULL, edited TEXT NOT NULL, discStatus TEXT NOT NULL, discNotes TEXT NOT NULL, discModel TEXT NOT NULL, discName TEXT NOT NULL, binderNumber TEXT NOT NULL, binderPageNumber TEXT NOT NULL, discNumber TEXT NOT NULL, cdID TEXT NOT NULL);";
            db.execSQL(sql6);

            Log.d("MYTAG", "New data table created");
            ContentValues values = new ContentValues();
            ArrayList<String> genres = new ArrayList<String>();
            BufferedReader br = null;
             try{
                br = new BufferedReader(new InputStreamReader(contextHelper.getAssets().open("genres.csv")));
                String word;

                    while((word=br.readLine())!= null){
                        genres.add(word);
                    }


                }catch(Exception e){
                    Log.d("MYTAG", "E="+e);
                }
                finally {
                    try {
                        br.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            for(int i=0; i<genres.size(); i++){
                values.clear();
                values.put("genre", genres.get(i).toString());
                db.insertOrThrow("_genres", null, values);
            }

            Cursor cursor = db.rawQuery("SELECT genre FROM _genres ORDER BY genre COLLATE NOCASE", null);
            if(cursor!=null){
                if(cursor.moveToFirst()){
                    do{
                        Log.d("MYTAG", "Genre="+cursor.getString(0));
                    } while (cursor.moveToNext());
                }
            }


        }catch (SQLiteException e){
            Log.d("MYTAG", "newAppSetup e="+e);
        }finally{
            if(db.isOpen()){
                db.close();
            }
        }
    }

这是错误:

07-01 17:41:10.833: ERROR/AndroidRuntime(18629): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.anthony.myApp/com.anthony.myApp.appMain}: java.lang.IllegalStateException: database not open
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at android.os.Looper.loop(Looper.java:123)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at android.app.ActivityThread.main(ActivityThread.java:4627)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at java.lang.reflect.Method.invokeNative(Native Method)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at java.lang.reflect.Method.invoke(Method.java:521)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at dalvik.system.NativeStart.main(Native Method)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629): Caused by: java.lang.IllegalStateException: database not open
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:552)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:113)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at com.anthony.myApp.appMain.onCreate(appMain.java:38)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-01 17:41:10.833: ERROR/AndroidRuntime(18629):     ... 11 more

2 个答案:

答案 0 :(得分:4)

请勿在{{1​​}}区块中关闭您的数据库...

finally

答案 1 :(得分:0)

删除

 db.close();

您正在关闭所有数据库连接,并且您可能正在其他地方使用它,这就是您的应用程序崩溃了。 感谢