zoukankan      html  css  js  c++  java
  • C++如何对接sqlitepp

    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方式使用。

  • 相关阅读:
    Web crawler study(1)
    SNMP
    Locked the resolv.conf via command chattr
    HTML 父窗口打开子窗口,并从子窗口返回值
    混合语言学习(一)
    Auto Scale TextView Text to Fit within Bounds
    Android SeekBar size
    Android设置全局字体
    PopupMenu使用,PopupMenu背景的设置
    Android-屏幕适配全攻略
  • 原文地址:https://www.cnblogs.com/bugutian/p/13971022.html
Copyright © 2011-2022 走看看