如何在SQLITE c++中保存结构体?
How can i save a struct in SQLITE C++
我尝试在sqlite3_bind_blob中保存一个简单的结构体(vector3),但我不知道这是如何工作的。如果我调试这段代码,控制台窗口将崩溃。我怎样才能解决这个问题?
struct vector3
{
int X;
int Y;
int Z;
};
int main ()
{
sqlite3 *db = NULL;
sqlite3_stmt *res = NULL;
sqlite3_open_v2("SaveGame1.sav", &db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL);
string query = "CREATE TABLE IF NOT EXISTS TestPlayer (vector BLOB)";
sqlite3_prepare_v2 (db, query.c_str(), query.length(), &res, 0);
sqlite3_step(res);
// Try to add mij struct
vector3 MyLocation;
MyLocation.X = 100;
MyLocation.Y = 100;
MyLocation.Z = 100;
query = "INSERT INTO TestPlayer (location) VALUES (?);";
sqlite3_prepare_v2 (db, query.c_str(), query.length(), &res, 0);
sqlite3_bind_blob (res, 1, &MyLocation, sizeof(vector3), SQLITE_TRANSIENT);
sqlite3_step(res);
query = "SELECT * FROM TestPlayer;";
sqlite3_prepare_v2 (db, query.c_str(), query.length(), &res, 0);
sqlite3_step(res);
const vector3 *GetLoc = (const vector3 *) sqlite3_column_blob(res, 0);
cout << GetLoc->X << endl;
sqlite3_finalize(res);
sqlite3_close(db);
return 0;
}
我认为必须对查询操作的结果使用sqlite3_column_blob()
。尝试为刚刚插入的数据发出查询
相关文章:
- 根据用户回答声明"Players"。用户选择玩家数量。播放器是结构体
- 结构体 S { int align; } 之间的区别;(struct 关键字后的名称)和 struct { int al
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 包含 std::list 的结构体的 C++ 初始化
- 结构体和类的不同大小(),彼此具有相同的字段类型
- 如何使用结构体的向量数组?
- 如何使用结构体在C++中更改这些代码?
- 无法在 Mosquitto MQTT Broker 插件上访问结构体 mosquitto 的元素
- 我应该如何在C++中使用结构体解决输入失败的问题?
- Qsort() 比较结构体整数的总和
- 如何使用迭代器指向结构体c++的向量
- 在C++中使用链表的堆栈实现中,访问结构体headNode成员count和top会导致运行时错误
- 如何获取结构体成员的地址
- 创建结构体向量,表达式:向量下标超出范围
- boost::任何带有结构体和无符号整数
- 如何在构造函数中初始化结构体的动态数组?
- 只写结构体的某些字段
- 如何在SQLITE c++中保存结构体?
- 如何保存值从指针数组到结构体在EEPROM
- 传递c++中保存静态数组的结构体