0

SQLiteデータベースでの重複を防ぎたいので、1つの列を作成しましたUNIQUE。しかし、奇妙なことに、間違った列が選択されています。これが私のDatabseAdapterです:

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

データベースのバージョンを変更しなかった場合、このエラーが発生します。これは、すべてのコードに問題はありませんが、Android データベース オブジェクトは以前のデータベースのように考えるため、データベースのバージョンを変更してもう一度確認すると、エミュレーターと実際のデバイスで動作します。最初のアプリを削除してから、Rasel のすぐ上で確認する必要があります

于 2014-04-04T05:45:21.647 に答える
1

最初に 1 つの列を一意にしてプロジェクトを実行し、後で変更した場合、変更は反映されません。テーブルは既に作成されており、新しい構造で再度作成されることはないためです。その場合はアプリをアンインストールしてくださいsqlite ブラウザを使用してテーブルをテストすることもできます。

于 2012-09-28T11:32:24.643 に答える