数据库:编程(打开、操作、关闭)

一、需要的头文件

sqlite3.h

二、编译过程

gcc xxx -lsqlite3

三、编程框架

打开数据库 ==》读写数据库(增,删,改,查) ==》关闭数据库

3.1 打开数据库: sqlite3_open

int sqlite3_open(char * path,sqlite3 ** db);

功能:打开指定path路径+文件名称的数据库,并将打开的地址指向db变量的句柄。

参数:path 要打开的数据库路径+名称

db 要打开的数据库地址指针

返回值:成功 0

失败 -1;

3.2 关闭数据库: sqlite3_close

int sqlite3_close(sqlite3 *db);

功能:关闭指定的数据库

参数:要关闭的数据库地址

返回值:成功 0

失败 -1;

3.3 数据库操作:

查询操作:sqlite3_get_table(); select

int sqlite3_get_table(sqlite3 *db,char *sql,char **++***++**result,int *nrow,int *ncol,char ** errmsg);

功能:在db数据库上执行sql查询语句,并将执行的结果集返回到rest地址上,同时返回查询的行和列。

参数:db 要执行查询语句的数据库

sql 要执行的select查询语句

result 查询的结果集是一个三级指针//

nrow 查询的结果的行数

ncol 查询的结果的列数

errmsg 如果执行有错误,则存储错误

返回值:成功 0

失败 非0;

cs 复制代码
#include <stdio.h>
#include <sqlite3.h>

int show(void*arg ,int col ,char**result ,char**title)
{
    *(int*)arg = 1;
    return 0;
}
int main(int argc, char *argv[])
{
    sqlite3* db;
    int ret = sqlite3_open("aaa.db",&db);
    if(SQLITE_OK != ret)
    {
        fprintf(stderr,"open db error:%s\n",sqlite3_errmsg(db));
        sqlite3_close(db); 
        return  1;
    }

    char name[50]="yyy";
    char pass[50] ="18";
    char sql_cmd[256]={0};
    sprintf(sql_cmd,"select * from user where name like '%s' and age ='%s';"
            ,name,pass);
    char * errmsg;
    int flag = 0;
    ret = sqlite3_exec(db,sql_cmd,show,&flag,&errmsg);
    if(SQLITE_OK != ret)
    {
        fprintf(stderr,"exec sqlcmd error:%s\n",errmsg);
        sqlite3_free(errmsg);
        sqlite3_close(db); 
        return  1;
    }
    if(0 == flag)
    {
        printf("username passwd error\n");
    }
    else 
    {
        printf("username passwd correct\n");
    }
    sqlite3_close(db);
    return 0;
}

执行sql语句 :sqlite3_exec(); insert delete update

int sqlite3_exec(sqlite3 *db,char *sql,callback fun,void * arg,char ** errmsg);

功能:在db数据库上执行sql ++非查询语句++ 。并将结果返回。

参数:db 要执行sql的数据库

sql 要执行的非查询sql语句

fun 如果该函数要执行查询语句,则该回调函数用来回收查询的结果。第三个参数返回结果

arg 回调函数的参数,如果没有回调函数则该参数为NULL;

errmsg 执行过程中的错误信息

返回值:成功 0

失败 非0 ;

cs 复制代码
#include <stdio.h>
#include <sqlite3.h>

int main(int argc, char *argv[])
{
    sqlite3* db;
    int ret = sqlite3_open("aaa.db",&db);
    if(SQLITE_OK != ret)
    {
        fprintf(stderr,"open db error:%s\n",sqlite3_errmsg(db));
        sqlite3_close(db); 
        return  1;
    }
    char sql_cmd[256]="insert into user values(10,'yyy',18);";
    char * errmsg;
    ret = sqlite3_exec(db,sql_cmd,NULL,NULL,&errmsg);
    if(SQLITE_OK != ret)
    {
        fprintf(stderr,"exec sqlcmd error:%s\n",errmsg);
        sqlite3_free(errmsg);
        sqlite3_close(db); 
        return  1;
    }

    sqlite3_close(db);
    return 0;
}

int fun(void *arg ,int f_num,char ** f_value,char ** f_name)

功能:该函数用于sqlite3_exec执行select语句的,结果集返回数据。

参数:arg 由sqlite3_exec传入的参数

f_num 执行该命令所返回测结果集的字段个数。

f_value 查询结果集中的字段的值。

f_name 查询结果集中的字段的名称。

返回值:成功 0

失败 非0

注意:该回调函数必须有返回值,否则可能导致查询异常。

相关推荐
Zzz 小生1 小时前
Claude Code学习笔记(四)-助你快速搭建首个Python项目
大数据·数据库·elasticsearch
liujing102329293 小时前
stm32大项目阶段20251015
linux
嵌入式郑工4 小时前
LINUX驱动开发: 设备和驱动是怎么匹配的?
linux·运维·服务器
nongcunqq4 小时前
abap 操作 excel
java·数据库·excel
rain bye bye5 小时前
calibre LVS 跑不起来 就将setup 的LVS Option connect下的 connect all nets by name 打开。
服务器·数据库·lvs
郭式云源生法则5 小时前
归档及压缩、重定向与管道操作和综合使用,find精确查找、find处理查找结果、vim高级使用、vimdiff多文件使用
linux·运维·服务器
一张假钞5 小时前
Ubuntu 24.04 安装 Jenkins
linux·ci/cd·jenkins
tuokuac6 小时前
查看你电脑上某个端口正在被哪个进程占用
linux
MANONGMN6 小时前
Linux 通配符与正则表达式(含实战案例+避坑指南)
linux·运维·正则表达式
带土16 小时前
18 .shell编程-正则表达式
linux·正则表达式