【发布时间】:2020-08-29 13:57:28
【问题描述】:
我正在尝试将 sqlite3 上的用户身份验证用于我的 c++ 项目。我已按照here 中解释的步骤进行操作。我在sqlite3.h 的末尾添加了sqlite3userauth.h,在sqlite3.c 的末尾添加了userauth.c,并在我的代码中包含了#include "sqlite3.h"。但是现在我的代码无法识别任何新功能,例如sqlite3_user_add。另外,我尝试使用以下命令gcc -o sqlite3Exe shell.c sqlite3.c -DSQLITE_USER_AUTHENTICATION -ldl -lpthread 编译shell.c,但出现此错误:
sqlite3.c:230543:11:致命错误:sqliteInt.h:没有这样的文件或 目录 # 包括“sqliteInt.h” 编译终止。
我试图评论该行,但随后又出现了另一个错误:
sqlite3.c:230730:3:错误:参数太少而无法运行 'sqlite3ExpirePreparedStatements'
sqlite3ExpirePreparedStatements(db);
该行指的是:
sqlite3ExpirePreparedStatements(db);
我没有对主要代码进行任何更改,并按照官网给出的提示进行操作,但似乎不起作用。你能指导我完成这个吗?我正在使用 QT 并尝试创建一个受密码保护的数据库接口。
这是我的代码的一些部分:
-
project.pro(我以这种方式添加了
sqlite3和编译器标志):来源 +=
sqlite/sqlite3.c
...标题 +=
sqlite/sqlite3.h
...QMAKE_CXXFLAGS += -DSQLITE_USER_AUTHENTICATION -ldl -lpthread
这是我的sqlite3.h 文件的一部分(它显示了我在哪里添加了sqlite3userauth.h 的代码):
...
#ifdef __cplusplus
} /* end of the 'extern "C"' block */
#endif
#endif /* _FTS5_H */
/******** End of fts5.h *********/
/*
** 2014-09-08
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
...
这是我的sqlite3.c 文件的一部分(这显示了我在哪里添加了userauth.c 的代码):
...
#if __LINE__!=230511
#undef SQLITE_SOURCE_ID
#define SQLITE_SOURCE_ID "2020-08-14 13:23:32 fca8dc8b578f215a969cd899336378966156154710873e68b3d9ac5881b0alt2"
#endif
/* Return the source-id for this library */
SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
/************************** End of sqlite3.c ******************************/
/*
** 2014-09-08
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
**
** This file contains the bulk of the implementation of the
** user-authentication extension feature. Some parts of the user-
** authentication code are contained within the SQLite core (in the
** src/ subdirectory of the main source code tree) but those parts
** that could reasonable be separated out are moved into this file.
**
...
这是我的dbmanager.cpp 文件,我在其中使用sqlite3:
extern "C"{
#include "sqlite/sqlite3.h"
}
#include<iostream>
using namespace std;
DBmanager::DBmanager(string dbAdd)
{
if(exists(dbAdd)){
cout<<dbAdd<<" file already exists"<<endl;
}else{
cout<<dbAdd<<" file doesn't exist and will be created for the first time"<<endl;
}
this->dbAdd=dbAdd;
int rc;
rc = sqlite3_open(this->dbAdd.c_str(), &this->db);
sqlite3_user_add(db,"Admin","Admin",2,1); // this is the line that throws the exceptions that I have mentioned below.
if( rc ) {
cout<<"Can't open database: "<< sqlite3_errmsg(this->db)<<endl;
return;
} else {
cout<<"Opened database successfully"<<endl;
}
}
更新:
- 我已将
QMAKE_CXXFLAGS += -DSQLITE_USER_AUTHENTICATION -ldl -lpthread添加到我的.pro文件中。现在,QtCreator 检测到诸如sqlite3_user_add之类的函数,但是当我尝试编译我的项目时,它给出了以下错误:
对 `sqlite3_user_add' 的未定义引用
:-1: 错误:collect2.exe:错误:ld 返回 1 个退出状态
【问题讨论】:
-
你链接
-lsqlite3吗?也许您可以尝试创建一个小示例,以便我们可以重现您遇到的错误? -
@AndreasDM 我没有将
sqlite3用作库。相反,我将sqlite3.c和sqlite3.h添加到我的项目中,并将sqlite3.h包含在我的cpp 文件中。如果我不使用身份验证,它会以这种方式工作,但在我使用与身份验证相关的功能时会崩溃。我会尝试添加代码。 -
@AndreasDM 我通过添加部分代码更新了我的帖子。请看一看。
标签: c++ sqlite gcc qt-creator mingw32