Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查输入的字符串是否为';这不是唯一的字符串。Java(SQLite数据库)_Java_Android_Sqlite - Fatal编程技术网

如何检查输入的字符串是否为';这不是唯一的字符串。Java(SQLite数据库)

如何检查输入的字符串是否为';这不是唯一的字符串。Java(SQLite数据库),java,android,sqlite,Java,Android,Sqlite,嗨,我是android开发的新手,我正在尝试制作一个存储约会的应用程序。我的片段和活动都很好,但是我正在尝试解决如何检查我的sqlite数据库,我尝试输入的字符串还没有存储为唯一的字符串,非常感谢您的帮助 这是我目前为止的数据库代码 public class MyDataBase extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 4; private static final Strin

嗨,我是android开发的新手,我正在尝试制作一个存储约会的应用程序。我的片段和活动都很好,但是我正在尝试解决如何检查我的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();
    }    

}

调用此方法并检查return 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();
      }

    } 

谢谢你,好心的先生。