【问题标题】:How to activate the user authentication logic of sqlite3 in c++如何在c++中激活sqlite3的用户认证逻辑
【发布时间】: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 并尝试创建一个受密码保护的数据库接口。

这是我的代码的一些部分:

  1. 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;
        }
    }

更新:

  1. 我已将QMAKE_CXXFLAGS += -DSQLITE_USER_AUTHENTICATION -ldl -lpthread 添加到我的.pro 文件中。现在,QtCreator 检测到诸如 sqlite3_user_add 之类的函数,但是当我尝试编译我的项目时,它给出了以下错误:

对 `sqlite3_user_add' 的未定义引用

:-1: 错误:collect2.exe:错误:ld 返回 1 个退出状态

【问题讨论】:

  • 你链接-lsqlite3吗?也许您可以尝试创建一个小示例,以便我们可以重现您遇到的错误?
  • @AndreasDM 我没有将sqlite3 用作库。相反,我将sqlite3.csqlite3.h 添加到我的项目中,并将sqlite3.h 包含在我的cpp 文件中。如果我不使用身份验证,它会以这种方式工作,但在我使用与身份验证相关的功能时会崩溃。我会尝试添加代码。
  • @AndreasDM 我通过添加部分代码更新了我的帖子。请看一看。

标签: c++ sqlite gcc qt-creator mingw32


【解决方案1】:

您在上面附加的错误,这是一个编译错误。 尝试添加 mysql 标志,这有助于链接您的客户端库

eg: g++ youSqlCode.cpp -o b lmysqlcppconn

【讨论】:

  • 我没有使用mysql;我在Qtcreator 上使用sqlite。我应该在哪里添加这一行?我正在尝试在QT 上构建我的应用程序,我在上面发布的命令行命令是检查它是否工作。主要问题是QT不承认用户认证的功能。
  • 这是一个无法检测头文件的编译错误有两种方法 -> 1. 在 QT 的编译器设置中包含头文件库 2. 在 .pro 中添加上面提到的标志文件或 Qmake 文件取决于您是使用 cmake 进行项目还是 Qt 配置。例如。源 += main.cpp2 QMAKE_CXXFLAGS += -std=c++0x
  • “这是一个链接错误” 不,不是。 SqlLite 与 MySQL 无关,也与 MySQL C++ 连接器无关。不要只是随机猜测,Omesh。
  • @omeshsharma 我已将QMAKE_CXXFLAGS += -DSQLITE_USER_AUTHENTICATION -ldl -lpthread 添加到我的.pro 文件中。现在,QtCreator 检测到诸如sqlite3_user_add 之类的函数,但是当我尝试编译我的项目时,它给出了以下错误:“未定义对 `sqlite3_user_add' 的引用”。
  • @NewUser 您需要调整链接器标志以链接到 sqlite3 库。库通常安装在 /usr/lib 或 /usr/lib64 或者,您可以将 sqlite3.c 文件复制到您的项目目录并作为 g++ 命令的一部分进行编译:
猜你喜欢
  • 2020-03-04
  • 2019-06-28
  • 2018-02-08
  • 1970-01-01
  • 1970-01-01
  • 2015-03-23
  • 2013-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多