我是Android开发的新手,所以请耐心等待。我的问题是我有一个外部SQLite文件,我想通过android访问,我们通常在iPhone或Windows中做我们只是添加SQlite文件作为资源并访问它,但我不能这样做。 我按照此处给出的说明Link但是它出现错误“无法打开数据库文件”。
编辑:
09-14 23:31:59.008: INFO/Database(335): sqlite returned: error code = 14, msg = cannot open file at source line 25467
09-14 23:31:59.008: ERROR/Database(335): sqlite3_open_v2("/data/data/com.mobile.socket.server/databases/Database/DictionaryDb.sqlite", &handle, 2, NULL) failed
我的DbHelper课程:
package com.dbhandler;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
public class DbHelper extends SQLiteOpenHelper
{
//The Android's default system path of your application database.
private String DB_PATH = "";//"/data/data/com.mobile.socket.server/databases/";
private static String DB_NAME = "DictionaryDb.db";
private SQLiteStatement insertStmt;
private SQLiteDatabase myDataBase;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DbHelper(Context context)
{
super(context, DB_NAME, null, 1);
//CreateDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
myDataBase = db;
}
private void CreateDatabase()
{
try
{
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openOrCreateDatabase(myPath, null);
final String CREATE_TABLE_WORDS = "CREATE TABLE WordsTable (WordId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , Word TEXT NOT NULL )";
final String CREATE_TABLE_MEANINGS = "CREATE TABLE MeaningTable (MeaningId INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , WordId INTEGER NOT NULL , Meaning TEXT NOT NULL )";
myDataBase.execSQL(CREATE_TABLE_WORDS);
myDataBase.execSQL(CREATE_TABLE_MEANINGS);
}
catch (Exception e)
{
e.fillInStackTrace();
}
}
public boolean AddNewWord(String word)
{
CreateDatabase();
String myPath = DB_PATH + DB_NAME;
//ContentValues newRecord = new ContentValues();
boolean RetVal = false;
try
{
final String INSERT = "Insert into WordsTable(Word) Values('"+word+"')";
myDataBase = myDataBase.openDatabase(DB_NAME, null,SQLiteDatabase.OPEN_READWRITE);
insertStmt = myDataBase.compileStatement(INSERT);
// myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
//newRecord.put("Wordstable", word);
//long newWordid = myDataBase.insert("Wordstable", null, newRecord);
}
catch (SQLException e)
{
e.fillInStackTrace();
}
return RetVal;
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:1)
我能够自己解决这个问题,SqLiteHelper的OnCreate函数没有被调用所以在类构造函数中我调用this.getWritableDatabase()
这个强制“OnCreate”函数创建了我的数据库对象我能够得到我的工作完成。