Note that there are some explanatory texts on larger screens.

plurals
  1. PODuplication of primary key
    primarykey
    data
    text
    <p>I am designing a form containing fields and wrote the query as</p> <pre><code>db.execSQL("CREATE TABLE pdetails( phone INTEGER PRIMARY KEY ASC, fname TEXT, lname TEXT, gender TEXT, email TEXT, mainpin INTEGER)"); </code></pre> <p>when I click the submit button data is inserted in database, but the problem is data is inserted with repeated primary keys. Can you please solve my problem? my insertion code is :</p> <pre><code> long flag = 0; int id = 1; SQLiteDatabase db = helper.getWritableDatabase(); Cursor cursor = db.query("pdetails", new String[]{"count(*) phone"}, null, null, null, null, null); while(cursor.moveToNext()) { int idFromDatabase = cursor.getInt(cursor.getColumnIndex("phone")); if(idFromDatabase != 0) { id = 1 + idFromDatabase; } } ContentValues values = new ContentValues(); //values.put("ID", id); values.put("fname", fnametxt.getText().toString().trim()); values.put("lname", lnametxt.getText().toString().trim()); if(male.isChecked()) { values.put("gender","male"); } else values.put("gender", "Female"); values.put("email", emailtxt.getText().toString()); values.put("phone", Integer.parseInt(phone.getText().toString())); values.put("mainpin", mainpin.toString()); flag = db.insert("pdetails", null, values); if(flag != -1) { Toast toast = Toast.makeText(getApplicationContext(), "You have successful inserted this record into database! "+flag, Toast.LENGTH_LONG); toast.show(); db.close(); return; } else { Toast toast = Toast.makeText(getApplicationContext(), "An error occured when insert this record into database!", Toast.LENGTH_LONG); toast.show(); db.close(); return; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload