gpt4 book ai didi

c++ - SQLite 中的段错误

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

我的 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,段错误。

进一步反汇编语句show error at a assembly instruction

调用 0x80500b0

知道代码有什么问题吗?

最佳答案

段错误的原因

sqlite3 documentation for sqlite3_column_text 说:

If any of these routines are called [...] after sqlite3_step() has returned something other than SQLITE_ROW, the results are undefined."

您没有检查 sqlite3_step 的返回值,所以您的查询似乎返回了某种错误,然后是 sqlite3_column_text正在返回一个无效的指针。

调试SQL错误

根据 the documentation for prepare :

The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are recommended for all new programs. The two older interfaces [including sqlite3_prepare, which you call] are retained for backwards compatibility, but their use is discouraged.

[...]

When an error occurs, sqlite3_step() will return one of the detailed error codes or extended error codes. The legacy behavior was that sqlite3_step() would only return a generic SQLITE_ERROR result code and the application would have to make a second call to sqlite3_reset() in order to find the underlying cause of the problem. With the "v2" prepare interfaces, the underlying reason for the error is returned immediately.

因此,如果您切换到较新的界面,它应该会提供更多信息,而不是通用的 SQLITE_ERROR .

您也可以尝试使用 sqlite3命令行程序,它会直接告诉你错误是什么。示例 session :

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) .

关于c++ - SQLite 中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14006402/

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