sqlite學習筆記7:C語言中使用sqlite之開啟資料庫,sqlite學習筆記

來源:互聯網
上載者:User

sqlite學習筆記7:C語言中使用sqlite之開啟資料庫,sqlite學習筆記

資料庫的基本內容前面都已經說得差不多了,接下看看怎樣在C語言中使用sqlite。

一 介面

sqlite3_open(const char *filename, sqlite3 **ppDb)

開啟資料庫,如果資料庫不存在則建立一個資料庫,並開啟


sqlite3_close(sqlite3*)

關閉資料庫,如果關閉之前還存在沒有執行完的語句,將會返回SQLITE_BUSY


二 執行個體

1 目錄結構

Projects{

main.c// 代碼所在檔案

       sqlite{// 官網下載下來的sqlite壓縮包解壓之後的檔案目錄

       shell.c// 本檔案在項目中實際上是用不上的,這個檔案是用來產生sqlite命令工具的,具體可以參考:sqlite學習筆記1

sqlite3.c

sqlite3.h

sqlite3ext.h

}

}


2 原始碼

// main.c#include <stdio.h>#include <stdlib.h>#include "sqlite/sqlite3.h"#define DB_NAME "hanfeng.db"int main(){    sqlite3* db = NULL ;    char* msg = NULL ;    int ret = 0 ;        ret = sqlite3_open(DB_NAME, &db);    if (ret){        fprintf(stderr, "error open datebase:%s\n.", DB_NAME) ;        exit(0) ;    }    else{        fprintf(stdout, "successfully open datebase.\n") ;    }    sqlite3_close(db) ;    return 0;}


為了今後擴充方便,現將代碼修改如下:

#include <stdio.h>#include <stdlib.h>#include "sqlite/sqlite3.h"#define DB_NANE "sqlite/test.db"sqlite3 *db = NULL;char* sql = NULL;char *zErrMsg = NULL;int ret = 0;typedef enum{    false,    true} bool;static int callback(void *NotUsed, int argc, char **argv, char **azColName){    int i = 0;    for(i=0; i < argc; i++){        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");    }    printf("\n");        return 0;}bool connectDB(){    ret = sqlite3_open(DB_NANE, &db);        if( ret != SQLITE_OK){        fprintf(stderr, "Error open database: %s\n", sqlite3_errmsg(db));        sqlite3_free(zErrMsg);                return false;    }        fprintf(stdout, "Successfully opened database\n");    return true;}bool closeDB(){    int ret = 0;    ret = sqlite3_close(db);    if ( ret == SQLITE_BUSY ){        return false;    }        return true;}int main(int argc, char* argv[]){    connectDB();    closeDB();        return 0;}



3 編譯運行

運行有兩種方式,基於前面的筆記,我們並沒有配置sqlite的環境,僅僅是下載解壓得到列一個檔案夾sqlite,因此,需要用如下命令:

gcc -o main main.c ./sqlite/sqlite3.c -lpthread -ldl

如果下載配置安裝了sqlite,則需要將上面的標頭檔包含改為:

#include <sqlite3.h>

然後執行命令:

gcc -o main main.c  -lsqlite3


命令執行完成之後會產生一個叫main的可執行檔,輸入:

./main

既可以看到結果。


#在編譯時間使用g++會報錯: error: invalid conversion from ‘const void*’ to ‘const char*’

g++貌似對類型轉換要求更為嚴格,不支援這樣的轉換。

路過的大俠,知道怎樣用g++編譯的,請指教......


用C語言做個sqlite資料庫

). 開啟VC建立一個“Win32 Dynamic-Link Library”工程,命名為:sqlite32). 在接下來的對話方塊中選擇"An empty DLL project",點 FINISH->OK3). 將源碼中所有的 *.c *.h *.def 複製到工程檔案夾下4). 在工程的Source File中添加你下載到的SQLite源檔案中所有*.c檔案,注意這裡不要添加shell.c和tclsqlite.c這兩個檔案。5). 將 SQLite 源檔案中的 sqlite3.def 檔案添加到在工程的Source File中6). 在Header File中添加你下載到的SQLite源檔案中所有*.h檔案,7). 開始編譯,Build(F7)一下也許到這裡會遇到一個錯誤:e:\zieckey\sqlite\sqlite3\sqlite3ext.h(22) : fatal error C1083: Cannot open include file: 'sqlite3.h': No such file or directory經檢查發現,源碼中包含sqlite3.h都是以 #include <sqlite3.h> 方式包含的,這就是說編譯器在系統預設路徑中搜尋,這樣當然搜尋不到 sqlite3.h 這個標頭檔啦,這時可以改為 #include "sqlite3.h" ,讓編譯器在工程路徑中搜尋,但是如果還有其他地方也是以 #include <sqlite3.h> 方式包含的,那麼改源碼就顯得有點麻煩,好了,我們可以這樣,在功能表列依次選擇:Tools->Options...->Directeries在下面的Directeries選項中輸入你的 sqlite3.h 的路徑,這裡也就是你的工程目錄.添加好後,我們在編譯一下就好了,最後我們在工程目錄的 Debug 目錄產生了下面兩個重要檔案:動態連結程式庫檔案 sqlite3.dll 和引入庫檔案 sqlite3.lib二. 使用動態連結程式庫下面我們來編寫個程式來測試下我們的動態連結程式庫.在VC下建立一個空的"Win32 Console Application" Win32控制台程式,工程命名為:TestSqliteOnWindows再建立一個 test.cpp 的C++語言來源程式,原始碼如下:// name: test.cpp// This prog is used to test C/C++ API for sqlite3 .It is very simple,ha !// Author : zieckey// data : 2006/11/28#include <stdio.h>#include <stdlib.h>#include "sqlite3.h" #define _DEBUG_int main( void ){sqlite3 *db=NULL;char *zErrMsg = 0;int rc;rc = sqlite3_open("zieckey.db", &db); //開啟指定的資料庫檔案,如果不存在將建立一個同名的資料庫檔案if( rc ){fprintf(stderr, &q......餘下全文>>
 
SQLITE資料庫C語言API 想使當資料庫不存在時 sqlite3_open 不建立資料庫

改用sqlite3_open_v2函數開啟
int sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags,const char *zVfs);
前兩個參數與sqllite3_open一樣,flags設定為SQLITE_OPEN_READWRITE,如果資料庫不存在就不建立,只返回一個error
參數zVfs允許應用程式命名一個虛擬檔案系統(Virtual File System)模組,用來與資料庫連接。VFS作為SQlite library和底層儲存系統(如某個檔案系統)之間的一個抽象層,通常客戶應用程式可以簡單的給該參數傳遞一個NULL指標,以使用預設的VFS模組。
 

相關文章

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.