2017-04-05 64 views
0

你好我想检查我的数据库,看看在这种情况下,存在一个特定的字符串列标题,然后我想返回该行的ID,因此我可以从该行获取所有信息并将其用于额外的验证。某些字符串,返回行ID搜索SQL数据库,在那里该字符串存在

我试图在我的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

检查我的答案,请。 – tahsinRupam

回答

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

这做了很多的感谢,你能不能解释你做,请更改?我想提高我在这方面的知识。 – Spoingen

+0

你是移动光标到'使用MoveToNext()'虽然你只有一个行作为输出。你也在输出栏中加入NULL值。你应该总是关闭'db'完成你的工作后,否则你可能面临内存泄漏。 – tahsinRupam

+0

好的谢谢你的帮助和解释。 – Spoingen