这是我的错误编码我创建了一个名为'details'的表,以下是该表的数据库代码,insert方法为'insertDescription'
public void insertDescription(int rid, String name, String phoneno, String email, String subject, String desc){
ContentValues cv = new ContentValues();
cv.put("_rid", rid);
cv.put("name", name);
cv.put("phoneno", phoneno);
cv.put("email", email);
cv.put("subject", subject);
cv.put("desc", desc);
Log.d("DB", "Record is here!");
sdb.insert("details", null, cv);
}
@Override
public void onCreate(SQLiteDatabase arg0) {
arg0.execSQL("CREATE TABLE users ( _uid INTEGER PRIMARY KEY, name TEXT, mail TEXT, password TEXT, hint TEXT);");
arg0.execSQL("CREATE TABLE details (_rid INTEGER PRIMARY KEY, name TEXT, " +
"phoneno VARCHAR(10) CONSTRAINT phone CHECK(phoneno LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')," +
" email TEXT, subject TEXT, desc TEXT);");
}
使用上面的代码我将通过以下java代码将值插入数据库
String rnos = rno.getText().toString();
int rnoz = Integer.parseInt(rnos);
String names = name.getText().toString();
String mobiles = mobile.getText().toString();
String emailids = emailid.getText().toString();
String subjects = subject.getText().toString();
String descs = desc.getText().toString();
String dates = date.getText().toString();
if(rnos.isEmpty() || names.isEmpty() || mobiles.isEmpty()
|| emailids.isEmpty() || subjects.isEmpty()|| descs.isEmpty() || dates.isEmpty()){
Toast.makeText(getApplicationContext(), "All Fields are Mandatory", Toast.LENGTH_LONG).show();
}else{
mdb.openDB();
mdb.insertDescription(rnoz, names, mobiles, emailids, subjects, descs);
Toast.makeText(getApplicationContext(), "Record Saved Successfully!", Toast.LENGTH_LONG).show();
}
mdb.closeDB();
发生以下错误。我不知道我刚刚从我的朋友那里得到了这个查询约束查询的SQL查询。无论如何,表成功创建我在程序执行后看到了表结构。以下是我的错误日志
07-30 22:06:23.215: E/SQLiteDatabase(876): Error inserting desc=uuuu email=uuuu subject=uuuu phoneno=99808 name=uuuu _rid=1234
07-30 22:06:23.215: E/SQLiteDatabase(876): android.database.sqlite.SQLiteConstraintException: constraint failed (code 19)
07-30 22:06:23.215: E/SQLiteDatabase(876): at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
07-30 22:06:23.215: E/SQLiteDatabase(876): at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
07-30 22:06:23.215: E/SQLiteDatabase(876): at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
07-30 22:06:23.215: E/SQLiteDatabase(876): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
07-30 22:06:23.215: E/SQLiteDatabase(876): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
07-30 22:06:23.215: E/SQLiteDatabase(876): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
07-30 22:06:23.215: E/SQLiteDatabase(876): at in.blogspot.pcnlap.sha_college_guide.MyDataBase.insertDescription(MyDataBase.java:47)
07-30 22:06:23.215: E/SQLiteDatabase(876): at in.blogspot.pcnlap.sha_college_guide.DescriptionPage.onClick(DescriptionPage.java:51)
07-30 22:06:23.215: E/SQLiteDatabase(876): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 22:06:23.215: E/SQLiteDatabase(876): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 22:06:23.215: E/SQLiteDatabase(876): at android.view.View$1.onClick(View.java:3593)
07-30 22:06:23.215: E/SQLiteDatabase(876): at android.view.View.performClick(View.java:4091)
07-30 22:06:23.215: E/SQLiteDatabase(876): at android.view.View$PerformClick.run(View.java:17072)
07-30 22:06:23.215: E/SQLiteDatabase(876): at android.os.Handler.handleCallback(Handler.java:615)
07-30 22:06:23.215: E/SQLiteDatabase(876): at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 22:06:23.215: E/SQLiteDatabase(876): at android.os.Looper.loop(Looper.java:153)
07-30 22:06:23.215: E/SQLiteDatabase(876): at android.app.ActivityThread.main(ActivityThread.java:5086)
07-30 22:06:23.215: E/SQLiteDatabase(876): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 22:06:23.215: E/SQLiteDatabase(876): at java.lang.reflect.Method.invoke(Method.java:511)
07-30 22:06:23.215: E/SQLiteDatabase(876): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
07-30 22:06:23.215: E/SQLiteDatabase(876): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
07-30 22:06:23.215: E/SQLiteDatabase(876): at dalvik.system.NativeStart.main(Native Method)
我希望错误发生是由于我在查询中给出的约束语句。在Java'insertDescription()'的插入方法中,我试图将所有内容保存为String。如果有任何更好的查询或替代品可用,建议我给我完整的项目zip不完整(我在查询的中间位置)skydrive
答案 0 :(得分:0)
您遇到phoneno VARCHAR(10) CONSTRAINT phone CHECK(phoneno LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
以下代码
arg0.execSQL("CREATE TABLE details (_rid INTEGER PRIMARY KEY, name TEXT, " +
"phoneno VARCHAR(10) CONSTRAINT phone CHECK(phoneno LIKE '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')," +
" email TEXT, subject TEXT, desc TEXT);");
答案 1 :(得分:0)
LIKE
不支持字符类。
您收到错误的原因是您的电话号码的第一个字符不是[
,或者第二个字符不是0
,或者第三个字符不是-
等。
将LIKE
替换为GLOB
。