0

I'm getting values from JSON url and storing to local database but when i store in to database i'm getting Insert error statement aborts at 6. Below is the JSON url values

[{"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);

Log:

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)

Insert:

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;
}
raj
  • 17
  • 9

1 Answers1

1

Your uuid key column has the wrong datatype, can't be int type.

Check this answer out related to storing UUIDs

Can we use Guid as a Primary Key in Sqlite Database

Community
  • 1
  • 1
Ben Yee
  • 1,547
  • 13
  • 19