Android数据库值未更新

时间:2014-12-16 05:44:00

标签: android json database sqlite android-asynctask

我在JSON服务器URL中有值,并使用JSON解析器获取这些值并存储到本地设备数据库并显示到移动设备屏幕。当我第一次运行应用程序时,它从JSON服务器URL获取所有详细信息并存储到设备数据库并显示到移动屏幕但是当我更新服务器中的任何内容并再次运行应用程序时它不会更新本地的值数据库和数据库没有打开写入,它给出了LogCat错误

android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database


private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        checkDB = SQLiteDatabase.openDatabase("/data/data/com.example.bconspot/databases/BeaconDetails", null, SQLiteDatabase.OPEN_READONLY);
        db.updateBeaconDetails(beacondetailstore);
        checkDB.close();
    } catch (SQLiteException e) {
        // database doesn't exist yet.
         db.insertBeaconDetails(beacondetailstore);
    }
    return checkDB != null ? true : false;
}

Logcat错误:

12-15 17:23:17.555: E/SQLiteLog(17270): (14) cannot open file at line 32516 of [00bb9c9ce4]
12-15 17:23:17.555: E/SQLiteLog(17270): (14) os_unix.c:32516: (2) open(/data/data/com.example.beacon/databases/BeaconDetails) - 
12-15 17:23:17.560: E/SQLiteDatabase(17270): Failed to open database '/data/data/com.example.beacon/databases/BeaconDetails'.
12-15 17:23:17.560: E/SQLiteDatabase(17270): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:339)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:236)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:515)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:207)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:891)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:859)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at com.example.beacon.MainActivity$JsonTask.doInBackground(MainActivity.java:226)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at com.example.beacon.MainActivity$JsonTask.doInBackground(MainActivity.java:1)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-15 17:23:17.560: E/SQLiteDatabase(17270):    at java.lang.Thread.run(Thread.java:841)

更新

public class DataBaseHandler extends SQLiteOpenHelper {

     public DataBaseHandler(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            context = this.context;
        }


    Context context ;

        // All Static variables
        // Database Version
        private static final int DATABASE_VERSION = 1;

        // Database Name
        private static final String DATABASE_NAME = "BeaconDetails";        

        // Contacts table name
        //table 1
        private static final String TABLE_BEACON = "beacon_details";

public long updateBeaconDetails(BeaconDetails beacondetailstore) 
    {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_NAME,beacondetailstore.name);
        values.put(KEY_UUID,beacondetailstore.uuid);
        values.put(KEY_MAJOR_ID,beacondetailstore.major);
        values.put(KEY_MINOR_ID,beacondetailstore.minor);


         long rowId=db.update(TABLE_BEACON, values,null,null);

         db.close();
         return rowId;
    }

1 个答案:

答案 0 :(得分:0)

您正在以只读模式打开数据库 要在写入模式下打开它,请尝试使用OPEN_READWRITE。

尝试更改以下行:OPEN_READONLY到OPEN_READWRITE

checkDB = SQLiteDatabase.openDatabase("/data/data/com.example.bconspot/databases/BeaconDetails", null, SQLiteDatabase.OPEN_READWRITE)
checkDB.updateBeaconDetails(beacondetailstore);
checkDB.close();