Victor
Victor

Reputation: 913

Error while inserting record in table of sqlite

I am getting data from the other table using webservices.I am tracing each value of the table so i am sure that i am getting values but i am unable to insert them into my new table.I am not able to get where i went would any one help me out..

The code i am using for this is

         private void insertData(String bbookId,String bdesc, byte[] image1,byte[] image2 ,String bid,String bname) {
            Log.i("id",bid);
            Log.i("BookId",bbookId);
            Log.i("name",bname);
            System.err.println("url1"+image1);
            System.err.println("url2"+image2);
            System.err.println("desc"+bdesc);
            Log.e("testing","LOOK @ 1214353469TRJB UJIOY ");
             SQLiteDatabase db = placeData.getWritableDatabase();
             Log.i("Bookid",bbookId);
             ContentValues values;
             values = new ContentValues();
             values.put("bbookid",bbookId);
             values.put("desc",bdesc);
             values.put("url1",image1);
             values.put("url2",image2);

             values.put("bid",bid);
             values.put("name", bname);
             Log.i("inserting","______________");
              db.insert("pagess", null, values); 
        } 

I am getting all the log values successfully and the log tag "inserting" _ is also being displayed

My database class is ::::::::

   public class PlaceData extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "page.db";
private static final String DATABASE_TABLE = "pagess";
private static final int DATABASE_VERSION = 1;
private static final String KEY_ID="bid";
private static final String KEY_BBOOKID="bbookid";
private static final String KEY_NAME="name";
private static final String KEY_URL1="url1";
private static final String KEY_URL2="url2";
private static final String KEY_DESC="desc";
 private SQLiteDatabase sqLiteDatabase;
private Context context;
public PlaceData(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
    Log.i("table","table");
    Log.i("bookid in table is ",KEY_BBOOKID);
    db.execSQL("CREATE TABLE " + DATABASE_TABLE+"("+KEY_BBOOKID+" varchar(15),"+KEY_DESC+" text(1500),"+KEY_URL1+" BLOB,"+KEY_URL2+" BLOB,"+KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT ,"+KEY_NAME+" varchar(150))");   
}   
public void createDataBase() throws IOException{
     boolean dbExist = checkDataBase(DATABASE_NAME);
     if(dbExist){
    Log.i("g","ds");         
     }else{
      CopyFiles();
     }
    }
    private void CopyFiles()
    {
     try
        {
        InputStream is = context.getAssets().open(DATABASE_NAME);
         File outfile = new File("data/data/com.books.bcukbook/databases/",DATABASE_NAME);
         outfile.getParentFile().mkdirs();
         outfile.createNewFile();

        if (is == null)
          throw new RuntimeException("stream is null");
        else
        {
             FileOutputStream out = new FileOutputStream(outfile);      
         byte buf[] = new byte[128];
             do {
              int numread = is.read(buf);
                 if (numread <= 0)
                     break;
              out.write(buf, 0, numread);
               } while (true);

             is.close();
             out.close();
        }
        }
        catch (IOException e)
        {
           throw new RuntimeException(e);
        }

    }    
    public long insert(String bbookid, String desc,String url1,String url2,String bid,String name){

          ContentValues contentValues = new ContentValues();      
          contentValues.put(KEY_BBOOKID,bbookid);
          contentValues.put(KEY_DESC,desc);
          contentValues.put(KEY_URL1,url1);
          contentValues.put(KEY_URL2,url2);
          contentValues.put(KEY_ID,bid);
          contentValues.put(KEY_NAME,name);
          return sqLiteDatabase.insert(DATABASE_TABLE, null, contentValues);
         }
    public Cursor selectQuery(String query) throws SQLException
    {
     String myPath = "data/data/com.books.bcukbook/databases/" + DATABASE_NAME;
     CursorFactory bcuk_pages = null;
     SQLiteDatabase myData = SQLiteDatabase.openDatabase(myPath, bcuk_pages, SQLiteDatabase.OPEN_READWRITE);
     Cursor mCursor =myData.rawQuery(query, null);
     mCursor.moveToFirst();    
        myData.close();
        return mCursor;
    }
private void versionUpdation(SQLiteDatabase db) {

}
public boolean checkDataBase(String db) {

    SQLiteDatabase checkDB = null;
     File dbFile = new File( "data/data/com.books.bcukbook/databases/" +  DATABASE_NAME);  
       return dbFile.exists();
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion >= newVersion)
        return;
    if (oldVersion == 1) {
        Log.d("New Version", "Datasadsggfdh can be upgraded");
    }
    Log.d("Sample Data", "onUpgrade : " + newVersion);
}
     @Override
     public synchronized void close() {
           super.close();
     }

    }

My Log shows as

  12-10 14:13:41.285: I/inserting(30596): ______________
  12-10 14:13:41.295: E/SQLiteDatabase(30596): Error inserting bbookid=1 desc=<p>Providing in-depth coverage of how to build mobile applications using the next major release of the Android SDK, this invaluable resource takes a hands-on approach to discussing Android with a series of projects, each of which introduces a new feature and highlights techniques and best practices to get the most out of Android.</p> url1=[B@42725768 bid=2 url2=[B@42636730 name=Hello World page1
  12-10 14:13:41.295: E/SQLiteDatabase(30596): android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at com.books.bcukbook.DetailsActivity$LoadAllProducts.insertData(DetailsActivity.java:688)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at com.books.bcukbook.DetailsActivity$LoadAllProducts.callInsertion(DetailsActivity.java:663)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at com.books.bcukbook.DetailsActivity$LoadAllProducts.doInBackground(DetailsActivity.java:616)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at com.books.bcukbook.DetailsActivity$LoadAllProducts.doInBackground(DetailsActivity.java:1)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
  12-10 14:13:41.295: E/SQLiteDatabase(30596):  at java.lang.Thread.run(Thread.java:856)

Upvotes: 0

Views: 4771

Answers (5)

Prasanna
Prasanna

Reputation: 236

You created table with one Primary Key. So its value must be unique and not NULL. So it works fine when you are running at first time. But Second time it is taking same value so its not meets constraints requirements.

Upvotes: 0

user370305
user370305

Reputation: 109237

Remove

values.put("bid",bid); From insert Statement values.

As bid is Auto Increment Primary Key.

And try again..

Update:

From your PlaceData Database class.

KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT 

And private static final String KEY_ID="bid";

So you don't have to insert bid Primary Key. Your Sqlite Table automatically generates (increment) it. For every New inserted Record.

Upvotes: 4

waqaslam
waqaslam

Reputation: 68177

The error itself tells you exactly whats wrong:

android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)

You don't really need to provide KEY_ID value.

Upvotes: 0

Arash GM
Arash GM

Reputation: 10395

log tells you whats wrong! your insert terminate your primary key constraint.check for inserting repeated value to primary key field

 android.database.sqlite.SQLiteConstraintException: PRIMARY KEY must be unique (code 19)

Upvotes: 0

Chirag
Chirag

Reputation: 56925

Do not try to put value of bid because it is already auto increment value.

Remove this values.put("bid",bid);

No need to insert it, it violates primary key constraint.

Upvotes: 1

Related Questions