值未插入数据库

时间:2014-11-15 11:32:14

标签: android database sqlite

我从JSON url获取值并存储到本地数据库但是当我存储到数据库时我得到插入错误语句在6中止。下面是JSON url值

[{"name":"Test","uuid":"B9407F30-F5F8-466E-AFF9-25556B57FE6D","major_id":"23695","minor_id":"46901","notification":"Test","type":"Image","product_image":"test.com/beacon/staging/images/product_images/visualize1.png"}]

   String CREATE_TABLE_BEACON = "CREATE TABLE " + TABLE_BEACON + "("
                + KEY_NAME + " TEXT,"+ KEY_UUID + " INTEGER PRIMARY KEY,"+ KEY_MAJOR_ID + " INTEGER,"+ KEY_MINOR_ID + " INTEGER,"+ KEY_TYPE + " TEXT)";
        db.execSQL(CREATE_TABLE_BEACON);

日志:

11-15 16:42:53.260: E/SQLiteLog(11819): (20) statement aborts at 6: [INSERT OR REPLACE  INTO beacon_details(uuid,type,minor,major,name) VALUES (?,?,?,?,?)] datatype mismatch
11-15 16:42:53.270: E/SQLiteDatabase(11819): Error inserting uuid=B9407F30-F5F8-466E-AFF9-25556B57FE6D type=Image minor=46901 major=23695 name=Test
11-15 16:42:53.270: E/SQLiteDatabase(11819): android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20)
11-15 16:42:53.270: E/SQLiteDatabase(11819):    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
11-15 16:42:53.270: E/SQLiteDatabase(11819):    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:973)
11-15 16:42:53.270: E/SQLiteDatabase(11819):    at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
11-15 16:42:53.270: E/SQLiteDatabase(11819):    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
11-15 16:42:53.270: E/SQLiteDatabase(11819):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1591)
11-15 16:42:53.270: E/SQLiteDatabase(11819):    at android.database.sqlite.SQLiteDatabase.replace(SQLiteDatabase.java:1507)
11-15 16:42:53.270: E/SQLiteDatabase(11819):    at com.estimote.examples.demos.DataBaseHandler.insertBeaconDetails(DataBaseHandler.java:86)
11-15 16:42:53.270: E/SQLiteDatabase(11819):    at com.estimote.examples.demos.NotifyDemoActivity$JsonTask.doInBackground(NotifyDemoActivity.java:141)
11-15 16:42:53.270: E/SQLiteDatabase(11819):    at com.estimote.examples.demos.NotifyDemoActivity$JsonTask.doInBackground(NotifyDemoActivity.java:1)
11-15 16:42:53.270: E/SQLiteDatabase(11819):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-15 16:42:53.270: E/SQLiteDatabase(11819):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-15 16:42:53.270: E/SQLiteDatabase(11819):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-15 16:42:53.270: E/SQLiteDatabase(11819):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-15 16:42:53.270: E/SQLiteDatabase(11819):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-15 16:42:53.270: E/SQLiteDatabase(11819):    at java.lang.Thread.run(Thread.java:841)

插入:

DatabaseHandler.java

 public long insertBeaconDetails(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);
       //  values.put(KEY_NOTIFICATION,beacondetailstore.notification);
         values.put(KEY_TYPE,beacondetailstore.type);    
       //  values.put(KEY_ACTION,beacondetailstore.action);    


         long rowId = db.replace(TABLE_BEACON, null, values);
         db.close();
         return rowId;
    }

MainActivity.java:

 beacondetailstore = new BeaconDetails();

        beacondetailstore.name = jsonRegion.getString("name");
                beacondetailstore.uuid = jsonRegion.getString("uuid");
                beacondetailstore.major = jsonRegion.getInt("major_id");
                beacondetailstore.minor = jsonRegion.getInt("minor_id");               
              //  beacondetailstore.notification=jsonRegion.getString("notification");
                beacondetailstore.type = jsonRegion.getString("type");
              //  beacondetailstore.action = jsonRegion.getString("product_image");

       db.insertBeaconDetails(beacondetailstore);

public class BeaconDetails {

      String uuid,name,notification,type,action;
      int major,minor;
}

1 个答案:

答案 0 :(得分:1)

您的uuid键列的数据类型错误,不能为int类型。

查看与存储UUID相关的答案

Can we use Guid as a Primary Key in Sqlite Database