0

我想防止我的 SQLite 数据库中出现重复,所以我做了一列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 重复,则会引发异常,但我想检查名称。谁能找到我的错误?不能那么戏剧化,但我找不到问题...

4

2 回答 2

1

如果您第一次使用唯一的一列运行项目,然后稍后更改它,那么您的更改将不会反映。因为表已经创建并且不会使用新结构再次创建。所以如果是这种情况,请卸载应用程序并再次运行。您也可以使用sqlite浏览器测试表格

于 2012-09-28T11:32:24.643 回答
1

如果您没有更改数据库版本,您将收到此错误,因为您的所有代码都很好,但 android 数据库对象认为它是以前的数据库,因此更改数据库版本并再次检查它会在模拟器和真实设备上运行需要删除第一个应用程序,然后在 Rasel 上方检查它说

于 2014-04-04T05:45:21.647 回答