Sqlite3安装与使用

前端之家收集整理的这篇文章主要介绍了Sqlite3安装与使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
安装:
sudo apt-get install sqlite3 libsqlite3-dev

使用:

//gcc test.c -o m -l sqlite3
#include <stdio.h>
#include <string.h>
#include "sqlite3.h"
 
void sqlite_test();
char query(sqlite3 *db,const char *sql);
 
int main() {
    sqlite_test(); 
}
 
void sqlite_test() {
    sqlite3 *db;
    int rc;
    rc = sqlite3_open("sqlite.sqlite3",&db);
    if(rc) {
        fprintf(stderr,"打开失败%s\n",sqlite3_errmsg(db));
        sqlite3_close(db);
    }else {
        printf("成功\n");
        char *sql = "CREATE TABLE IF NOT EXISTS test(\
                    id INTEGER PRIMARY KEY,\
                    uid INTEGER,\
                    nickname VARCHAR(12),\
                    latitude FLOAT\
        )";
        query(db,sql);
        sql = "INSERT INTO test(uid,nickname,latitude)VALUES(1,'abcdefg',1.234)";
        query(db,sql);
        sql = "SELECT * FROM test";
        query(db,sql);
    }
}
char query(sqlite3 *db,const char *sql) {
    printf("%s\n",sql);   
    char *select_str = "SELECT";
    char *errorMsg;
    char *str_str = strstr(sql,select_str);
    if(str_str) {
        printf("in it,%s\n",str_str);
        int row = 0,column = 0,i = 0;
        char **result;
        sqlite3_get_table(db,sql,&result,&row,&column,&errorMsg);
        printf("row:%d,column:%d\n",row,column);
        for(; i < column * (row + 1); i++) {
            printf("result[%d]=%s\n",i,result[i]);   
        }  
    }else{
        sqlite3_exec(db,&errorMsg);
    }
    if(errorMsg){
        printf("%s\n",errorMsg);
    }else{
        printf("success!\n");
    }
}
原文链接:https://www.f2er.com/sqlite/200191.html

猜你在找的Sqlite相关文章