在VC6.0中使用C++訪問sqlite資料庫)

來源:互聯網
上載者:User

在sqlite.org上下載得到Windows版本的
sqlite,它是以sqlitedll.zip檔案提供的,其中有sqlite3.def和
sqlite3.dll檔案,當然可以直接通過LoadLibrary等WIN32API來操作dll,尋找其中包含的函數,並使用這些函數,但是一般都
不這麼做,原因很簡單:這樣太麻煩,所以一般先使用LIB命令產生用於連結的lib,然後把sqlite標頭檔sqlite3.h包含進程式中,
這樣直接調用
sqlite的API就方便多了.當然sqlite3.h檔案得從sqlite原始碼(以sqlite-source-3_3_4.zip檔案提供)中搞
到.

使用VC++的LIB命令有以下步驟:
(1)設定VC98中LIB.exe所在的路徑:
D:/MyDoc/db/capi>set path=%path%;"D:/Program Files/Microsoft Visual
Studio/VC98/Bin"
(2)產生SQLite的lib檔案:
D:/MyDoc/db/capi>LIB /DEF:SQLITE3.DEF /MACHINE:IX86

Microsoft (R) Library Manager Version 6.00.8168 Copyright (C)
Microsoft Corp 1992-1998. All rights reserved. Creating library
SQLITE.lib and object SQLITE.exp
這樣就成功地建立了在WIN32程式中訪問sqlite所需要的庫,可以用於連結WIN32程式.
    到此所有使用sqlite的準備工作已告罄.現在在MSVC6中建立一個Win32 Console
Application工程,把sqlite.dll,sqlite.h和sqlite.lib檔案複製到工程檔案夾中,把sqlite.h檔案加入到項
目中,然後在Project Setting的Link中的物件程式庫模組中增加sqlite.lib檔案.
然後修改加入如下代碼即可:

// 使用ISO C++來訪問SQLite

#include <IOSTREAM>

#include <STRING>

#include <SSTREAM>

#include "sqlite3.h"

using namespace std;

sqlite3* pDB;

int createTable()
{
char* errMsg;
std::string dropTab="drop table test_tab;";
string strSQL= "create table test_tab (f1 int, f2 long);";
int res = sqlite3_exec(pDB,dropTab.c_str(),0,0, &errMsg);
if (res != SQLITE_OK)
{
   std::cout << "執行SQL 出錯." << errMsg << std::endl;
   return -1;
}
res = sqlite3_exec(pDB,strSQL.c_str(),0,0, &errMsg);

if (res != SQLITE_OK)
{
   std::cout << "執行建立table的SQL 出錯." << errMsg <<
std::endl;
   return -1;
}
else
{
   std::cout << "建立table的SQL成功執行."<< std::endl;
}

return 0;
}

int insert1()
{
char* errMsg;

int res = sqlite3_exec(pDB,"begin transaction;",0,0, &errMsg);

for (int i= 1; i < 10; ++i)
{
   std::stringstream strsql;
   strsql << "insert into test_tab values(";
   strsql << i << ","<< (i+10) << ");";
   std::string str = strsql.str();
   res = sqlite3_exec(pDB,str.c_str(),0,0, &errMsg);
   if (res != SQLITE_OK)
   {
    std::cout << "執行SQL 出錯." << errMsg << std::endl;
    return -1;
   }
}

res = sqlite3_exec(pDB,"commit transaction;",0,0, &errMsg);

std::cout << "SQL成功執行."<< std::endl;

return 0;
}

static int callback(void *NotUsed, int argc, char **argv, char
**azColName){
int i;
for(i=0; i   std::cout<< azColName[i] << " = "<<
(argv[i] ? argv[i] : "NULL")<< ", " ;
}
   std::cout<< "/n";
return 0;
}

int select1()
{
char* errMsg;
string strSQL= "select * from test_tab;";

int res = sqlite3_exec(pDB,strSQL.c_str(),callback,0, &errMsg);

if (res != SQLITE_OK)
{
   std::cout << "執行SQL 出錯." << errMsg << std::endl;
   return -1;
}
else
{
   std::cout << "SQL成功執行."<< std::endl;
}

return 0;
}

int main(int argc, char* argv[])
{
if (argc < 2)
{
   std::cout << "請輸入命令列參數:sqlite資料庫名." << std::endl;
   return 0;
}

int res = sqlite3_open(argv[1], &pDB);

if( res ){
   std::cout << "Can't open database: "<<
sqlite3_errmsg(pDB);
   sqlite3_close(pDB);
   return -1;
}
res = createTable();
if (res != 0)
{
   return 0;
}
res = insert1();
if (res != 0)
{
   return 0;
}
select1();

return 0;
}

   
在MSVC6中使用sqlite確實很簡單,上面的程式很容易懂,建立一個資料庫,然後插入一條記錄.並且省略了錯誤處理.SQLite不愧是資料存放區的

"瑞士軍刀".不像使用某些資料庫,要配置ODBC,還要把一大堆的dll一起打包到最終的使用者程式中去.還得使用depends之類的工具看要打包哪
些.dll.

 

原文地址:http://hi.baidu.com/lossless1009/blog/item/4715800e3c76f0e236d122ae.html

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.