我正在开发Android联系人列表应用程序。下面是我的Dbhandler课程。当我想将新联系人插入我的数据库并按下保存按钮时,它会给我一个错误:
Error inserting Name=f HOME=00 Phone=a Address=b android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
请帮帮我,我还附上了我的logcat。
源代码:
public class DBHandler extends SQLiteOpenHelper{
private static final int DB_Version = 1;
private static final String DB_Name = "Places";
protected static final String Places_Table = "Places";
private String DB_PATH="";
String name, Address, Website,ID;
//WebpreneurActivity Contact;
public static final String Key_ID = "ID";
public static final String Key_Name = "Name";
public static final String Key_Phone = "Phone";
public static final String Key_Address = "Address";
public static final String Key_Website = "Website";
public static final String Key_Home = "HOME";
public static final String PROJECTION[] = {
Key_ID,
Key_Name,
Key_Address,
Key_Phone,
Key_Home
};
String CREATE_PLACES_TABLE = "create table if not exists Places_Table (id integer primary key ,"+
"name VARCHAR not null, phone VARCHAR not null, address VARCAHR not null, website VARCHAR not null,Home VARCHAR not null)";
private final Context context;
private static SQLiteDatabase db;
public DBHandler(Context context) {
super(context, DB_Name, null, DB_Version);
this.context = context;
DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
Log.d("database1" ,"4");
}
@Override
public void onCreate(SQLiteDatabase db) {
try{
Log.d("DB", "DB creationnewwwww");
db.execSQL("CREATE TABLE " + Places_Table + " (" +
Key_ID + " INTEGER PRIMARY KEY NOT NULL,"+
Key_Name + " TEXT NOT NULL," +
Key_Phone + " TEXT NOT NULL," +
Key_Address + " TEXT NOT NULL," +
Key_Website + " TEXT," +
Key_Home + " TEXT)");
Log.d("DB", "DB creationnewwwwwwwwwwwwwwwwwwwwwwwwwww");
}
catch(SQLiteException e){
Log.d("DB", "DB creation excptionhhhhhhhhhhhhhhh");
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("Drop Table If Exists" + Places_Table);
onCreate(db);
}
public void open()
{Log.d("DB", "DB creation 9");
db = this.getWritableDatabase();
//onCreate(db);
Log.d("DB", "DB creation 9");
String myPath = DB_PATH + DB_Name;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if(db != null)
db.close();
super.close();
}
public void createRow(String name, String address, String Phone, String home)
{SQLiteDatabase db = this.getWritableDatabase();
ContentValues initialValues = new ContentValues();
initialValues.put(Key_Name, name);
initialValues.put(Key_Address, address);
initialValues.put(Key_Phone, Phone);
initialValues.put(Key_Home, home);
//pass the initialValues to the database to insert the row
Log.d(Key_Home, home);
db.insert(Places_Table, null, initialValues);
Log.d("Your Location4", "okna:");
}
public void deleteRow(long rowId){
db.delete(Places_Table, Key_ID+"="+rowId,null);
}
public boolean updateRow (long rowId, String name, String address, String Phone, String home){
ContentValues args = new ContentValues();
args.put(Key_Name, name);
args.put(Key_Address, address);
args.put(Key_Phone, Phone);
args.put(Key_Home, home);
return db.update(Places_Table, args, Key_ID +"="+ rowId, null)>0;
}
public Cursor fetchRow(long rowId) throws SQLException{
Cursor result = db.query( Places_Table, null,
Key_ID + "=" + rowId, null, null, null,null);
if ((result.equals(rowId)) || !result.isFirst()) {
throw new SQLException("No note matching ID: " + rowId);
}
return result;
}
public Cursor fetchAllRows(){
Log.d("Your Location4", "ok99:");
return db.query(Places_Table, PROJECTION,
null, null, null, null, null);
}
}
Here is my logcat
01-27 19:00:21.664: D/database1(15280): b4
01-27 19:00:21.674: D/database1(15280): button3
01-27 19:00:21.684: D/database1(15280): button4
01-27 19:00:21.714: D/database1(15280): button5
01-27 19:00:21.714: D/database1(15280): button5
01-27 19:00:36.519: W/KeyCharacterMap(15280): Can't open keycharmap file
01-27 19:00:36.519: W/KeyCharacterMap(15280): Error loading keycharmap file '/system/usr/keychars/ux500-ske-keypad.kcm.bin'. hw.keyboards.0.devname='ux500-ske-keypad'
01-27 19:00:36.529: W/KeyCharacterMap(15280): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
01-27 19:00:40.543: E/what(15280): result set
01-27 19:00:40.583: D/Your Location4(15280): jj:
01-27 19:00:40.583: D/HOME(15280): 6r
01-27 19:00:40.613: E/Database(15280): Error inserting Name=g HOME=6r Phone=64 Address=f
01-27 19:00:40.613: E/Database(15280): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
01-27 19:00:40.613: E/Database(15280): at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
01-27 19:00:40.613: E/Database(15280): at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:61)
01-27 19:00:40.613: E/Database(15280): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1590)
01-27 19:00:40.613: E/Database(15280): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1434)
01-27 19:00:40.613: E/Database(15280): at com.webpreneur_contactlist.DBHandler.createRow(DBHandler.java:154)
01-27 19:00:40.613: E/Database(15280): at com.webpreneur_contactlist.WebpreneurActivity.onActivityResult(WebpreneurActivity.java:145)
01-27 19:00:40.613: E/Database(15280): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
01-27 19:00:40.613: E/Database(15280): at android.app.ActivityThread.deliverResults(ActivityThread.java:2549)
01-27 19:00:40.613: E/Database(15280): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2595)
01-27 19:00:40.613: E/Database(15280): at android.app.ActivityThread.access$2000(ActivityThread.java:121)
01-27 19:00:40.613: E/Database(15280): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:973)
01-27 19:00:40.613: E/Database(15280): at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 19:00:40.613: E/Database(15280): at android.os.Looper.loop(Looper.java:138)
01-27 19:00:40.613: E/Database(15280): at android.app.ActivityThread.main(ActivityThread.java:3701)
01-27 19:00:40.613: E/Database(15280): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 19:00:40.613: E/Database(15280): at java.lang.reflect.Method.invoke(Method.java:507)
01-27 19:00:40.613: E/Database(15280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
01-27 19:00:40.613: E/Database(15280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
01-27 19:00:40.613: E/Database(15280): at dalvik.system.NativeStart.main(Native Method)
01-27 19:00:40.613: D/Your Location4(15280): okna:
01-27 19:00:40.623: D/Your Location4(15280): ok90:
01-27 19:00:40.623: D/Your Location4(15280): ok6:
01-27 19:00:40.623: D/Your Location4(15280): ok99:
01-27 19:00:40.623: D/AndroidRuntime(15280): Shutting down VM
01-27 19:00:40.623: W/dalvikvm(15280): threadid=1: thread exiting with uncaught exception (group=0x4001d578)
01-27 19:00:40.623: E/AndroidRuntime(15280): FATAL EXCEPTION: main
01-27 19:00:40.623: E/AndroidRuntime(15280): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { (has extras) }} to activity {com.webpreneur_contactlist/com.webpreneur_contactlist.WebpreneurActivity}: java.lang.IllegalStateException: database not open
01-27 19:00:40.623: E/AndroidRuntime(15280): at android.app.ActivityThread.deliverResults(ActivityThread.java:2553)
01-27 19:00:40.623: E/AndroidRuntime(15280): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2595)
01-27 19:00:40.623: E/AndroidRuntime(15280): at android.app.ActivityThread.access$2000(ActivityThread.java:121)
01-27 19:00:40.623: E/AndroidRuntime(15280): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:973)
01-27 19:00:40.623: E/AndroidRuntime(15280): at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 19:00:40.623: E/AndroidRuntime(15280): at android.os.Looper.loop(Looper.java:138)
01-27 19:00:40.623: E/AndroidRuntime(15280): at android.app.ActivityThread.main(ActivityThread.java:3701)
01-27 19:00:40.623: E/AndroidRuntime(15280): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 19:00:40.623: E/AndroidRuntime(15280): at java.lang.reflect.Method.invoke(Method.java:507)
01-27 19:00:40.623: E/AndroidRuntime(15280): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
01-27 19:00:40.623: E/AndroidRuntime(15280): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
01-27 19:00:40.623: E/AndroidRuntime(15280): at dalvik.system.NativeStart.main(Native Method)
01-27 19:00:40.623: E/AndroidRuntime(15280): Caused by: java.lang.IllegalStateException: database not open
01-27 19:00:40.623: E/AndroidRuntime(15280): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1238)
01-27 19:00:40.623: E/AndroidRuntime(15280): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1197)
01-27 19:00:40.623: E/AndroidRuntime(15280): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1279)
01-27 19:00:40.623: E/AndroidRuntime(15280): at com.webpreneur_contactlist.DBHandler.fetchAllRows(DBHandler.java:181)
01-27 19:00:40.623: E/AndroidRuntime(15280): at com.webpreneur_contactlist.WebpreneurActivity.fillData(WebpreneurActivity.java:76)
01-27 19:00:40.623: E/AndroidRuntime(15280): at com.webpreneur_contactlist.WebpreneurActivity.onActivityResult(WebpreneurActivity.java:147)
01-27 19:00:40.623: E/AndroidRuntime(15280): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
01-27 19:00:40.623: E/AndroidRuntime(15280): at android.app.ActivityThread.deliverResults(ActivityThread.java:2549)
01-27 19:00:40.623: E/AndroidRuntime(15280): ... 11 more
答案 0 :(得分:0)
看看这些例外情况:
Caused by: java.lang.IllegalStateException: database not open
和
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { (has extras) }} to activity {com.webpreneur_contactlist/com.webpreneur_contactlist.WebpreneurActivity}: java.lang.IllegalStateException: database not open
似乎您没有打开与数据库的连接。您需要先在open()
。
WebpreneurActivity
方法