我尝试在Android(API 25)中创建和使用SQLite数据库。 我在清单中提供许可。我想也许数据库路径有问题。但我无法解决这个问题...
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permissio8n.READ_EXTERNAL_STORAGE"/>
有数据库开放代码
String db = "jdbc:sqlite:" + Environment.getDataDirectory().getAbsolutePath() + "/MyDatabase.db";
Class.forName("org.sqlite.JDBC");
但是有一些关于权限的错误
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: java.sql.SQLException: opening db: '/data/MyDatabase.db': Permission denied
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: at org.sqlite.core.CoreConnection.open(CoreConnection.java:203)
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: at org.sqlite.core.CoreConnection.<init>(CoreConnection.java:76)
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: at org.sqlite.jdbc3.JDBC3Connection.<init>(JDBC3Connection.java:24)
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: at org.sqlite.jdbc4.JDBC4Connection.<init>(JDBC4Connection.java:23)
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: at org.sqlite.SQLiteConnection.<init>(SQLiteConnection.java:45)
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: at org.sqlite.JDBC.createConnection(JDBC.java:114)
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: at org.sqlite.JDBC.connect(JDBC.java:88)
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:569)
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: at java.sql.DriverManager.getConnection(DriverManager.java:237)
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: at com.example.user.app.DB.openDbConnection(DB.java:33)
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: at com.example.user.app.Helpers.isTableIsEmpty(Helpers.java:24)
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: at com.example.user.app.activity.BachelorITFCourses1.onCreate(BachelorITFCourses1.java:42)
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: at android.app.Activity.performCreate(Activity.java:6679)
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
06-05 21:05:35.962 3698-3698/com.example.user.app W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
06-05 21:05:35.963 3698-3698/com.example.user.app W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
06-05 21:05:35.963 3698-3698/com.example.user.app W/System.err: at android.app.ActivityThread.-wrap12(ActivityThread.java)
06-05 21:05:35.963 3698-3698/com.example.user.app W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
06-05 21:05:35.963 3698-3698/com.example.user.app W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
06-05 21:05:35.963 3698-3698/com.example.user.app W/System.err: at android.os.Looper.loop(Looper.java:154)
06-05 21:05:35.963 3698-3698/com.example.user.app W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6119)
06-05 21:05:35.963 3698-3698/com.example.user.app W/System.err: at java.lang.reflect.Method.invoke(Native Method)
06-05 21:05:35.963 3698-3698/com.example.user.app W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
06-05 21:05:35.963 3698-3698/com.example.user.app W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
P.S。我有dtabase操作的特殊类DB。
答案 0 :(得分:0)
8允许......读错是对的吗? 你要求权限吗?我认为编写和读取外部存储是至关重要的,必须要求: https://developer.android.com/training/permissions/requesting.html
答案 1 :(得分:0)
所以你认为我需要请求我需要的权限?像这样的东西?
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}