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

时间:2022-04-11 15:26:46

I want to insert vales into a date base only if the database is empty. If is empty i want to insert all the db.addQuestion. If the questions are already there, i don't want to insert anything. This is the code from the MainActivity where i insert the questions into the database:

我想仅在数据库为空时才将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();

This is the code from the DBHandler where i create table, add a new question and get all questions.

这是来自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;
}

And what if i have 2 or more tables? Thanks!

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

1 个解决方案

#1


0  

Make a value unique then check database value is already exists or not during insert . like this :

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

checking method :

检查方法:

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

and insert

并插入

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

#1


0  

Make a value unique then check database value is already exists or not during insert . like this :

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

checking method :

检查方法:

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

and insert

并插入

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