c从sqlite3数据库中获取数据,并对数据进行拼接
生活随笔
收集整理的這篇文章主要介紹了
c从sqlite3数据库中获取数据,并对数据进行拼接
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
c從sqlite3數(shù)據(jù)庫中獲取數(shù)據(jù),并對數(shù)據(jù)進行拼接
- 函數(shù)功能
- 對數(shù)據(jù)庫的操作
- 創(chuàng)建數(shù)據(jù)庫:
- 創(chuàng)建USER表:
- 創(chuàng)建表內(nèi)數(shù)據(jù):
- 查看表內(nèi)數(shù)據(jù):
- 查看表結(jié)構(gòu):
- 函數(shù)實現(xiàn)
- 函數(shù)編譯:
- 函數(shù)結(jié)果:
函數(shù)功能
數(shù)據(jù)庫內(nèi)的表:USER
獲取數(shù)據(jù)的命令:select id,name,pass from USER where name = ‘user_one’
獲取到的數(shù)據(jù):
拼接后的格式:
1,user_one,qwerty;3,user_one,123456;對數(shù)據(jù)庫的操作
創(chuàng)建數(shù)據(jù)庫:
- [root@localhost database]# sqlite3 user.db
創(chuàng)建USER表:
- sqlite> CREATE TABLE USER(ID INT PRIMARY KEY NOT NULL,NAME TEXT NOT NULL, PASS TEXT NOT NULL);
創(chuàng)建表內(nèi)數(shù)據(jù):
- sqlite> insert into USER values ("1", "user_one", "qwerty");
- sqlite> insert into USER values ("2", "user_two", "rewqtre");
- sqlite> insert into USER values ("3", "user_one", "123456");
查看表內(nèi)數(shù)據(jù):
- sqlite> select * from USER;1|user_one|qwerty2|user_two|rewqtre3|user_one|123456
查看表結(jié)構(gòu):
- sqlite> .schema
函數(shù)實現(xiàn)
#include <stdio.h> #include <string.h> #include <sqlite3.h>#define VALUE_LEN 1024 #define MAX_BUF 4096 #define PARAMETER_VALUE_ERROR 101 //invalid parameter #define INFO_NOT_FOUND 142 //sqlite get table erro#define SUCCESS 0//數(shù)據(jù)庫訪問錯誤 #define DATA_NOT_FOUND_ERROR 130 //未獲取到數(shù)據(jù) #define DB_ACCESS_ERROR 131//訪問數(shù)據(jù)庫失敗 #define DB_SQL_EXEC_ERROR 132 //執(zhí)行sql 失敗 #define DB_TABLE_ACCESS_ERROR 133 //訪問數(shù)據(jù)庫表失敗 #define DB_FILE_OPEN_FAIL 134 //sqlite open errorchar *fwdb = "/home/user.db";sqlite3 *db = NULL;/* 功能:數(shù)據(jù)庫的初始化*/ int initdb() {int rt = 0;rt = sqlite3_open(fwdb, &db);if(rt != SQLITE_OK){printf("打開數(shù)據(jù)庫失敗!\n");return DB_FILE_OPEN_FAIL;} }/* 功能:從數(shù)據(jù)庫獲取信息*/ int getfieldvalue(char *fieldname, char *fieldvalue, char *condfield, char *condvalue, char *table) {char sql[MAX_BUF] = {0};char s_temp[MAX_BUF] = {0};char **tb;int i = 0;int rows = 0; int cols = 0;int rt =0;if(db == NULL)initdb();if(fieldname && condfield && condvalue && table){sprintf(sql, "select %s from %s where %s = '%s'", fieldname, table, condfield, condvalue);printf("%s", sql); rt = sqlite3_get_table(db, sql, &tb, &rows, &cols, NULL); if(rt != SQLITE_OK) {printf("%d, %d, %s\n", rt, SQLITE_OK, sql);printf("sqlite3_get_table error!\n");return DB_TABLE_ACCESS_ERROR;} int j = 0;int index = cols;printf("%d %d\n", rows, cols);/* 查詢結(jié)果:2行3列*///1、是一維數(shù)組,不是二維數(shù)組,前三個字段為字段名,然后才是字段值:/*(gdb) p tb[0]$2 = 0x616438 "ID"(gdb) p tb[1]$3 = 0x616918 "NAME"(gdb) p tb[2]$4 = 0x616938 "PASS"(gdb) p tb[3]$5 = 0x616958 "1"(gdb) p tb[4]$6 = 0x616978 "user_one"(gdb) p tb[5]$7 = 0x616998 "qwerty"(gdb) p tb[6]$8 = 0x6169b8 "3"(gdb) p tb[7]$9 = 0x6169d8 "user_one"(gdb) p tb[8]$10 = 0x6169f8 "123456" */if(rows || cols){ for(i = 0; i < rows; i++){int iflag = 0;for(j = 0; j < cols; j++){strcat(s_temp, tb[index++]);iflag++;if(iflag == cols)strcat(s_temp, ";");elsestrcat(s_temp, ",");}} strcpy(fieldvalue, s_temp);sqlite3_free_table(tb); return SUCCESS;}elsereturn INFO_NOT_FOUND;}return PARAMETER_VALUE_ERROR; }int main() {/* 讀取數(shù)據(jù)庫按照條件*/// getrecordvalues getfieldvaluechar value[VALUE_LEN] = {0};getfieldvalue("id,name,pass", value, "name", "user_one", "USER");printf("%s", value);return 0; }函數(shù)編譯:
[root@localhost 桌面]# gcc -g a.c -o a -lsqlite3函數(shù)結(jié)果:
[root@localhost 桌面]# ./a select id,name,pass from USER where name = 'user_one'2 3 1,user_one,qwerty;3,user_one,123456;總結(jié)
以上是生活随笔為你收集整理的c从sqlite3数据库中获取数据,并对数据进行拼接的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c拆分字符串,并按照指定格式存入结构体数
- 下一篇: Pycharm主题:基于网上一款eyec