您好我正在尝试检查我的数据库以查看是否存在特定字符串,在这种情况下是列标题,然后我想返回该行的ID,以便我可以从该行获取所有信息并将其用于某些额外验证。
我在getAppointment ID方法中尝试了这个,但是我一直得到一个游标索引错误-1。一旦我能够返回ID,我计划使用它来检索行并返回正确约会的实例。我对SQL非常陌生,任何帮助都会受到赞赏。
public class MyDataBase extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "appointments.db";
public static final String TABLE_APPOINTMENTS = "appointments";
public static final String _ID = "id";
public static final String COLUMN_DAY = "day";
public static final String COLUMN_MONTH = "month";
public static final String COLUMN_YEAR = "year";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_TIME = "time";
public static final String COLUMN_DESCRIPTION = "details";
public MyDataBase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_APPOINTMENTS
+ "(" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_DAY + " INTEGER, " + COLUMN_MONTH + " INTEGER, " + COLUMN_YEAR + " INTEGER, "
+ COLUMN_TITLE + " TEXT, " + COLUMN_TIME + " TEXT, " + COLUMN_DESCRIPTION + " TEXT" + ")";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_APPOINTMENTS);
onCreate(db);
}
public int getAppointmentID(String name){
SQLiteDatabase db = getReadableDatabase();
int i=0;
String selection = COLUMN_TITLE + " = ? ";
String[] selectionArgs = new String[]{name};
Cursor cursor = db.query(TABLE_APPOINTMENTS, null, selection, selectionArgs, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
if(cursor.moveToNext())
i = cursor.getInt(0);
}
cursor.close();
return i;
}
public void addAppointment(Appointment app){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_DAY, app.get_day());
values.put(COLUMN_MONTH, app.get_month());
values.put(COLUMN_YEAR, app.get_year());
values.put(COLUMN_TITLE, app.get_title()); // need to check that string being entered isn't already a unique entry
values.put(COLUMN_TIME, app.get_time());
values.put(COLUMN_DESCRIPTION, app.get_details());
db.insert(TABLE_APPOINTMENTS, null, values);
db.close();
}
public Appointment getAppointment(int a){
String selectQuery = "SELECT * FROM " + TABLE_APPOINTMENTS + " WHERE id
= " + Integer.toString(a);
SQLiteDatabase db = this.getWritableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
Appointment app = new Appointment();
if(c.moveToFirst()){
app.set_day(Integer.parseInt(c.getString(1)));
app.set_month(Integer.parseInt(c.getString(2)));
app.set_year(Integer.parseInt(c.getString(3)));
app.set_title(c.getString(4));
app.set_time(c.getString(5));
app.set_details(c.getString(6));
}
return app;
}
}
答案 0 :(得分:0)
修改您的getAppointmentID()
方法,如下所示:
public int getAppointmentID(String name){
SQLiteDatabase db = getReadableDatabase();
int i=0;
String[] projection = {_ID};
String selection = COLUMN_TITLE + " = ? ";
String[] selectionArgs = new String[]{name};
Cursor cursor = db.query(TABLE_APPOINTMENTS, projection, selection, selectionArgs, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
}
i = cursor.getInt(0);
cursor.close();
db.close();
return i;
}