专家,
以下是我的代码,
问题描述:db.execSQL语句发生错误,表示null db object。
我试着检查出了什么问题,并且应用了2个toast,发现“test”没有显示,我理解这意味着checkSelfPermission判断为false,所以我已经获得了许可。但是如果我直接编写db = SQLiteDatabase.openOrCreateDatabase(),它将会出现“未知错误(代码14):无法打开数据库”。
我真的不知道,请提前帮助,谢谢。
ActivityCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED
代码:
public class AbcActivity extends Activity {
SQLiteDatabase db;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 111222:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
db = SQLiteDatabase.openOrCreateDatabase(Global.dataFolder + File.separator + "xxxxx", null);
else {
System.exit(0);
}
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_param);
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE))
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
111222);
else
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
111222);
}
Toast.makeText(getApplicationContext(), "test2", Toast.LENGTH_SHORT).show();
//db = SQLiteDatabase.openOrCreateDatabase(Global.dataFolder + File.separator + "xxxxx", null);
db.execSQL("create table if not exists " + "xxxxx" + "(" +
//<editor-fold desc="columns">
"DtdKink REAL," +
"TttBin REAL" +
//</editor-fold>
")");
}
}
答案 0 :(得分:1)
我认为它与权限无关,您应该使用MariaDB [dev]> SELECT postcode, ( 3959 * acos( cos( radians( 53.1852582 ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians(-3.0198408999999997) ) + sin( radians(53.1852582) ) * sin( radians( latitude ) ) ) ) AS distance
-> FROM uk_postcodes
-> ORDER BY distance ASC
-> LIMIT 1;
+----------+---------------------+
| postcode | distance |
+----------+---------------------+
| CH5 3PF | 0.13510896180231324 |
+----------+---------------------+
1 row in set (3.10 sec)
类来使用SQLite。
示例:
SQLiteOpenHelper
来自:
的信息https://developer.android.com/training/basics/data-storage/databases.html
编辑:
在API 23中,当您尝试在外部存储中创建文件夹时,openOrCreateDatabase()不会创建该文件夹,请在调用此方法之前尝试执行该文件夹
检查此解决方案:
public class FeedReaderDbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "FeedReader.db";
public FeedReaderDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}