sqlite學習筆記10:C語言中使用sqlite之查詢和更新資料

來源:互聯網
上載者:User

標籤:sql   database   sqlite   資料庫   linux   

前面說到的 sqlite_exec() 中的第三個參數, SQLite 將為 sql 參數內執行的每個 SELECT 語句中處理的每個記錄調用這個回呼函數。

本節添加了兩個函數,selectFromTable和updateTable.


執行個體程式如下:

#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;const char* data = "Callback function called";/* 改為全域的 */int ret = 0;typedef enum{    false,    true} bool;/*typedef int (*sqlite3_callback)(void*,    Data provided in the 4th argument of sqlite3_exec()int,      The number of columns in rowchar**,   An array of strings representing fields in the rowchar**    An array of strings representing column names);*/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 createTable(){    /* Create SQL statement */    sql = "CREATE TABLE COMPANY("      "ID INT PRIMARY KEY     NOT NULL,"     "NAME           TEXT    NOT NULL,"     "AGE            INT     NOT NULL,"     "ADDRESS        CHAR(50),"     "SALARY         REAL );";        /* Execute SQL statement */    ret = sqlite3_exec(db, sql, callback, 0, &zErrMsg);    if( ret != SQLITE_OK ){        fprintf(stderr, "Error SQL: %s\n", zErrMsg);        sqlite3_free(zErrMsg);                return false;    }        fprintf(stdout, "Successfully table created\n");        return true;}bool insertRecords(){    /* Create SQL statement */    sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "      "VALUES (1, 'Paul', 32, 'California', 20000.00 ); "     "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "      "VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); "         "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)"     "VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );"     "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)"     "VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );";        /* Execute SQL statement */    ret = sqlite3_exec(db, sql, callback, 0, &zErrMsg);    if( ret != SQLITE_OK ){        fprintf(stderr, "SQL error: %s\n", zErrMsg);        sqlite3_free(zErrMsg);        return false;    }        fprintf(stdout, "successfully records created\n");    return true;}
bool selectFromTable()/*新加*/{    /* Create SQL statement */    sql = "SELECT * from COMPANY";        /* Execute SQL statement */    ret = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);    if( ret != SQLITE_OK ){        fprintf(stderr, "Error SQL: %s\n", zErrMsg);        sqlite3_free(zErrMsg);        return false;    }        fprintf(stdout, "successfully operation done\n");        return true;}

bool updateTable()/* 新加 */{    /* Create merged SQL statement */    sql = "UPDATE COMPANY set SALARY = 25000.00 where ID=1; "     "SELECT * from COMPANY";        /* Execute SQL statement */    ret = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);    if( ret != SQLITE_OK ){        fprintf(stderr, "SQL error: %s\n", zErrMsg);        sqlite3_free(zErrMsg);                return false;    }        fprintf(stdout, "Successfully operation done \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();    /*createTable();*/    /*insertRecords();*/    selectFromTable();    updateTable();    selectFromTable();    closeDB();        return 0;}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.