如何使用tntdb类来更新Sqlite数据库中的字符串值

how to use tntdb classes to update string values in Sqlite database

本文关键字:数据库 字符串 Sqlite 更新 何使用 tntdb      更新时间:2023-10-16

嗨,我在我的c++代码中使用TNTDB类时遇到了一个问题,只有整数值在数据库中更新,而字符串值正在更新为垃圾值,我在下面给出我的代码,请帮助我解决这个问题

#include <tntdb/connection.h>
#include <tntdb/connect.h>
#include <stdio.h>
#include <string>
#include <sys/shm.h>
using namespace std;
int main()
{
    string s="create table test2(T1 int not null primary key,NAME varchar(20),STATUS    varchar(20));";
    try{
        tntdb::Connection conn;
        conn = tntdb::connect("sqlite:/home/gaian/Desktop/test.db");
        string k="abc";
        conn.execute(s);
        tntdb::Statement st = conn.prepare("insert into test2(T1,NAME,STATUS) values (:T1, :NAME,:STATUS)");
        st.set("T1",10)
            .set("NAME",k)
            .set("STATUS","bye")
            .execute();
    }
    catch(const std::exception& e){
        printf("error is %sn",e.what());
    }
    return 0;
}

我运行了你的代码,它在我的平台上运行得很好。

为了避免隐式强制转换,你可以使用setString而不是set,也就是说:

st.set("T1",10)
    .setString("NAME",k)
    .setString("STATUS","bye")
    .execute();