gpt4 book ai didi

c++ - 在sqlite数据库中存储类

转载 作者:行者123 更新时间:2023-11-28 06:11:01 30 4
gpt4 key购买 nike

我正在尝试弄清楚如何在 SQLite 数据库中为 C++ 项目存储类对象。

根据我在网上学到的知识,我需要将对象存储在 SQLite 表的 blob 字段中。您需要采取哪些步骤来执行此操作?我相信您必须序列化要插入数据库的对象。

例如,我正在尝试将程序从使用数组转换为数据库,其中每个数组[索引] 都是一个类对象。

目前正在使用:

public class Example
{
private int x;
Example(int a)
{
x=a;
}
}

main()
{
Example array[10];
for(int i=0; i<10; i++)
{
array[i] = new Example(i);
}
}

和我想要的一样:

public class Example
{
private int x;
Example(int a)
{
x=a;
}
}
main()
{
sqlite3 *db;
int rc;
char *sql;

rc = sqlite3_open("test.db", &db);
for(int i=0; i<10; i++)
{
sql = "INSERT INTO INFORMATION(ID,DATA) VALUES ('i', Example(i));"
rc = sqlite3_exec(db, sql, notused, notused, notused);
}


}

这可能吗?或者是否有类似的东西将对象存储在 sqlite 数据库中?

最佳答案

看起来您正在寻找一个 ORM(对象关系映射)库,例如Hiberlite

关于c++ - 在sqlite数据库中存储类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31275681/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com