我在 Android 中使用 SQLite 数据库,用户必须输入名称、ip 和 url,然后将其保存在数据库中。我希望能够识别名称列中是否有双重条目,但我不知道如何对其进行编程。我认为制作专栏UNIQUE
是正确的方法......
我的 SQL 适配器:
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_ROUTERIP + "," +
KEY_PROJECTNAME + "," +
KEY_URL + "," +
KEY_CALIMERO + "," +
"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);
}
public long createProject(String ip, String name,
String url, String calimero) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ROUTERIP, ip);
initialValues.put(KEY_PROJECTNAME, name);
initialValues.put(KEY_URL, url);
initialValues.put(KEY_CALIMERO, calimero);
return mDb.insert(SQLITE_TABLE, null, initialValues);
}
我希望你能给我一些提示,我已经尝试在创建表时使用 UNIQUE 但没有成功......
更新
SQLiteAdapter 中的方法:
public long createProject(String ip, String name,
String url, String calimero) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ROUTERIP, ip);
initialValues.put(KEY_PROJECTNAME, name);
initialValues.put(KEY_URL, url);
initialValues.put(KEY_CALIMERO, calimero);
return mDb.insertOrThrow(SQLITE_TABLE, null, initialValues);
}
我活动中的电话:
//DATABASE
// Add project to Database
dbHelper = new ProjectsDBAdapter(this);
dbHelper.open();
//Add Strings to database
dbHelper.insertSomeProjects(IP, Name, URL, Calimero);
//Generate ListView from SQLite Database
displayListView();
和 logcat 输出:
01-27 00:02:56.555: E/AndroidRuntime(28526): FATAL EXCEPTION: main
01-27 00:02:56.555: E/AndroidRuntime(28526): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.bertrandt.bertrandtknx/de.bertrandt.bertrandtknx.ProjectList}: android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
01-27 00:02:56.555: E/AndroidRuntime(28526): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
01-27 00:02:56.555: E/AndroidRuntime(28526): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
01-27 00:02:56.555: E/AndroidRuntime(28526): at android.app.ActivityThread.access$600(ActivityThread.java:128)
01-27 00:02:56.555: E/AndroidRuntime(28526): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
01-27 00:02:56.555: E/AndroidRuntime(28526): at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 00:02:56.555: E/AndroidRuntime(28526): at android.os.Looper.loop(Looper.java:137)
01-27 00:02:56.555: E/AndroidRuntime(28526): at android.app.ActivityThread.main(ActivityThread.java:4514)
01-27 00:02:56.555: E/AndroidRuntime(28526): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 00:02:56.555: E/AndroidRuntime(28526): at java.lang.reflect.Method.invoke(Method.java:511)
01-27 00:02:56.555: E/AndroidRuntime(28526): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
01-27 00:02:56.555: E/AndroidRuntime(28526): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
01-27 00:02:56.555: E/AndroidRuntime(28526): at dalvik.system.NativeStart.main(Native Method)
01-27 00:02:56.555: E/AndroidRuntime(28526): Caused by: android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
01-27 00:02:56.555: E/AndroidRuntime(28526): at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)
01-27 00:02:56.555: E/AndroidRuntime(28526): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113)
01-27 00:02:56.555: E/AndroidRuntime(28526): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1839)
01-27 00:02:56.555: E/AndroidRuntime(28526): at android.database.sqlite.SQLiteDatabase.insertOrThrow(SQLiteDatabase.java:1738)
01-27 00:02:56.555: E/AndroidRuntime(28526): at SQLite.ProjectsDBAdapter.createProject(ProjectsDBAdapter.java:89)
01-27 00:02:56.555: E/AndroidRuntime(28526): at SQLite.ProjectsDBAdapter.insertSomeProjects(ProjectsDBAdapter.java:140)
01-27 00:02:56.555: E/AndroidRuntime(28526): at de.bertrandt.bertrandtknx.ProjectList.setup(ProjectList.java:120)
01-27 00:02:56.555: E/AndroidRuntime(28526): at de.bertrandt.bertrandtknx.ProjectList.onCreate(ProjectList.java:55)
01-27 00:02:56.555: E/AndroidRuntime(28526): at android.app.Activity.performCreate(Activity.java:4562)
01-27 00:02:56.555: E/AndroidRuntime(28526): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
01-27 00:02:56.555: E/AndroidRuntime(28526): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
01-27 00:02:56.555: E/AndroidRuntime(28526): ... 11 more
解决方案
//Add Strings to database
try {
dbHelper.insertSomeProjects(IP, Name, URL, Calimero);
} catch (SQLiteException exception) {
Log.d("SQLite", "Error"+exception.toString());
Toast.makeText(getApplicationContext(),
"Name is duplicated", Toast.LENGTH_SHORT).show();
exception.printStackTrace();
}