2012-09-28 57 views
0

我想防止SQLite數據庫中有重複項,所以我創建了一列UNIQUE。但奇怪的是,選擇了錯誤的列。這裏是我的DatabseAdapter:SQLite與Android中的錯誤UNIQUE列

public class ProjectsDBAdapter { 

public static final String KEY_ROWID = "_id"; 
public static final String KEY_PROJECTNAME = "projectname"; 
public static final String KEY_ROUTERIP = "routerip"; 
public static final String KEY_URL = "url"; 
public static final String KEY_CALIMERO = "calimero"; 

private static final String TAG = "ProjectsDBAdapter"; 
private DatabaseHelper mDbHelper; 
private SQLiteDatabase mDb; 

private static final String DATABASE_NAME = "KNXTable"; 
private static final String SQLITE_TABLE = "Project"; 
private static final int DATABASE_VERSION = 1; 

private final Context mCtx; 

private static final String DATABASE_CREATE = 
    "CREATE TABLE if not exists " + SQLITE_TABLE + " (" + 
    KEY_ROWID + " integer PRIMARY KEY autoincrement," + 
    KEY_PROJECTNAME + " TEXT UNIQUE," + 
    KEY_ROUTERIP + " TEXT NOT NULL," + 
    KEY_URL + " TEXT NOT NULL," + 
    KEY_CALIMERO + " TEXT NOT NULL," + 
    " UNIQUE (" + KEY_PROJECTNAME +"));"; 

private static class DatabaseHelper extends SQLiteOpenHelper { 

    DatabaseHelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
    //invoked when the database is created, 
    //this is where we can create tables and columns to them, create views or triggers. 
    Log.w(TAG, DATABASE_CREATE); 
    db.execSQL(DATABASE_CREATE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    //invoked when we make a modification to the database such as altering, 
    //dropping , creating new tables. 
    Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
    + newVersion + ", which will destroy all old data"); 
    db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE); 
    onCreate(db); 
    } 
} 

public ProjectsDBAdapter(Context ctx) { 
    this.mCtx = ctx; 
} 

public ProjectsDBAdapter open() throws SQLException { 
    mDbHelper = new DatabaseHelper(mCtx); 
    mDb = mDbHelper.getWritableDatabase(); 
    return this; 
} 

public void close() { 
    if (mDbHelper != null) { 
    mDbHelper.close(); 
    } 
} 

public long createProject(String ip, String name, 
    String url, String calimero) { 

    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_PROJECTNAME, name); 
    initialValues.put(KEY_ROUTERIP, ip); 
    initialValues.put(KEY_URL, url); 
    initialValues.put(KEY_CALIMERO, calimero); 

    return mDb.insertOrThrow(SQLITE_TABLE, null, initialValues); 
} 

public boolean deleteAllProjects() { 

    int doneDelete = 0; 
    doneDelete = mDb.delete(SQLITE_TABLE, null , null); 
    Log.w(TAG, Integer.toString(doneDelete)); 
    return doneDelete > 0; 
} 

//---deletes a particular title--- 
public boolean deleteTitle(String name) 
{ 
    return mDb.delete(SQLITE_TABLE, KEY_PROJECTNAME + "='" + name+"'", null) > 0; 
} 

public Cursor fetchProjectsByName(String inputText) throws SQLException { 
    Log.w(TAG, inputText); 
    Cursor mCursor = null; 
    if (inputText == null || inputText.length() == 0) { 
    mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID, 
    KEY_PROJECTNAME, KEY_ROUTERIP, KEY_URL, KEY_CALIMERO}, 
    null, null, null, null, null); 
    } 
    else { 
    mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID, 
    KEY_PROJECTNAME, KEY_ROUTERIP, KEY_URL, KEY_CALIMERO}, 
    KEY_PROJECTNAME + " like '%" + inputText + "%'", null, 
    null, null, null, null); 
    } 
    if (mCursor != null) { 
    mCursor.moveToFirst(); 
    } 
    return mCursor; 
} 

public Cursor fetchAllProjects() { 

    Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID, 
    KEY_PROJECTNAME, KEY_ROUTERIP, KEY_URL, KEY_CALIMERO}, 
    null, null, null, null,KEY_PROJECTNAME +" DESC"); 

    if (mCursor != null) { 
    mCursor.moveToFirst(); 
    } 
    return mCursor; 
} 

}

,並使用此代碼我填的是數據庫:

 Name = project_name.getText().toString(); 
     IP = router_ip.getText().toString(); 
     URL = url.getText().toString(); 
     dbHelper = new ProjectsDBAdapter(this); 
     dbHelper.open(); 
     //TODO Name statt IP auf Duplikat überprüfen 
     try { 
      dbHelper.createProject(IP, Name, URL, "Edit"); 
     } catch (SQLiteException exception) { 
      Log.d("SQLite", "Error"+exception.toString()); 
      exception.printStackTrace(); 
      duplicate_flag = 1; 
     } 

logcat的輸出:

