我在android中使用sqlite数据库,用户必须输入名称、ip和url,然后保存在数据库中。我想知道名称列中是否有两个条目,但我不知道如何编程。我认为列UNIQUE
是正确的方法…
我的SqlAdapter:
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);
}
我希望你能给我一些提示,我已经尝试使用独特时,创建表,但没有成功…
更新
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();
}
最佳答案
可以将列创建为主键。例如:
"CREATE TABLE mytable ("
"field1 text,"
"field2 text,"
"field3 integer,"
"PRIMARY KEY (field1)"
");"
现在,当您尝试在此列(field1)中作为重复值插入时,您的代码将抛出
SQLException
。你需要抓住SQLException
并采取适当的行动。如果没有太多行要处理,则应考虑填充包含所有名称的
ArrayList
,并使用myArrayList.contains(myNameVariable)
检查名称是否已在前面处理过。然后,您将能够避免实际执行sql语句来查看它是否抛出异常。关于android - 在Android的SQLite数据库中查找重复的条目,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12603175/