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;

}
相关推荐
数据库那些事儿32 分钟前
DMS Airflow:企业级数据工作流编排平台的专业实践
数据库
一 乐1 小时前
流浪动物救助|流浪猫狗救助|基于Springboot+vue的流浪猫狗救助平台设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·毕设
好记忆不如烂笔头abc1 小时前
Configuration of TCP/IP with SSL and TLS for Database Connections
数据库·网络协议·ssl
安全系统学习1 小时前
自学网络安全学习的误区和陷阱
数据库·学习·安全·web安全·网络安全·安全架构
黄色茶杯2 小时前
AI编程工具TRAE解决日常问题之SQLite数据复制
数据库·sqlite
老华带你飞2 小时前
订票系统|车票管理系统|基于Java+vue的车票管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·订票系统
weixin_wx520-19832 小时前
骑士人才网全系与phpyun人才网系统数据转移或互转的技术文档和要领,和大家一起共勉
数据库·骑士人才网开源版·骑士人才网数据转移·phpyun人才网源码
聆风吟º2 小时前
国产化数据库选型深度剖析:金仓KES与达梦DM全生命周期成本对比
数据库·kingbasees
码农阿豪2 小时前
金仓KES vs. 达梦DM:全面对比解析迁移、运维与授权成本
运维·数据库·国产
qqxhb3 小时前
系统架构设计师备考第67天——数据库系统的安全&系统架构的脆弱性
数据库·安全·系统架构·访问控制·完整性·脆弱性·身份鉴别