如何在SQLITE C中保存结构

前端之家收集整理的这篇文章主要介绍了如何在SQLITE C中保存结构前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我尝试在sqlite3_bind_blob中保存一个simpel结构(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,0);

    sqlite3_bind_blob (res,1,&MyLocation,sizeof(vector3),sqlITE_TRANSIENT);
    sqlite3_step(res);

    query = "SELECT * FROM TestPlayer;";
    sqlite3_prepare_v2 (db,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().尝试为刚刚插入的数据发出查询

猜你在找的Sqlite相关文章