Android Studio 打开数据库报错
Android初学者...困扰了好久的错误 T.T,求助大佬帮忙
因为一直在报错,把代码最简化了尝试还是不可以,如下:
DBHelper.java
public class DBHelper extends SQLiteOpenHelper{
private static final String DB_NAME = "scenic";
private static final int DB_VERSION = 1;
private static final String DB_PATH = "/data/";
private SQLiteDatabase myDataBase;
DBHelper(Context context){
super(context, DB_NAME, null, DB_VERSION);
}
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
File file = new File(DB_PATH);
Log.e("", "文件夹存在");
if(!file.exists()){
Log.e("", "文件夹不存在,新建一个");
file.mkdirs();
}
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public void onCreate(SQLiteDatabase db) {
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
}
}
使用的.java代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DBHelper dbHelper = new DBHelper(showinfo.this);
try{
dbHelper.openDataBase();
}catch(Exception e){
System.out.println(e.toString());
}
}
结果:文件目录存在,openDataBase报错:
05-11 05:38:28.089 4743-4743/? E/SQLiteLog: (14) cannot open file at line 35803 of [605907e73a]
05-11 05:38:28.089 4743-4743/? E/SQLiteLog: (14) os_unix.c:35803: (2) open(/data/scenic) -
05-11 05:38:28.093 4743-4743/? E/SQLiteDatabase: Failed to open database '/data/scenic'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:210)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:194)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:493)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:200)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:192)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:864)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:849)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:750)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:699)
at com.example.hp.mptest.DBHelper.openDataBase(DBHelper.java:49)
at com.example.hp.mptest.showinfo.onCreate(showinfo.java:28)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)