sqlite 環境搭建及demo測試

一、sqlite 優勢

sqlite不需要server,輕量級適合嵌入式開發使用,接口有C++ Java等,開發方便,

詳細優勢如下鏈接

https://blog.csdn.net/qq_34470212/article/details/52769724

二、移植

官網下載源碼

https://www.sqlite.org/download.html

編譯x86版本

./configure --prefix=/opt/sqlite_x86 
安裝編譯 
make && make install

編譯arm版本

./configure CC=arm-linux-gcc --host=arm-linux --prefix=/opt/sqlite_arm(要注意arm-linux-gcc編譯器的路徑) 
安裝編譯:make && make install

三、簡單的demo測試

添加編譯出來的庫 頭文件到工程

#include "dialog.h"
#include <QApplication>
#include <sqlite3.h>
#include <QDebug>
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;
int callback(void*,int,char**,char**);
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
   // Dialog w;
    //w.show();
    time_t timer_old;//time_t就是long int 類型
   timer_old = time(NULL);
    while(1)
    {
        sqlite3* db;
        int nResult = sqlite3_open("/home/lzc/develop/sqlite/data/test.db",&db);
        if (nResult != SQLITE_OK)
        {
            cout<<"打開數據庫失敗:"<<sqlite3_errmsg(db)<<endl;
            return 0;
        }
        else
        {
              qDebug("打開數據庫成功\n");
        }

        char* errmsg;

//第一次創建使用

//nResult = sqlite3_exec(db,"create table fuck(id integer primary key autoincrement,name varchar(100))",NULL,NULL,&errmsg);
//         if (nResult != SQLITE_OK)
//         {
//             sqlite3_close(db);
//            // cout<<errmsg;
//             sqlite3_free(errmsg);
//            return 0;
//        }


        string strSql;
        strSql+="begin;\n";
        for (int i=0;i<1;i++)
        {
            strSql+="insert into fuck values(null,'heh');\n";
        }
        strSql+="commit;";
        //cout<<strSql<<endl;

        nResult = sqlite3_exec(db,strSql.c_str(),NULL,NULL,&errmsg);

        if (nResult != SQLITE_OK)
        {
            sqlite3_close(db);
           // cout<<errmsg<<endl;
            sqlite3_free(errmsg);
            return 0;
        }

//        strSql = "select * from fuck";
//        nResult = sqlite3_exec(db,strSql.c_str(),callback,NULL,&errmsg);
//          if (nResult != SQLITE_OK)
//        {
//            sqlite3_close(db);
//           // cout<<errmsg<<endl;
//            sqlite3_free(errmsg);
//            return 0;
//        }

        sqlite3_close(db);


        time_t timer;//time_t就是long int 類型
        timer = time(NULL);


        time_t ttt=timer-timer_old;
  qDebug("5555 \n");
        qDebug("ttt = % ld \n",ttt);


     //   fprintf( stream, "%ld\n", ttt );

        timer_old = timer;

    }


     return a.exec();
}

int callback(void* ,int nCount,char** pValue,char** pName)
{
    string s;
    for(int i=0;i<nCount;i++)
    {
        s+=pName[i];
        s+=":";
        s+=pValue[i];
        s+="\n";
    }
    cout<<s<<endl;
    return 0;
}


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章