android无法打开数据库

时间:2017-06-06 20:34:58

标签: android database

我在android中的工作数据库有问题。 android无法打开数据库。我的应用程序只有这个类和mainActivity类。 我的代码是: `

public class G  {
public static Context        context;
public static SQLiteDatabase database;
public static final String   DIR_SDCARD   = Environment.getExternalStorageDirectory().getAbsolutePath();
public static final String   DIR_DATABASE = DIR_SDCARD + "/database-test";
@Override
public void onCreate() {
    super.onCreate();
    context = this.getApplicationContext();
   // new File(DIR_DATABASE).mkdirs();
    File file=new File(DIR_DATABASE);
    file.mkdirs();
    database = SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE + "/database.sqlite", null);
    database.execSQL("CREATE  TABLE  IF NOT EXISTS person (person_name TEXT NOT NULL ," +
                " person_family TEXT NOT NULL , " +
                " person_password TEXT NOT NULL   )");
}

and this error is:

06-06 19:44:23.978 24284-24284/com.example.khatere.mydatabasetests E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.khatere.mydatabasetests, PID: 24284
                                                                                     java.lang.RuntimeException: Unable to create application com.example.khatere.mydatabasetests.G: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
                                                                                         at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5406)
                                                                                         at android.app.ActivityThread.-wrap2(ActivityThread.java)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                         at android.os.Looper.loop(Looper.java:154)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                      Caused by: 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:209)
                                                                                         at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
                                                                                         at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
                                                                                         at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
                                                                                         at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
                                                                                         at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:808)
                                                                                         at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:793)
                                                                                         at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
                                                                                         at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:711)
                                                                                         at com.example.khatere.mydatabasetests.G.onCreate(G.java:29)
                                                                                         at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024)

` 我不知道要解决这个问题。请帮帮我。

1 个答案:

答案 0 :(得分:0)

将写入权限添加到Android Manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

如果您在API级别23或更高级别上运行应用程序,则必须在运行时请求权限。

请求权限:

String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(permissions, WRITE_REQUEST_CODE);

然后处理结果:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
       case WRITE_REQUEST_CODE:
         if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
           //Granted.
           //Continue with the database process

         }
       else{
           //Denied.
         }
        break;
    }
}

有关详细信息,请访问Requesting Permissions at Run Time - Android Doc