SQLiteException故障排除(无法打开数据库文件)

时间:2011-09-23 12:14:29

标签: android sqlite

这是databasehelper类。我正在努力从存储在mTable中的数据库中获取一个字符串。

public class KamaDBAdapter extends SQLiteOpenHelper
{
protected static final String TAG = "TAG";

private  Context mContext;
private SQLiteDatabase mDb;
//private DataBaseHelper mDbHelper;

private static String TABLE="mTable";
private static String DB_NAME="mdb12.db";
private static String ROW_ID="_id";
public static String ROW_QUOTES= "quote";
private String DB_PATH = "/data/data/com.android.android/databases/";

public  KamaDBAdapter(Context context) 
{
super(context,DB_NAME,null,2);
 this.mContext = context;
//mDb = new DataBaseHelper(mContext);
}
/*public KamaDBAdapter createDatabase() throws SQLException 
{
this.createDatabase();
return this;
}*/

public  void readDataBase() 
{
this.getReadableDatabase();



}
public void openDataBase() throws SQLException{

//Open the database
String myPath = DB_PATH + DB_NAME;
mDb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

}

public void close() 
{
mDb.close();
}
public  String retriveData()
{


Cursor mCursor = mDb.query(TABLE, new String[] {ROW_ID},null , null, null, null, null);
//mCursor.moveToFirst();
String mReturn = mCursor.getString(mCursor.getColumnIndex(ROW_ID));

mCursor.close();
return mReturn;


}
public boolean checkdatabase()  {

  SQLiteDatabase mCheckDataBase = null;
    try
    {
        String myPath = DB_PATH + DB_NAME;
        mCheckDataBase = SQLiteDatabase.openDatabase(myPath, null,    

 SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    }
    catch(SQLiteException mSQLiteException)
    {
        Log.e(TAG, "DatabaseNotFound " + mSQLiteException.toString());
    }

    if(mCheckDataBase != null)
    {
        mCheckDataBase.close();
    }
    return mCheckDataBase != null;
}
public void copydatabase() throws IOException {

//Open your local db as the input stream
InputStream myinput = mContext.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 byte to inputfile to 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();

}

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

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

主要活动代码是。

public class DbActivity extends Activity {
/** Called when the activity is first created. */
private static KamaDBAdapter mDbHelper=null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mDbHelper = new KamaDBAdapter(this);
    TextView tv=(TextView)findViewById(R.id.text);
   boolean b= mDbHelper.checkdatabase();
   if(b==true)
   {
    mDbHelper.readDataBase();
    try {
        mDbHelper.openDataBase();
    } catch (SQLException e) {

        e.printStackTrace();
    }
   }
    try {
        mDbHelper.copydatabase();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        throw new Error("Unable to copy database");
    }

    try {
        mDbHelper.openDataBase();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

  String s=  mDbHelper.retriveData();
    tv.setText(s);

}
}

我收到错误:

09-23 17:29:17.872: ERROR/Database(2658): sqlite3_open_v2("/data/data/com.android.android/databases/mdb12.db", &handle, 2, NULL) failed
09-23 17:29:17.882: ERROR/TAG(2658): DatabaseNotFound android.database.sqlite.SQLiteException: unable to open database file

我该如何解决?

3 个答案:

答案 0 :(得分:0)

应该是

DB_NAME="mdb12.sqlite";

而不是

DB_NAME="mdb12.db";

答案 1 :(得分:0)

尝试这一个而不是......

public void readDataBase()

{

this.getReadableDatabase();

}

改变并尝试........

public void readDataBase()

{

SQLiteDatabase db_Read = null;

    db_Read = this.getReadableDatabase(); 
        db_Read.close();

}

答案 2 :(得分:0)

应该是这样的:

DB_NAME="mdb12";

此外,除非您在android.com

工作
DB_PATH = "/data/data/com.android.android/databases/";

可能是错的。输入正确的包名(您可以从清单文件中检查)