我使用sqlite-db在blob字段中保存了一个类实例。 表policyRule只有一个字段是“blob”类型。 现在我从表中得到它,这是在macOS中正常运行但是ubuntu。 你能给我一些建议吗?
#include <string.h>
#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string>
using namespace std;
class SENDFILERULE
{
public:
string a;
int b;
string c;
};
void doTest()
{
sqlite3* conn = NULL;
int result = sqlite3_open("mytest.db",&conn);
if (result != SQLITE_OK)
{
sqlite3_close(conn);
return;
}
char createTableSQL[1024];
sprintf(createTableSQL,"%s","CREATE TABLE IF NOT EXISTS policyRule (rule blob)");
sqlite3_stmt* stmt = NULL;
//8.read from database
const char* selectSQL = "SELECT * FROM policyRule";
stmt = NULL;
if(sqlite3_prepare_v2(conn,selectSQL,strlen(selectSQL),&stmt,NULL) !=SQLITE_OK)
{
if (stmt)
sqlite3_finalize(stmt);
sqlite3_close(conn);
return;
}
do
{
int ret = sqlite3_step(stmt);
if (ret == SQLITE_ROW)
{
char* buf =(char*)malloc(1024);
memset(buf,0,1024);
buf = (char*)sqlite3_column_blob(stmt,0);
SENDFILERULE* pRule =reinterpret_cast<SENDFILERULE*>(buf);
printf("%s %d %s \n",pRule->a.c_str(),pRule->b,pRule->c.c_str());
}
else if(ret == SQLITE_DONE)
{
printf("select end \n");
break;
}
}while(1);
sqlite3_finalize(stmt);
}
int main()
{
doTest();
return 0;
}
当我打印pRule-> b时,没关系,但字符串类型。