濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > SQLite教程(十三):C語(yǔ)言編程實(shí)例代碼(1)

SQLite教程(十三):C語(yǔ)言編程實(shí)例代碼(1)

熱門標(biāo)簽:智能電銷機(jī)器人教育 中國(guó)地圖標(biāo)注省份用什么符號(hào) 奧維地圖標(biāo)注字體大小修改 孝感銷售電銷機(jī)器人廠家 無(wú)錫梁溪公司怎樣申請(qǐng)400電話 北京智能外呼系統(tǒng)供應(yīng)商家 高德地圖標(biāo)注電話怎么沒(méi)了 江西穩(wěn)定外呼系統(tǒng)供應(yīng)商 電話機(jī)器人錄音師薪資

一、獲取表的Schema信息:

    1). 動(dòng)態(tài)創(chuàng)建表。

    2). 根據(jù)sqlite3提供的API,獲取表字段的信息,如字段數(shù)量以及每個(gè)字段的類型。

    3). 刪除該表。

    見(jiàn)以下代碼及關(guān)鍵性注釋:

復(fù)制代碼 代碼如下:

#include sqlite3.h>
#include string>

using namespace std;

void doTest()
{
    sqlite3* conn = NULL;
    //1. 打開(kāi)數(shù)據(jù)庫(kù)
    int result = sqlite3_open("D:/mytest.db",conn);
    if (result != SQLITE_OK) {
        sqlite3_close(conn);
        return;
    }
    const char* createTableSQL =
        "CREATE TABLE TESTTABLE (int_col INT, float_col REAL, string_col TEXT)";
    sqlite3_stmt* stmt = NULL;
    int len = strlen(createTableSQL);
    //2. 準(zhǔn)備創(chuàng)建數(shù)據(jù)表,如果創(chuàng)建失敗,需要用sqlite3_finalize釋放sqlite3_stmt對(duì)象,以防止內(nèi)存泄露。
    if (sqlite3_prepare_v2(conn,createTableSQL,len,stmt,NULL) != SQLITE_OK) {
        if (stmt)
            sqlite3_finalize(stmt);
        sqlite3_close(conn);
        return;
    }
    //3. 通過(guò)sqlite3_step命令執(zhí)行創(chuàng)建表的語(yǔ)句。對(duì)于DDL和DML語(yǔ)句而言,sqlite3_step執(zhí)行正確的返回值
    //只有SQLITE_DONE,對(duì)于SELECT查詢而言,如果有數(shù)據(jù)返回SQLITE_ROW,當(dāng)?shù)竭_(dá)結(jié)果集末尾時(shí)則返回
    //SQLITE_DONE。
    if (sqlite3_step(stmt) != SQLITE_DONE) {
        sqlite3_finalize(stmt);
        sqlite3_close(conn);
        return;
    }
    //4. 釋放創(chuàng)建表語(yǔ)句對(duì)象的資源。
    sqlite3_finalize(stmt);
    printf("Succeed to create test table now.\n");
    //5. 構(gòu)造查詢表數(shù)據(jù)的sqlite3_stmt對(duì)象。
    const char* selectSQL = "SELECT * FROM TESTTABLE WHERE 1 = 0";
    sqlite3_stmt* stmt2 = NULL;
    if (sqlite3_prepare_v2(conn,selectSQL,strlen(selectSQL),stmt2,NULL) != SQLITE_OK) {
        if (stmt2)
            sqlite3_finalize(stmt2);
        sqlite3_close(conn);
        return;
    }
    //6. 根據(jù)select語(yǔ)句的對(duì)象,獲取結(jié)果集中的字段數(shù)量。
    int fieldCount = sqlite3_column_count(stmt2);
    printf("The column count is %d.\n",fieldCount);
    //7. 遍歷結(jié)果集中每個(gè)字段meta信息,并獲取其聲明時(shí)的類型。   
    for (int i = 0; i fieldCount; ++i) {
        //由于此時(shí)Table中并不存在數(shù)據(jù),再有就是SQLite中的數(shù)據(jù)類型本身是動(dòng)態(tài)的,所以在沒(méi)有數(shù)據(jù)時(shí)
        //無(wú)法通過(guò)sqlite3_column_type函數(shù)獲取,此時(shí)sqlite3_column_type只會(huì)返回SQLITE_NULL,
        //直到有數(shù)據(jù)時(shí)才能返回具體的類型,因此這里使用了sqlite3_column_decltype函數(shù)來(lái)獲取表聲
        //明時(shí)給出的聲明類型。
        string stype = sqlite3_column_decltype(stmt2,i);
        stype = strlwr((char*)stype.c_str());
        //下面的解析規(guī)則見(jiàn)該系列的“數(shù)據(jù)類型-->1. 決定字段親緣性的規(guī)則”部分,其鏈接如下:
        //https://www.jb51.net/article/65424.htm
        if (stype.find("int") != string::npos) {
            printf("The type of %dth column is INTEGER.\n",i);
        } else if (stype.find("char") != string::npos
            || stype.find("text") != string::npos) {
            printf("The type of %dth column is TEXT.\n",i);
        } else if (stype.find("real") != string::npos
            || stype.find("floa") != string::npos
            || stype.find("doub") != string::npos ) {
            printf("The type of %dth column is DOUBLE.\n",i);
        }
    }
    sqlite3_finalize(stmt2);
    //8. 為了方便下一次測(cè)試運(yùn)行,我們這里需要?jiǎng)h除該函數(shù)創(chuàng)建的數(shù)據(jù)表,否則在下次運(yùn)行時(shí)將無(wú)法
    //創(chuàng)建該表,因?yàn)樗呀?jīng)存在。
    const char* dropSQL = "DROP TABLE TESTTABLE";
    sqlite3_stmt* stmt3 = NULL;
    if (sqlite3_prepare_v2(conn,dropSQL,strlen(dropSQL),stmt3,NULL) != SQLITE_OK) {
        if (stmt3)
            sqlite3_finalize(stmt3);
        sqlite3_close(conn);
        return;
    }
    if (sqlite3_step(stmt3) == SQLITE_DONE) {
        printf("The test table has been dropped.\n");
    }
    sqlite3_finalize(stmt3);
    sqlite3_close(conn);
}

