使用SqliteModernCpp进行Sqlite更新查询

问题描述

我正在使用sqliteModernCpp库。我有一个数据访问对象模式,包括以下功能

void movie_data_access_object::update_movie(movie to_update)
{
    // connect to the database
    sqlite::database db(this->connection_string);

    // execute the query
    std::string query = "UPDATE movies SET title = " + to_update.get_title() + " WHERE rowid = " + std::to_string(to_update.get_id());
    db << query;
}

本质上,我想更新数据库rowid(即PK)在其参数中具有对象to_update的值(由get_id()返回)中的记录。

代码产生sql逻辑错误。是什么原因造成的?

解决方法

原来,正在创建的查询字符串中的单引号(')丢失了。该行应为:

std::string query = "UPDATE movies SET title = '" + to_update.get_title() + "' WHERE rowid = " + std::to_string(to_update.get_id());