2017-03-31 59 views
-1

嗨我是新的Android開發,我試圖做一個存儲約會的應用程序。我的碎片和活動都很好,但我正在努力解決如何檢查我的sqlite數據庫,我想輸入的字符串還沒有存儲爲一個唯一的字符串,任何幫助,非常感謝。如何檢查輸入的字符串不是唯一的字符串。 Java(SQLite數據庫)

這是我迄今爲止的數據庫代碼。

public class MyDataBase extends SQLiteOpenHelper { 

    private static final int DATABASE_VERSION = 4; 
    private static final String DATABASE_NAME = "appointments.db"; 
    public static final String TABLE_APPOINTMENTS = "appointments"; 
    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 
       + "(" + COLUMN_DAY + " INTEGER, " + COLUMN_MONTH + " INTEGER, " + COLUMN_YEAR + " INTEGER, " 
       + COLUMN_TITLE + " TEXT NOT NULL UNIQUE, " + 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 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(); 
    }  

} 

回答

0

調用此方法並檢查返回true是否意味着它存在否則不存在。

public boolean checkAppointmentExist(String name){ 
     booolean isExist = false; 

     String selection = COLUMN_TITLE + " = ? "; 
     String[] selectionArgs = new String[]{name}; 
     Cursor cursor = database.query(TABLE_APPOINTMENTS, null, selection, selectionArgs, null, null, null); 
     if (cursor != null) { 
      if (cursor.getCount() > 0) { 
       isExist = true; 
      } 
     } 

    return isExist; 

} 


public void addAppointment(Appointment app){ 

     if(!checkAppointmentExist(app.get_title())){ 
     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(); 
     } 

    } 
+0

謝謝你,先生。 – Spoingen

相關問題