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

标签 c++ tntnet

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

#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 %s\n",e.what());
    }
    return 0;
}

最佳答案

我运行了您的代码,它在我的平台上运行良好。

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

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

关于c++ - 如何使用 tntdb 类更新 Sqlite 数据库中的字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23757257/

相关文章:

c++ - 在 Windows 中签署结构并在 Linux 中读取它

c++ - 如何进行反向转换

c++ - 我可以将堆大小设置为 0 吗?

c++ - 指向函数返回函数指针的指针

c++ - 有没有人对 TNTWEB Web 服务器中的应用程序的 C++ 内存管理有任何想法?

c++ - 如何在给定第一个索引的情况下打印二维 map 中的所有元素