int main()
{
    doTest();
    return 0;
}
//輸出結(jié)果為:
//Succeed to create test table now.
//The column count is 3.
//The type of 0th column is INTEGER.
//The type of 1th column is DOUBLE.
//The type of 2th column is TEXT.
//The test table has been dropped.

二、常規(guī)數(shù)據(jù)插入:

    1). 創(chuàng)建測(cè)試數(shù)據(jù)表。
    2). 通過(guò)INSERT語(yǔ)句插入測(cè)試數(shù)據(jù)。
    3). 刪除測(cè)試表。
    見(jiàn)以下代碼及關(guān)鍵性注釋:

復(fù)制代碼 代碼如下:

#include sqlite3.h>
#include string>
#include stdio.h>

using namespace std;

void doTest()
{
    sqlite3* conn = NULL;
    //1. 打開(kāi)數(shù)據(jù)庫(kù)
    int result = sqlite3_open("D:/mytest.db",conn);
    if (result != SQLITE_OK) {
        sqlite3_close(conn);
        return;
    }
    const char* createTableSQL =
        "CREATE TABLE TESTTABLE (int_col INT, float_col REAL, string_col TEXT)";
    sqlite3_stmt* stmt = NULL;
    int len = strlen(createTableSQL);
    //2. 準(zhǔn)備創(chuàng)建數(shù)據(jù)表,如果創(chuàng)建失敗,需要用sqlite3_finalize釋放sqlite3_stmt對(duì)象,以防止內(nèi)存泄露。
    if (sqlite3_prepare_v2(conn,createTableSQL,len,stmt,NULL) != SQLITE_OK) {
        if (stmt)
            sqlite3_finalize(stmt);
        sqlite3_close(conn);
        return;
    }
    //3. 通過(guò)sqlite3_step命令執(zhí)行創(chuàng)建表的語(yǔ)句。對(duì)于DDL和DML語(yǔ)句而言,sqlite3_step執(zhí)行正確的返回值
    //只有SQLITE_DONE,對(duì)于SELECT查詢而言,如果有數(shù)據(jù)返回SQLITE_ROW,當(dāng)?shù)竭_(dá)結(jié)果集末尾時(shí)則返回
    //SQLITE_DONE。
    if (sqlite3_step(stmt) != SQLITE_DONE) {
        sqlite3_finalize(stmt);
        sqlite3_close(conn);
        return;
    }
    //4. 釋放創(chuàng)建表語(yǔ)句對(duì)象的資源。
    sqlite3_finalize(stmt);
    printf("Succeed to create test table now.\n");

    int insertCount = 10;
    //5. 構(gòu)建插入數(shù)據(jù)的sqlite3_stmt對(duì)象。
    const char* insertSQL = "INSERT INTO TESTTABLE VALUES(%d,%f,'%s')";
    const char* testString = "this is a test.";
    char sql[1024];
    sqlite3_stmt* stmt2 = NULL;
    for (int i = 0; i insertCount; ++i) {
        sprintf(sql,insertSQL,i,i * 1.0,testString);
        if (sqlite3_prepare_v2(conn,sql,strlen(sql),stmt2,NULL) != SQLITE_OK) {
            if (stmt2)
                sqlite3_finalize(stmt2);
            sqlite3_close(conn);
            return;
        }
        if (sqlite3_step(stmt2) != SQLITE_DONE) {
            sqlite3_finalize(stmt2);
            sqlite3_close(conn);
            return;
        }
        printf("Insert Succeed.\n");
    }
    sqlite3_finalize(stmt2);
    //6. 為了方便下一次測(cè)試運(yùn)行,我們這里需要?jiǎng)h除該函數(shù)創(chuàng)建的數(shù)據(jù)表,否則在下次運(yùn)行時(shí)將無(wú)法
    //創(chuàng)建該表,因?yàn)樗呀?jīng)存在。
    const char* dropSQL = "DROP TABLE TESTTABLE";
    sqlite3_stmt* stmt3 = NULL;
    if (sqlite3_prepare_v2(conn,dropSQL,strlen(dropSQL),stmt3,NULL) != SQLITE_OK) {
        if (stmt3)
            sqlite3_finalize(stmt3);
        sqlite3_close(conn);
        return;
    }
    if (sqlite3_step(stmt3) == SQLITE_DONE) {
        printf("The test table has been dropped.\n");
    }
    sqlite3_finalize(stmt3);
    sqlite3_close(conn);
}

