国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > SQLite > 正文

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

2020-01-25 19:26:18
字體:
供稿:網(wǎng)友
這篇文章主要介紹了SQLite教程(十三):C語言編程實例代碼(1),本文講解了獲取表的Schema信息、動態(tài)創(chuàng)建表、刪除該表、常規(guī)數(shù)據(jù)插入、創(chuàng)建測試數(shù)據(jù)表、刪除測試表等內(nèi)容,需要的朋友可以參考下
 

一、獲取表的Schema信息:

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

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

    3). 刪除該表。

    見以下代碼及關鍵性注釋:

復制代碼代碼如下:

#include <sqlite3.h>
#include <string>

 

using namespace std;

void doTest()
{
    sqlite3* conn = NULL;
    //1. 打開數(shù)據(jù)庫
    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. 準備創(chuàng)建數(shù)據(jù)表,如果創(chuàng)建失敗,需要用sqlite3_finalize釋放sqlite3_stmt對象,以防止內(nèi)存泄露。
    if (sqlite3_prepare_v2(conn,createTableSQL,len,&stmt,NULL) != SQLITE_OK) {
        if (stmt)
            sqlite3_finalize(stmt);
        sqlite3_close(conn);
        return;
    }
    //3. 通過sqlite3_step命令執(zhí)行創(chuàng)建表的語句。對于DDL和DML語句而言,sqlite3_step執(zhí)行正確的返回值
    //只有SQLITE_DONE,對于SELECT查詢而言,如果有數(shù)據(jù)返回SQLITE_ROW,當?shù)竭_結(jié)果集末尾時則返回
    //SQLITE_DONE。
    if (sqlite3_step(stmt) != SQLITE_DONE) {
        sqlite3_finalize(stmt);
        sqlite3_close(conn);
        return;
    }
    //4. 釋放創(chuàng)建表語句對象的資源。
    sqlite3_finalize(stmt);
    printf("Succeed to create test table now./n");
    //5. 構造查詢表數(shù)據(jù)的sqlite3_stmt對象。
    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語句的對象,獲取結(jié)果集中的字段數(shù)量。
    int fieldCount = sqlite3_column_count(stmt2);
    printf("The column count is %d./n",fieldCount);
    //7. 遍歷結(jié)果集中每個字段meta信息,并獲取其聲明時的類型。    
    for (int i = 0; i < fieldCount; ++i) {
        //由于此時Table中并不存在數(shù)據(jù),再有就是SQLite中的數(shù)據(jù)類型本身是動態(tài)的,所以在沒有數(shù)據(jù)時
        //無法通過sqlite3_column_type函數(shù)獲取,此時sqlite3_column_type只會返回SQLITE_NULL,
        //直到有數(shù)據(jù)時才能返回具體的類型,因此這里使用了sqlite3_column_decltype函數(shù)來獲取表聲
        //明時給出的聲明類型。
        string stype = sqlite3_column_decltype(stmt2,i);
        stype = strlwr((char*)stype.c_str());
        //下面的解析規(guī)則見該系列的“數(shù)據(jù)類型-->1. 決定字段親緣性的規(guī)則”部分,其鏈接如下:
        //http://www.survivalescaperooms.com/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. 為了方便下一次測試運行,我們這里需要刪除該函數(shù)創(chuàng)建的數(shù)據(jù)表,否則在下次運行時將無法
    //創(chuàng)建該表,因為它已經(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)建測試數(shù)據(jù)表。
    2). 通過INSERT語句插入測試數(shù)據(jù)。
    3). 刪除測試表。
    見以下代碼及關鍵性注釋:

 

復制代碼代碼如下:

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

 

using namespace std;

void doTest()
{
    sqlite3* conn = NULL;
    //1. 打開數(shù)據(jù)庫
    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. 準備創(chuàng)建數(shù)據(jù)表,如果創(chuàng)建失敗,需要用sqlite3_finalize釋放sqlite3_stmt對象,以防止內(nèi)存泄露。
    if (sqlite3_prepare_v2(conn,createTableSQL,len,&stmt,NULL) != SQLITE_OK) {
        if (stmt)
            sqlite3_finalize(stmt);
        sqlite3_close(conn);
        return;
    }
    //3. 通過sqlite3_step命令執(zhí)行創(chuàng)建表的語句。對于DDL和DML語句而言,sqlite3_step執(zhí)行正確的返回值
    //只有SQLITE_DONE,對于SELECT查詢而言,如果有數(shù)據(jù)返回SQLITE_ROW,當?shù)竭_結(jié)果集末尾時則返回
    //SQLITE_DONE。
    if (sqlite3_step(stmt) != SQLITE_DONE) {
        sqlite3_finalize(stmt);
        sqlite3_close(conn);
        return;
    }
    //4. 釋放創(chuàng)建表語句對象的資源。
    sqlite3_finalize(stmt);
    printf("Succeed to create test table now./n");

    int insertCount = 10;
    //5. 構建插入數(shù)據(jù)的sqlite3_stmt對象。
    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. 為了方便下一次測試運行,我們這里需要刪除該函數(shù)創(chuàng)建的數(shù)據(jù)表,否則在下次運行時將無法
    //創(chuàng)建該表,因為它已經(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.

 

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 安陆市| 平定县| 新平| 古田县| 保定市| 迁安市| 温宿县| 无为县| 玉溪市| 柳州市| 河间市| 永丰县| 南华县| 大同县| 福海县| 柳州市| 大足县| 密云县| 张家港市| 桓台县| 贺州市| 邹平县| 百色市| 宣恩县| 丹江口市| 巴彦淖尔市| 思南县| 泰兴市| 广西| 南靖县| 九寨沟县| 永丰县| 太仓市| 紫金县| 永仁县| 上高县| 定结县| 易门县| 苏尼特左旗| 常宁市| 白城市|