菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
329
0

C++如何对接sqlitepp

原创
05/13 14:22
阅读数 56692

sqlitepp是一个用C++封装的操作sqlite的工具

使用方法

示例(example.cpp):

#include <iostream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include "sqlite3x.h"

using namespace sqlite3x;

sqlite3_connection m_dbSessions;
int InitDB()
{
    int bResult = 0;

    do 
    {
        try {
            m_dbSessions.open("sessions.db");
            if(0 == m_dbSessions.executeint("select count(*) from sqlite_master where name='sessions';"))
            {
                m_dbSessions.executenonquery("create table sessions("
                    "sid char(32) PRIMARY KEY," //sessionId
                    "acd char(32)," 
                    "url char(32)" 
                    ");"
                );
                m_dbSessions.executenonquery("CREATE UNIQUE INDEX sessionIndex ON sessions(session ASC);");
            }
            else
            {
                sqlite3_command cmd(m_dbSessions, "delete from sessions ;");
                cmd.executenonquery();
                cmd.finalize();
            }
        }
        catch(database_error &ex)
        {
            std::cout << "Error: "<< ex.what() << std::endl;
            return -1;
        }
        catch(std::exception &ex)
        {
            return -1;
        }
        catch(... )
        {
            return -1;
        }
    } while (0);

    return bResult;
}

void Insert(std::string a, std::string b,std::string c)
{    
    std::stringstream strmSQLpre;
    strmSQLpre << "INSERT INTO sessions VALUES( ?,?,?);";
    try
    {
        sqlite3_command cmd(m_dbSessions);
        cmd.prepare(strmSQLpre.str());
        cmd.bind(1, a );
        cmd.bind(2, b );
        cmd.bind(3, c );
        cmd.executenonquery();
    }
    catch(database_error &ex)
    {
        std::cout << "Error:" << ex.what();
    }
}
void Select()
{
    std::stringstream strmSQLpre;
    strmSQLpre << " select * from sessions;";
    try
    {
        sqlite3_command cmd(m_dbSessions);
        cmd.prepare(strmSQLpre.str());
        std::cout << "Start Print:" << std::endl;
        while(cmd.read())
        {
            std::cout << cmd.getstring(0) << " | "; 
            std::cout << cmd.getstring(1) << " | ";
            std::cout << cmd.getstring(2) << " | " << std::endl;
        }
        std::cout << "End Print:" << std::endl;
    }
    catch(database_error &ex)
    {
        std::cout << "Error :" <<ex.what();
    }
}

int main()
{
    InitDB();
    Insert("11","aa","33");
    Insert("22","bb","44");
    Select();
    
    return 0;
}

编译命令:

g++ example.cpp -I /path/to/include/libsqlitepp/  /path/to/libsqlitepp.a /path/to/libsqlite3.a -lpthread -ldl -o example

运行结果:

Start Print:
11 | aa | 33 | 
22 | bb | 44 | 
End Print:

常见的报错和解决方法

1.错误如下:

sqlite3.c:24604: undefined reference to `dlerror'
sqlite3.c:24635: undefined reference to `dlclose'
sqlite3.c:24631: undefined reference to `dlsym'
sqlite3.c:24590: undefined reference to `dlopen'

解决方法:编译时添加 -ldl 选项;
2.错误如下:

sqlite3.c:14608: undefined reference to `pthread_mutex_trylock'
sqlite3.c:14485: undefined reference to `pthread_mutexattr_init'
sqlite3.c:14486: undefined reference to `pthread_mutexattr_settype'
sqlite3.c:14488: undefined reference to `pthread_mutexattr_destroy'

解决方法:编译时添加-lpthread选项;
3.错误如下:

sqlite3x_command.cpp:94: undefined reference to `sqlite3_prepare'
sqlite3x_command.cpp:101: undefined reference to `sqlite3_column_count'
sqlite3x_command.cpp:108: undefined reference to `sqlite3_prepare16'
sqlite3x_command.cpp:115: undefined reference to `sqlite3_column_count'
sqlite3x_command.cpp:134: undefined reference to `sqlite3_finalize'
sqlite3x_command.cpp:126: undefined reference to `sqlite3_reset'
sqlite3x_command.cpp:134: undefined reference to `sqlite3_finalize'
sqlite3x_command.cpp:140: undefined reference to `sqlite3_bind_null'
sqlite3x_command.cpp:149: undefined reference to `sqlite3_bind_int'

解决方法:调整引用动态库的顺序/path/to/libsqlitepp.a在前 /path/to/libsqlite3.a在后。
4.错误如下:

"error: ‘sqlite3_connection’ does not name a type"

解决方法:代码中添加

using namespace sqlite3x;

如果不加,所有sqlite的函数就模仿cout等函数那种用法std::cout方式使用。

发表评论

0/200
329 点赞
0 评论
收藏