int main()
{
    doTest();
    return 0;
}
//輸出結(jié)果如下:
//Succeed to create test table now.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//Insert Succeed.
//The test table has been dropped.


您可能感興趣的文章:
  • SQLite教程(十四):C語(yǔ)言編程實(shí)例代碼(2)
  • linux c語(yǔ)言操作數(shù)據(jù)庫(kù)(連接sqlite數(shù)據(jù)庫(kù))
  • c++獲取sqlite3數(shù)據(jù)庫(kù)表中所有字段的方法小結(jié)
  • SQLite教程(二):C/C++接口簡(jiǎn)介
  • VC++操作SQLite簡(jiǎn)單實(shí)例
  • 讓Sqlite脫離VC++ Runtime獨(dú)立運(yùn)行的方法
  • C++操作SQLite簡(jiǎn)明教程
  • C語(yǔ)言SQLite3事務(wù)和鎖的操作實(shí)例

標(biāo)簽:臨滄 那曲 荊州 阜陽(yáng) 齊齊哈爾 海北 通化 泰州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SQLite教程(十三):C語(yǔ)言編程實(shí)例代碼(1)》,本文關(guān)鍵詞  SQLite,教程,十三,語(yǔ)言編程,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SQLite教程(十三):C語(yǔ)言編程實(shí)例代碼(1)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于SQLite教程(十三):C語(yǔ)言編程實(shí)例代碼(1)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    太湖县| 开封县| 黄山市| 高安市| 尚志市| 安远县| 天镇县| 巩留县| 嵩明县| 涟水县| 拜泉县| 前郭尔| 新蔡县| 仁布县| 芜湖市| 大姚县| 天门市| 赤城县| 遂昌县| 沂南县| 雷山县| 武乡县| 西城区| 柏乡县| 旬阳县| 江油市| 济宁市| 铁岭县| 延川县| 濮阳市| 易门县| 许昌县| 杨浦区| 江华| 吉安县| 莱西市| 玉树县| 武城县| 高雄县| 桂平市| 新化县|