我的DataBase.cpp文件中的代码:
#include "DataBase.h"
#include <sqlite3.h>
#include <string.h>
#include <wx/msgdlg.h>
bool CanClose(void)
{
sqlite3 *Sqlite;
sqlite3_stmt *sqlstmt;
char *result;
if(sqlite3_open("SysConfig",&Sqlite)==SQLITE_OK)
{
sqlite3_prepare(Sqlite,"SELECT config_value FROM configuration WHERE config_id = 1;",-1,&sqlstmt,NULL);
sqlite3_step(sqlstmt);
result = (char*)sqlite3_column_text(sqlstmt,0);
sqlite3_close(Sqlite);
if(strcmp(result,"YES")==1) //Error Here
return true;
else
return false;
}
else
{
wxMessageBox(_("Cannot Find System File!"),_("Error!"));
sqlite3_close(Sqlite);
return false;
}
}
我的节目表现突然.. 当我尝试对其进行调试时,上面指出的行(第19行)给出了一些错误:
程序收到信号SIGSEGV,分段错误。
语句的进一步解密显示汇编指令的错误
call 0x80500b0
知道代码有什么问题吗?
答案 0 :(得分:4)
sqlite3 documentation for sqlite3_column_text
说:
如果在
sqlite3_step()
返回SQLITE_ROW
之外的其他内容后调用了这些例程中的任何一个,则结果未定义。“
您没有检查sqlite3_step
的返回值,因此您的查询似乎返回某种错误,然后sqlite3_column_text
返回无效指针。
根据the documentation for prepare
:
建议所有新程序使用
sqlite3_prepare_v2()
和sqlite3_prepare16_v2()
接口。保留两个较旧的接口[包括sqlite3_prepare
,您调用它们)以实现向后兼容,但不鼓励使用它们。[...]
发生错误时,sqlite3_step()将返回详细错误代码或扩展错误代码之一。遗留行为是sqlite3_step()只返回一个通用的SQLITE_ERROR结果代码,应用程序必须再次调用sqlite3_reset()才能找到问题的根本原因。使用“v2”prepare接口,会立即返回错误的根本原因。
因此,如果您切换到较新的界面,它应该提供更多信息,而不是通用的SQLITE_ERROR
。
您还可以尝试使用sqlite3
命令行程序,该程序将直接告诉您错误是什么。示例会话:
user@host:/path$ sqlite3 test.sqlite
sqlite> create table example ( id numeric primary key );
sqlite> select bogus from example;
Error: no such column: bogus
顺便说一句,对于标准C ++,#include <cstring>
使用#include <string.h>
,bool CanClose()
使用bool CanClose(void)
。