[英]Android SQlite, how do I check if the value in database already exists?
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE_COURSES + " ("
+ KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
+ KEY_COURSE_ID + " TEXT NOT NULL, "
+ KEY_COURSE_TITLE + " TEXT NOT NULL, "
+ KEY_COURSE_CNUMBER + " TEXT NOT NULL, "
+ KEY_COURSE_SUBJECT + " TEXT NOT NULL, "
+ KEY_COURSE_DAYS + " TEXT NOT NULL, "
+ KEY_COURSE_START_TIME + " TEXT NOT NULL, "
+ KEY_COURSE_END_TIME + " TEXT NOT NULL, "
+ KEY_COURSE_PROFESSOR + " TEXT NOT NULL, "
+ KEY_COURSE_BUILDING + " TEXT NOT NULL, "
+ KEY_COURSE_ROOM_NUMBER + " TEXT NOT NULL);");
I want to check that if the course_id already exists in the table, then don't make another entry. 我想检查一下如果表中已经存在course_id,那么不要再输入一个条目。 Thanks
谢谢
To check if a field exists or not in general : 检查字段是否存在:
Cursor cur1=db.query("database_table_courses", "course_id=id_u_wanttocheck", null, null, null, null, null);
cur1.moveToLast();
int count1=cur1.getCount();
if(count1==0)
{
//course id not present
}
else
{
//course id present
}
Or if you want the course_id to be never repeated in the table, you can set that as the primary key and remove the primary key tag from row_no
. 或者,如果您希望在表中不再重复course_id ,则可以将其设置为主键并从
row_no
删除主键标记。 This way you can simply insert a new row. 这样您就可以简单地插入一个新行。 If the course_id is alreday present it willnot insert and throw an exception -
SQLException
, you can catch it and do what ever you want. 如果course_id是alreday存在,它将不会插入并抛出异常 -
SQLException
,您可以捕获它并执行您想要的任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.