我想創建一個管理表。從應用程序的輸入並將其添加到表。我的管理員類用於獲取和設置值。 我嘗試了很多,但無法確定問題。即使我沒有離開任何字段爲空,爲什麼我會得到SQLlite約束錯誤(代碼= 19)?
這是從我的數據庫Helper類
@Override
public void onCreate(SQLiteDatabase db) {
String query_create_administrator="create table "+TBL_ADMINISTRATOR+" ("+ADMIN_ID+" INTEGER PRIMARY KEY, "+ADMIN_NAME+
" TEXT, "+ADMIN_MAIL+" TEXT, "+ADMIN_PASSWORD+" TEXT, "+ADMIN_CONTACT+" TEXT, "+ADMIN_ADDRESS+" TEXT, "+
ADMIN_ROLE+" TEXT, "+STATUS+" TEXT)";
String query_create_student="create table "+TBL_STUDENT+" ("+STUDENT_ID+" INTEGER PRIMARY KEY, "
+STUDENT_PID+" TEXT, "+STUDENT_NAME+" TEXT, "+STUDENT_MAIL+" TEXT, "+STUDENT_PASSWORD+" TEXT, "
+STUDENT_CONTACT+" TEXT, "+STUDENT_ADDRESS+" TEXT, "+STUDENT_COURSE+" TEXT, "+STUDENT_YEAR+" TEXT, "
+STUDENT_BRANCH+" TEXT)";
db.execSQL(query_create_administrator);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long registerAdmin(Administrator obj){
SQLiteDatabase db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(ADMIN_ID,obj.getId());
cv.put(ADMIN_NAME,obj.getName());
cv.put(ADMIN_MAIL,obj.getMail());
cv.put(ADMIN_PASSWORD,obj.getPassword());
cv.put(ADMIN_CONTACT,obj.getContact());
cv.put(ADMIN_ADDRESS,obj.getAddress());
cv.put(ADMIN_ROLE,obj.getRole());
cv.put(STATUS,obj.getStatus());
db.insert(TBL_ADMINISTRATOR,null,cv);
long done=3;
return done;
}
這是從活動類中的方法
public void register(View v){
long check=0;
obj=new Administrator();
SchoolDBHandler db=new SchoolDBHandler(this);
String n,m,p,c,a;
n=name.getText().toString();
m=mail.getText().toString();
p=pass.getText().toString();
c=contact.getText().toString();
a=address.getText().toString();
if(n.equalsIgnoreCase("")||m.equalsIgnoreCase("")||p.equalsIgnoreCase("")||c.equalsIgnoreCase("")||a.equalsIgnoreCase(""))
{
Toast.makeText(getApplicationContext(),"All fields are mandatory",Toast.LENGTH_SHORT).show();
}
else {
obj.setName(n);
obj.setMail(m);
obj.setPassword(p);
obj.setContact(c);
obj.setAddress(a);
obj.setStatus("Active");
if(role_select2!=null)obj.setRole(role_select2);
check=db.registerAdmin(obj);
if(check>0) {
resetEditTextxs();
Toast.makeText(getApplicationContext(),"Successfully Created",Toast.LENGTH_SHORT).show();
}
else Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();
}
}
這是錯誤日誌:
E /數據庫:插入狀態錯誤=活動地址= AAA電子郵件= AAA名稱= AAA角色=父級聯繫人= 1211 eid = 0通過= AAA andr oid.database.sqlite.SQLiteConstraintException:錯誤代碼19:約束失敗 at android.database.sqlite.SQLiteStatement.native_execute(Native Method) at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61) at android (SQLiteDatabase.java:1426) at com.thebitshoes.classproject.dao.SchoolDBHandler.registerAdmin(SchoolDBHandler.java(SQLiteDatabase.java:1482) :191) at com.thebitshoes.classproject.AdminSignUp.register(AdminSignUp.java:109) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick( View.java:2485) 在android.view.View $ PerformClick.run(View.java:9080) 在android.os.Handler.handleCallback(Handler.java:587) 在android.os.Handler.dispatchMessage( Handler.java:92) at android.os.Looper.loop(Looper.java:130) at android.app.ActivityThread.main(ActivityThread.java:3683) at java.lang.reflect.Method.invokeNative(本地方法) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) at com.android.internal.os。 ZygoteInit.main(ZygoteInit.java:597) 在dalvik.system.NativeStart.main(本機方法)
什麼是'eid = 0'? –
我認爲你可能已經有一個條目,其中eid = 0,並且你試圖用0添加另一個條目。 – MikeT
ADMIN_ID =「eid」; – ThePercept