Java 在SQL数据库中搜索特定字符串并返回该字符串所在的行id

Java 在SQL数据库中搜索特定字符串并返回该字符串所在的行id,java,android,database,sqlite,Java,Android,Database,Sqlite,您好,我正在检查我的数据库,看看在本例中是否存在一个特定的字符串—列标题,然后我想返回该行的ID,这样我就可以从该行获取所有信息,并使用它进行一些额外的验证 我在getAppointment ID方法中尝试了这一点,但是我一直得到一个游标索引错误-1。一旦我能够返回ID,我计划使用它检索行并返回正确约会的实例。我对SQL非常陌生,任何帮助者都将不胜感激 public class MyDataBase extends SQLiteOpenHelper { private static final

您好,我正在检查我的数据库,看看在本例中是否存在一个特定的字符串—列标题,然后我想返回该行的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;
}    

}

修改
getAppointId()
方法,如下所示:

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;
}

请检查我的答案。谢谢,你能解释一下你所做的更改吗?我想提高我在这方面的知识。您正在将光标移动到
moveToNext()
,尽管您只有一行作为输出。您还在输出列中输入
NULL
值。您应该在完成工作后关闭
db
,否则您可能会面临内存泄漏。这是您的荣幸。