如果值不存在,如何将数据插入SQLite数据库?

时间:2015-11-11 14:56:00

标签: java android database sqlite

我想仅在数据库为空时才将vales插入日期库。如果为空,我想插入所有db.addQuestion。如果问题已经存在,我不想插入任何内容。 这是MainActivity中的代码,我将问题插入到数据库中:

    DBHandler db = new DBHandler(this);

    // Inserting Questions
    db.addQuestion(new Question("1. Question 1", 1));
    db.addQuestion(new Question("2. Question 2", 2));
    db.addQuestion(new Question("3. Question 3", 3));
    db.addQuestion(new Question("4. Question 4", 4));
    db.addQuestion(new Question("5. Question 5", 5));

    // Reading all Questions
    List<Question> questions = db.getAllQuestions();

这是来自DBHandler的代码,我在其中创建表,添加一个新问题并获得所有问题。

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_QUESTIONS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_QUESTIONS + "("
            + KEY_QUESTION_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUESTION + " TEXT, "
            + KEY_ORDER_NO + " INTEGER " + ")";
    db.execSQL(CREATE_QUESTIONS_TABLE);
}

// Adding New Question
void addQuestion(Question question) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_QUESTION, question.getQuestion());
    values.put(KEY_ORDER_NO, question.getorderNo());

    db.insert(TABLE_QUESTIONS, null, values);
    db.close();
}

// Getting All Questions
public List<Question> getAllQuestions() {
    List<Question> questionList = new ArrayList<Question>();
    String selectQuery = "SELECT  * FROM " + TABLE_QUESTIONS;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // Looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Question question = new Question();
            question.setID(Integer.parseInt(cursor.getString(0)));
            question.setQuestion(cursor.getString(1));
            question.setorderNo(Integer.parseInt(cursor.getString(2)));
            // Adding Questions to List
            questionList.add(question);
        } while (cursor.moveToNext());
    }
    return questionList;
}

如果我有2张或更多桌子怎么办?谢谢!

1 个答案:

答案 0 :(得分:0)

使值唯一,然后在插入期间检查数据库值是否已存在。 像这样:

检查方法:

public boolean dataExists(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
        String Query = "SELECT * FROM " + TABLE_NAME + " WHERE " + KEY_QUESION_ID + "=" + id;
        Cursor cursor = db.rawQuery(Query, null);
        if (cursor.getCount() <= 0) {
            cursor.close();
            return false;
        }
        cursor.close();
        return true;
    }

并插入

void addQuestion(Question question) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_QUESTION, question.getQuestion());
    values.put(KEY_ORDER_NO, question.getorderNo());
  if (dataExists(question.getQuestionId())) {
            db.update(TABLE_QUESTION, values, KEY_QUESTION_ID + "=" + question.getQuestionId(), null);
        } else {
            db.insert(TABLE_QUESTOIN, null, values);
            db.close();
        }
}