09-28 13:32:15.925: I/URL(22204): URL http://www.mokkapps.de/wp-admin/calimero.xml ist gültig! 
09-28 13:32:15.925: I/Name(22204): Projektname ist Test 
09-28 13:32:15.925: I/IP(22204): IP ist 1 
09-28 13:32:16.000: W/AndroidParseXMLActivity(22204): Start 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): java.io.FileNotFoundException: /mnt/sdcard/Test.xml: open failed: ENOENT (No such file or directory) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at libcore.io.IoBridge.open(IoBridge.java:406) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at java.io.FileInputStream.<init>(FileInputStream.java:78) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at de.bertrandt.bertrandtknx.CreateProject.parseXML(CreateProject.java:340) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at de.bertrandt.bertrandtknx.CreateProject.save_project(CreateProject.java:291) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at de.bertrandt.bertrandtknx.CreateProject.onOptionsItemSelected(CreateProject.java:108) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at android.app.Activity.onMenuItemSelected(Activity.java:2564) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:984) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:613) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:121) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at android.view.View.performClick(View.java:3644) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at android.view.View$PerformClick.run(View.java:14313) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at android.os.Handler.handleCallback(Handler.java:605) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at android.os.Handler.dispatchMessage(Handler.java:92) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at android.os.Looper.loop(Looper.java:137) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at android.app.ActivityThread.main(ActivityThread.java:4517) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at java.lang.reflect.Method.invokeNative(Native Method) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at java.lang.reflect.Method.invoke(Method.java:511) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at dalvik.system.NativeStart.main(Native Method) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at libcore.io.Posix.open(Native Method) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): at libcore.io.IoBridge.open(IoBridge.java:390) 
09-28 13:32:16.020: W/AndroidParseXMLActivity(22204): ... 22 more 
09-28 13:32:16.060: E/SKIA(22204): FimgApiStretch:stretch failed 
09-28 13:32:22.590: I/URL(22204): URL http://www.mokkapps.de/wp-admin/calimero.xml ist gültig! 
09-28 13:32:22.590: I/Name(22204): Projektname ist Test2 
09-28 13:32:22.590: I/IP(22204): IP ist 1 
09-28 13:32:22.635: D/SQLite(22204): Errorandroid.database.sqlite.SQLiteConstraintException: error code 19: constraint failed 
09-28 13:32:22.635: W/System.err(22204): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed 
09-28 13:32:22.640: W/System.err(22204): at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method) 
09-28 13:32:22.640: W/System.err(22204): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113) 
09-28 13:32:22.640: W/System.err(22204): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1838) 
09-28 13:32:22.640: W/System.err(22204): at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1737) 
09-28 13:32:22.640: W/System.err(22204): at SQLite.ProjectsDBAdapter.createProject(ProjectsDBAdapter.java:88) 
09-28 13:32:22.640: W/System.err(22204): at de.bertrandt.bertrandtknx.CreateProject.save_project(CreateProject.java:283) 
09-28 13:32:22.640: W/System.err(22204): at de.bertrandt.bertrandtknx.CreateProject.onOptionsItemSelected(CreateProject.java:108) 
09-28 13:32:22.645: W/System.err(22204): at android.app.Activity.onMenuItemSelected(Activity.java:2564) 
09-28 13:32:22.645: W/System.err(22204): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:984) 
09-28 13:32:22.645: W/System.err(22204): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735) 
09-28 13:32:22.645: W/System.err(22204): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149) 
09-28 13:32:22.645: W/System.err(22204): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874) 
09-28 13:32:22.645: W/System.err(22204): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:613) 
09-28 13:32:22.645: W/System.err(22204): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:121) 
09-28 13:32:22.645: W/System.err(22204): at android.view.View.performClick(View.java:3644) 
09-28 13:32:22.645: W/System.err(22204): at android.view.View$PerformClick.run(View.java:14313) 
09-28 13:32:22.645: W/System.err(22204): at android.os.Handler.handleCallback(Handler.java:605) 
09-28 13:32:22.645: W/System.err(22204): at android.os.Handler.dispatchMessage(Handler.java:92) 
09-28 13:32:22.645: W/System.err(22204): at android.os.Looper.loop(Looper.java:137) 
09-28 13:32:22.645: W/System.err(22204): at android.app.ActivityThread.main(ActivityThread.java:4517) 
09-28 13:32:22.645: W/System.err(22204): at java.lang.reflect.Method.invokeNative(Native Method) 
09-28 13:32:22.645: W/System.err(22204): at java.lang.reflect.Method.invoke(Method.java:511) 
09-28 13:32:22.645: W/System.err(22204): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993) 
09-28 13:32:22.645: W/System.err(22204): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760) 
09-28 13:32:22.645: W/System.err(22204): at dalvik.system.NativeStart.main(Native Method) 

此刻拋出異常如果routerip重複,但我想檢查名稱。 任何人都可以找到我的錯誤?不能是戲劇性的,但我找不到問題...

+0

請在這裏添加您的完整logcat。 – Sajmon

+0

添加logcat輸出到我的主帖 –

回答

1

如果你第一次運行項目與一列獨特,然後改變它,那麼你的改變將不會反映。因爲表已經創建,而不是將重新創建新的結構。所以,如果是這種情況卸載應用程序並再次運行它。你也可以使用sqlite瀏覽器來測試表

+0

哇真的很成功,這是一個簡單的解決方案!謝謝!!!!! –

+0

不客氣 – Rasel

1

如果你沒有更改數據庫版本,你會得到這個錯誤,因爲你的所有代碼都很好,但android數據庫對象認爲它是以前的數據庫,所以更改數據庫版本,並再次檢查它會在模擬器和真實設備上工作,你需要刪除第一個應用程序,然後檢查它上面的Rasel說