Reputation: 43
I am trying to insert some data into android database but its throwing constraint error.I am not able to catch the cause of this exception. this is my database creation java file.
package Database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class VendorTable extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "ghartrends";
// Contacts table name
private static final String TABLE_NAME = "fixingvendor";
// Contacts Table Columns names
private static final String FIRM_NAME = "firm_name";
private static final String OWNER_NAME = "owner_name";
private static final String CONTACT = "contact";
private static final String EMAIL_ID = "email_id";
private static final String ADDRESS = "address";
private static final String WORKSCOPE = "workscope";
private static final String VENDOR_ID = "vendor_id";
private static final String ACTIVATION = "activation_code";
public VendorTable(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_FIXINGVENDOR_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ FIRM_NAME + " TEXT," + OWNER_NAME + " TEXT,"
+ CONTACT + " TEXT,"+ EMAIL_ID +" TEXT,"+ ADDRESS +" TEXT,"
+WORKSCOPE +" TEXT,"+ VENDOR_ID +" TEXT,"+ ACTIVATION +" TEXT PRIMARY KEY"+ ")";
db.execSQL(CREATE_FIXINGVENDOR_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(db);
}
public String GetActivationCode(){
String activationcode;
String selectQuery = "SELECT activation_code FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
do{
activationcode=cursor.getString(8);
} while(cursor.moveToNext());
return activationcode;
}
public void addVendorInfo(String firmname,String ownername,String contact,String emailid,
String address,String workscope,String vendorid, String activationcode) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FIRM_NAME, firmname);
values.put(OWNER_NAME, ownername);
values.put(CONTACT, contact);
values.put(EMAIL_ID, emailid);
values.put(ADDRESS, address);
values.put(WORKSCOPE, workscope);
values.put(VENDOR_ID, vendorid);
values.put(ACTIVATION, activationcode);
// Inserting Row
db.insert(TABLE_NAME, null, values);
db.close(); // Closing database connection
}
}
this is the error it is throwing
05-10 00:00:46.478: E/Database(527): Error inserting owner_name=kkhgfd address=hgds workscope=jhfd vendor_id=TSSRANCY47MB08IEIDSB [email protected] contact=9877654345 firm_name=hjgfds activation_code=0NA
05-10 00:00:46.478: E/Database(527): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
05-10 00:00:46.478: E/Database(527): at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
05-10 00:00:46.478: E/Database(527): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
05-10 00:00:46.478: E/Database(527): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1582)
05-10 00:00:46.478: E/Database(527): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
05-10 00:00:46.478: E/Database(527): at Database.VendorTable.addVendorInfo(VendorTable.java:81)
05-10 00:00:46.478: E/Database(527): at com.ghartrends.SignUp$1.onClick(SignUp.java:88)
05-10 00:00:46.478: E/Database(527): at android.view.View.performClick(View.java:2485)
05-10 00:00:46.478: E/Database(527): at android.view.View$PerformClick.run(View.java:9080)
05-10 00:00:46.478: E/Database(527): at android.os.Handler.handleCallback(Handler.java:587)
05-10 00:00:46.478: E/Database(527): at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 00:00:46.478: E/Database(527): at android.os.Looper.loop(Looper.java:123)
05-10 00:00:46.478: E/Database(527): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-10 00:00:46.478: E/Database(527): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 00:00:46.478: E/Database(527): at java.lang.reflect.Method.invoke(Method.java:507)
05-10 00:00:46.478: E/Database(527): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-10 00:00:46.478: E/Database(527): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-10 00:00:46.478: E/Database(527): at dalvik.system.NativeStart.main(Native Method)
As from the above two lines of the error you can notice no parameter which I am trying to store in database is null.
So where is the problem I am not able to configure.
Upvotes: 0
Views: 223
Reputation: 168
You define only activation_code
field as a primary key that means that there can't be two or more records with the same value in activation_code
. Exception occurs because you're trying to insert a record with activation_code=0NA
for the second time.
If activation_code
is not something like secret key, then I think the better way is to generate this field value based on other fields values.
Upvotes: 1