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;

}
相关推荐
专业软件系统开发2 小时前
药品说明书查询系统源码 本地数据库 PHP版本
数据库·查询系统源码·说明书查询源码
冉冰学姐6 小时前
SSM足球爱好者服务平台i387z(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·ssm 框架·足球爱好者服务平台
大飞记Python6 小时前
部门管理|“编辑部门”功能实现(Django5零基础Web平台)
前端·数据库·python·django
清风6666667 小时前
基于单片机的智能收银机模拟系统设计
数据库·单片机·毕业设计·nosql·课程设计
资深低代码开发平台专家8 小时前
PostgreSQL 18 发布
数据库·postgresql
与衫9 小时前
在 VS Code 里看清你的数据流向:Gudu SQL Omni 实测体验
数据库·sql
Element_南笙9 小时前
吴恩达新课程:Agentic AI(笔记2)
数据库·人工智能·笔记·python·深度学习·ui·自然语言处理
长安城没有风9 小时前
从入门到精通【Redis】Redis 典型应⽤ --- 分布式锁
数据库·redis·分布式
Ytadpole9 小时前
MySQL 数据库优化设计:优化原理和数据库表设计技巧
数据库·mysql·优化·索引·查询·检索·表设计