0

我四处搜索(从另外两个表插入第三个表),但没有看到这个简单问题的明确答案。下面的代码似乎可以工作,但它很笨拙,我想知道我是否工作太努力了。

任务:

INSERT id of keyword "blue" FROM table "keywords" and id of image of image FROM table "images" WHERE id = 1 as FOREIGN_KEYS into 第三个表 "images_keywords" 表 "images_keywords" 只有这两个字段,并且都定义为 FOREIGN_KEYS。

我在想:

  • 我是怎么做的效率低下(或者只是计划错了......)?
  • 我是否正确处理了 FOREIGN_KEYS?

void MainWindow::on_addKeywordBtn_clicked()
{
    // find keyword = "blue"
    QSqlQuery keywordQuery(db);
    keywordQuery.prepare("SELECT id, keyword FROM keywords WHERE keyword = ?");
    keywordQuery.addBindValue(QString("blue"));
    keywordQuery.exec();

    // find image with id = 1
    QSqlQuery imageQuery(db);
    imageQuery.prepare("SELECT id FROM images WHERE id = ?");
    imageQuery.addBindValue(1);
    imageQuery.exec();

    QSqlQuery insertQuery(db);
    while (keywordQuery.next() && imageQuery.next()) {

        insertQuery.prepare( "INSERT INTO images_keywords (image_id, keyword_id) VALUES (:image_id, :keyword_id)" );
        insertQuery.bindValue( ":image_id", imageQuery.record().value("id").toInt());
        insertQuery.bindValue( ":keyword_id", keywordQuery.record().value("id").toInt());
        bool result= insertQuery.exec();

        if (!result)  qDebug() <<  insertQuery.lastError().text();
    }
}
4

1 回答 1

0

This looks correct mostly and I'm assuming that you've already populated your keywords and images tables. I just have a couple of suggestions to improve on your process.

When doing any data interactions in my applications, I create a data access layer class, usually named DAL, for easier code re-use. If you have objects representing keywords and images, you could pass objects to each method and perform the CRUD operations on the objects themselves. Think of this as sort of a very rudimentary object relational mapper.

In the code below, you might not need GetImage if you already have the image ID. You would simply just pass along the image ID into InsertImageKeyword after retrieving the keyword ID.

int DAL::GetKeyword(QString keyword)
{
    // get ID
    QSqlQuery keywordQuery(db);
    keywordQuery.prepare("SELECT id FROM keywords WHERE keyword = ?");
    keywordQuery.addBindValue(image);
    keywordQuery.exec();
    int id = -1;
    if(keywordQuery.next())
        id = keywordQuery.record().value("id").toInt();
    return id;
}

int DAL::GetImage(QString image)
{
    // get ID
    QSqlQuery imageQuery(db);
    imageQuery.prepare("SELECT id FROM images WHERE image = ?");
    imageQuery.addBindValue(image);
    imageQuery.exec();
    int id = -1;
    if(imageQuery.next())
        id = imageQuery.record().value("id").toInt();
    return id;
}

bool DAL::InserImageKeyword(int imageId, int keywordId)
{
    QSqlQuery insertQuery(db);
    insertQuery.prepare( "INSERT INTO images_keywords (image_id, keyword_id) VALUES (:image_id, :keyword_id)" );
    insertQuery.bindValue( ":image_id", imageId);
    insertQuery.bindValue( ":keyword_id", keywordId);

    bool result= insertQuery.exec();

    if (!result)  
        qDebug() <<  insertQuery.lastError().text();

    return result;
}
于 2013-08-08T19:02:30.563 回答