Sqlite数据库路径不起作用

时间:2013-07-24 10:09:28

标签: android sqlite

我正在尝试在我的应用程序中使用sqlite数据库。这是运行时的问题

07-26 18:03:10.374: E/AndroidRuntime(1347): FATAL EXCEPTION: main
07-26 18:03:10.374: E/AndroidRuntime(1347): android.database.sqlite.SQLiteException: no such table: QUESTIONS (code 1): , while compiling: SELECT * FROM QUESTIONS
07-26 18:03:10.374: E/AndroidRuntime(1347):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:37)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at com.db.DBAdapter.getQuestionSet(DBAdapter.java:143)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at com.example.test_question.MainActivity.getQuestionSetFromDb(MainActivity.java:45)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at com.example.test_question.MainActivity.onClick(MainActivity.java:75)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at android.view.View.performClick(View.java:4084)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at android.view.View$PerformClick.run(View.java:16966)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at android.os.Handler.handleCallback(Handler.java:615)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at android.os.Looper.loop(Looper.java:137)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at android.app.ActivityThread.main(ActivityThread.java:4745)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at java.lang.reflect.Method.invokeNative(Native Method)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at java.lang.reflect.Method.invoke(Method.java:511)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-26 18:03:10.374: E/AndroidRuntime(1347):     at dalvik.system.NativeStart.main(Native Method)

这是代码

public class DBAdapter extends SQLiteOpenHelper {

protected static final String TAG = "DataAdapter";

private static String DB_NAME = "mydb.sqlite";
private SQLiteDatabase myDatabase;
private static Context myContext;
private static String DB_PATH ="/data/data/com/databases/";



public DBAdapter(Context context) {
    super(context, DB_NAME, null, 1);
    // TODO Auto-generated constructor stub
    this.myContext = context;
}

/**
 * Creates a empty database on the system and rewrites it with your own
 * database.
 * */
public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();
    if (!dbExist) {
        // By calling this method and empty database will be created into
        // the default system path
        // of your application so we are gonna be able to overwrite that
        // database with our database.
        this.getReadableDatabase();

        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error(e.toString());
        }
    }
}

/**
 * Check if the database already exist to avoid re-copying the file each
 * time you open the application.
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
        // database does't exist yet.
    }
    if (checkDB != null) {
        checkDB.close();
    }

    return checkDB != null ? true : false;
}




/**
 * Copies your database from your local assets-folder to the just created
 * empty database in the system folder, from where it can be accessed and
 * handled. This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException {

    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    // transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

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

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

public List<Question> getQuestionSet(int numQ) {
    List<Question> questionSet = new ArrayList<Question>();
    Cursor c = myDatabase.rawQuery("Select * from questions",null);
    while (c.moveToNext()) {
        Question q = new Question();
        q.setSolution(c.getString(0));
        q.setOption3(c.getString(1));
        q.setOption2(c.getString(2));
        q.setOption1(c.getString(3));
        q.setAnswer(c.getString(4));
        q.setQuestion(c.getString(5));
        questionSet.add(q);
    }
    return questionSet;
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
     Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        onCreate(db);
}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我认为你永远不会调用createDatabase()方法。不应该在你的构造函数中:

public DBAdapter(Context context) {
    super(context, DB_NAME, null, 1);
    // TODO Auto-generated constructor stub
    this.myContext = context;

    createDataBase();
}

我认为您还需要问题评论中提到的正确路径。