sqlite3数据库的相关API使用

1 1:使用sqlite3_exec函数读取数据库的数据,将这些数据存入链表遍历该链表,输出"字段"="数据"2:使用sqlite3_get_table读取数据库的数据写一个"字段" ="数据"输出的循环

1

cpp 复制代码
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
#include <sqlite3.h>

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;

typedef struct sqlite_data{
    char id[20];
    char name[20];
    char sorce[20];
}sqlite_t;
typedef struct STU{
    union {
        sqlite_t  date;
        struct STU *tail;
    };
    struct STU *next;                                                                               
}stu_t;
int callback(void* arg , int argc , char **argv , char **col){
    stu_t * head  =(stu_t*)arg;
    stu_t * t = calloc(1, sizeof(stu_t));
    head->tail->next = t;

    strcpy(t->date.id,argv[0]);
    strcpy(t->date.name,argv[1]);
    strcpy(t->date.sorce,argv[2]);
    
    head->tail = t;
    t->next = NULL;
    //free(t);
    return 0;
}

int main(int argc, const char *argv[])
{
    sqlite3 *db = NULL;
    if(sqlite3_open("./student.db" , &db)!= SQLITE_OK){
        printf("数据库打开失败\n");
        return -1;
    }

    char * errmsg = NULL;
    stu_t *stu = calloc(1, sizeof(stu_t));
    stu->next =NULL;
    stu->tail = stu;
    const char *sql = "SELECT * FROM stu";
    if(sqlite3_exec(db , sql , callback , stu  , &errmsg)!= SQLITE_OK){
        printf("errmsg:%s\n" , errmsg);
        return -1;
    }
    sqlite3_close(db);
    
    stu_t * p  = stu->next;
    while(p!=NULL){
        printf("id = %s\n",p->date.id);
        printf("neme = %s\n",p->date.name);
        printf("sorce =%s\n ",p->date.sorce);//这个 出结果了;
        p=p->next;
        printf("-----------------------\n");
    }

    return 0;
}
                                                                                                    
cpp 复制代码
ubuntu@ubuntu:~/11/dataaa$ vim stu.c
ubuntu@ubuntu:~/11/dataaa$ cat stu.c
#include <stdio.h> 
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>
#include <sqlite3.h>
typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;





int main(int argc, const char *argv[])
{
	sqlite3  *db = NULL;
	if(sqlite3_open("./student.db" , &db)!= SQLITE_OK){
		printf("数据库打开失败\n");
		return -1;
	}
	char *errmsg = NULL;
	const char * sql = "SELECT * FROM stu";
	char **buf = NULL;
	int  recordno = 0;
	int colno = 0;
	if(sqlite3_get_table(db ,sql, &buf ,&recordno , &colno ,&errmsg)!= SQLITE_OK){
		printf(" errmsg:%s\n" , errmsg);
		return -1;
	}
	sqlite3_close(db);
	for(int i = 0  ; i < recordno ; i++){
		for(int j = 0 ; j<   colno ; j++){
			printf("\t%s" , *(buf+j+i*colno));
		}
		printf("\n");
	}
	return 0;

}
相关推荐
Anastasiozzzz7 小时前
MySQL深分页问题与优化思路
数据库·mysql
伯明翰java7 小时前
数据库的操作
数据库
知识分享小能手11 小时前
PostgreSQL 入门学习教程,从入门到精通,PostgreSQL 16 语法知识点与案例详解(1)
数据库·学习·postgresql
康康的AI博客11 小时前
智能情感分析与品牌策略优化:如何通过AI洞察提升企业市场响应力
大数据·数据库·人工智能
Anastasiozzzz11 小时前
阿亮随手记:MySQL移除查询缓存、子查询优化深分页、自增主键溢出、索引失效
数据库·mysql·缓存
ppp今天又没打瓦12 小时前
围达梦数据库批量插入更新性能实测:普通表、一级分区与二级分区的对决
数据库
@insist12312 小时前
软考-数据库系统工程师-计算机体系结构与流水线核心考点解析
数据库·软考·数据系统工程师
可观测性用观测云13 小时前
KES(KingbaseES)数据库监控最佳实践
数据库
新缸中之脑13 小时前
在Reddit上探索未满足的需求
数据库·oracle
安当加密13 小时前
用 SMS 凭据管理系统替代 HashiCorp Vault:中小企业的轻量级 Secrets 管理实践
服务器·数据库·安全·阿里云