我试图将一些数据插入到android数据库中但是它的抛出约束错误。我无法捕获此异常的原因。 这是我的数据库创建java文件。
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
}
}
这是它正在抛出的错误
05-10 00:00:46.478: E/Database(527): Error inserting owner_name=kkhgfd address=hgds workscope=jhfd vendor_id=TSSRANCY47MB08IEIDSB email_id=o@f.n 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)
从错误的上面两行可以看出,我试图在数据库中存储的参数都没有。
那么我无法配置的问题在哪里。
答案 0 :(得分:1)
您只将activation_code
字段定义为主键,这意味着activation_code
中不能有两个或多个具有相同值的记录。发生异常是因为您尝试第二次插入带有activation_code=0NA
的记录。
如果activation_code
不是密钥,那么我认为更好的方法是根据其他字段值生成此字段值。