android数据库类无法正常工作

时间:2012-03-06 22:54:26

标签: android sqlite

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper{


//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/dk.millerbean.quiz/databases/";
private static String DB_NAME = "quiz.db";

//private SQLiteDatabase myDataBase; 
private SQLiteDatabase myData; 
private final Context myContext;

/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
this.myData = getWritableDatabase();

}




/**
* Creates a empty database on the system and rewrites it with your own database.
* */
private void createDataBase(SQLiteDatabase db) throws IOException
{
    boolean dbExist = checkDataBase();
    if(dbExist)
    {
        //do nothing - database already exist
    }
    else
    {
        db.execSQL("CREATE TABLE Quiz(_id INTEGER PRIMARY KEY, quizName Text)");

    }
}

/**
 * Execute all of the SQL statements in the String[] array
 * @param db The database on which to execute the statements
 * @param sql An array of SQL statements to execute
 */
private void execMultipleSQL(SQLiteDatabase db, String[] sql){
    for( String s : sql )
        if (s.trim().length()>0)
        {
            db.execSQL(s);
        }
}


public boolean checkDataBase()
{ 
    SQLiteDatabase checkDB = null; 
    try
    { 
        String myPath = DB_PATH + DB_NAME; 
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }
    catch(SQLiteException e)
    { 
        e.getStackTrace();
    } 

    if(checkDB != null)
    { 
        checkDB.close();
    } 

    return checkDB != null ? true : false; 
}


public void openDataBase() throws SQLException
{
    //Open the database
    String myPath = DB_PATH + DB_NAME;
    myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}


@Override
public synchronized void close() 
{
    if(myData != null)
    {
        myData.close();
    }
    super.close();
}


@Override
public void onCreate(SQLiteDatabase db) 
{
    try {
        createDataBase(db);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

}

来自logcat的错误:

02-28 22:34:24.160: W/dalvikvm(18715): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
02-28 22:34:24.200: E/AndroidRuntime(18715): FATAL EXCEPTION: main
02-28 22:34:24.200: E/AndroidRuntime(18715): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{dk.millerbean.quiz/dk.millerbean.quizActivity}: java.lang.NullPointerException
02-28 22:34:24.200: E/AndroidRuntime(18715):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
02-28 22:34:24.200: E/AndroidRuntime(18715):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-28 22:34:24.200: E/AndroidRuntime(18715):    at android.app.ActivityThread.access$600(ActivityThread.java:123)

1 个答案:

答案 0 :(得分:0)

尝试使用SQLiteOpenHelper来管理表格的创建。它将确保一切都以正确的顺序发生。