CRUD

struct student

typedef struct STUDENT {
unsigned int id;
unsigned char name[16];
unsigned int age;
unsigned char address[64];
} Student;

create table

CREATE TABLE IF NOT EXISTS student(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`name` TEXT NOT NULL,
`age` INT NOT NULL,
`address` TEXT NOT NULL
)
int createTable(sqlite3 *db) {
char *result = {0};
char *sql = "CREATE TABLE IF NOT EXISTS student("\
"`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," \
"`name` TEXT NOT NULL," \
"`age` INT NOT NULL," \
"`address` TEXT NOT NULL);";
int r = sqlite3_exec(db, sql, NULL, 0, &result);
if (r == SQLITE_OK) {
printf("create table success!\n");
}
return r;
}

generate student data

void generateStudent(Student *student, int count) {
// int count = 100;
// Student student[count];
int i = 0;
for (; i < count; i++) {
unsigned char name[16] = {0};
unsigned char address[64] = {0};
sprintf((char *) name, "zing%d", i);
sprintf((char *) address, "jiangsu-wuxi-%d", i);
student[i].id = (unsigned int) (1000 + i);
memcpy(student[i].name, name, sizeof(name));
student[i].age = (unsigned int) (10 + i);
memcpy(student[i].address, address, sizeof(address));
printf("%d-%s-%d-%s\n", student[i].id, student[i].name, student[i].age, student[i].address);
}
// printf("----------==ok---------\n");
// for (i = 0; i < count; ++i) {
// printf("id:%d,name:%s,age:%d,address:%s\n", student[i].id, student[i].name, student[i].age, student[i].address);
// }
}

insert table

int insertTable(sqlite3 *db, Student student) {
char sql[128], *result = {0};
sprintf(sql, "INSERT INTO student (`name`,`age`,`address`) VALUES('%s','%d','%s')", student.name, student.age,
student.address);
int rc = sqlite3_exec(db, sql, NULL, 0, &result);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", result);
sqlite3_free(result);
} else {
printf("insert ok!\n");
}
return rc;
}

query table 1

int queryTable(sqlite3 *db, Student *student, int index, int count) {
char sql[128];
if (count > 10)
count = 10;
sprintf(sql, "select * from student order by id desc limit %d,%d", index, count);
printf("%s\n", sql);
char *err = NULL;
int col, row, i, j = 0;
char **result;
int rs = sqlite3_get_table(db, sql, &result, &row, &col, &err);
if (rs == SQLITE_OK) {
printf("row %d, col %d\n", row, col);
// id-name-age-address
// printf("%s-%s-%s-%s\n", result[0], result[1], result[2], result[3]);
// 1-zing0-10-jiangsu-wuxi-0
// printf("%s-%s-%s-%s\n", result[4], result[5], result[6], result[7]);
// printf("%s-%s-%s-%s\n", result[1120], result[1121], result[1122], result[1123]);
// for (i = 0; i < (row + 1) * col; i++) {
// printf("%s\n", result[i]);
// }
for (i = 0; i < (row + 1) * col; i++) {
if (i % col == 0 && i >= col) {
student[j].id = (unsigned int) atoi(result[i]);
memcpy(student[j].name, result[i + 1], sizeof(student[i].name));
student[j].age = (unsigned int) atoi(result[i + 2]);
memcpy(student[j].address, result[i + 3], sizeof(student[i].address));
// printf("id:%d,name:%s,age:%d,address:%s\n", student[j].id, student[j].name, student[j].age, student[j].address);
// printf("id:%s,name:%s,age:%s,address:%s\n", result[i], result[i + 1], result[i + 2], result[i + 3]);
j++;
}
}
} else {
printf("error\n");
}
return rs;
}

query table 2

/*
* @param stu
* @param argc
* @param argv
* @param azColName
* @return
*/
int queryCallback(void *stu, int argc, char **argv, char **azColName) {
static int j;
Student *student = stu;
student[j].id = (unsigned int) atoi(argv[0]);
memcpy(student[j].name, argv[1], sizeof(student[j].name));
student[j].age = (unsigned int) atoi(argv[2]);
memcpy(student[j].address, argv[3], sizeof(student[j].address));
j++;
// printf("\n");
// for (i = 0; i < argc; i++) {
// printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
// }
// printf("argc-->%d\n",argc);
return 0;
} int query(sqlite3 *db, Student *student, int index, int count) {
char sql[128];
sprintf(sql, "select * from student order by id desc limit %d,%d;", index, count);
char **err = NULL;
return sqlite3_exec(db, sql, queryCallback, student, err);
}

update table

int update(sqlite3 *db, Student student) {

    char sql[128];
sprintf(sql, "UPDATE student set address = '%s' where ID=%d;", student.address, student.id);
char *err = NULL;
/* Execute SQL statement */
int rc = sqlite3_exec(db, sql, NULL, (void *) 0, &err);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", err);
sqlite3_free(err);
} else {
fprintf(stdout, "Operation done successfully\n");
}
return rc;
}
int update2(sqlite3 *db, int id, char *address) { char sql[128];
sprintf(sql, "UPDATE student set address = '%s' where ID=%d;", address, id);
char *err = NULL;
/* Execute SQL statement */
int rc = sqlite3_exec(db, sql, NULL, (void *) 0, &err);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", err);
sqlite3_free(err);
} else {
fprintf(stdout, "Operation done successfully\n");
}
return rc;
} int update3(sqlite3 *db, void *data) {
Student *student = (Student *) data;
printf("--> %d\n", student->id);
printf("--> %s\n", student->address);
char sql[128];
sprintf(sql, "UPDATE student set address = '%s' where ID=%d;", student->address, student->id);
char *err = NULL;
/* Execute SQL statement */
int rc = sqlite3_exec(db, sql, NULL, (void *) 0, &err);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", err);
sqlite3_free(err);
} else {
fprintf(stdout, "Operation done successfully\n");
}
return rc;
}

main test

int main() {
int i = 0;
const char *name = "test.db";
sqlite3 *db;
int count = 10;
Student student[count]; int r = sqlite3_open(name, &db);
if (r == 0) {
printf("open sb success\n");
}
// generateStudent(student, count);
// printf("----------==ok---------\n");
// for (i = 0; i < count; ++i) {
// printf("id:%d,name:%s,age:%d,address:%s\n", student[i].id, student[i].name, student[i].age, student[i].address);
// }
// createTable(db);
// for (i = 0; i < count; ++i) {
// insertTable(db, student[i]);
// }
if (queryTable(db, student, 2, 10) == SQLITE_OK) {
for (i = 0; i < count; ++i) {
printf("id:%d,name:%s,age:%d,address:%s\n", student[i].id, student[i].name, student[i].age,
student[i].address);
}
} Student stu;
stu.id = 68;
char *address = "jiangsu-xuzhou";
memcpy(stu.address, address, strlen(address));
update(db, stu);
queryTable(db, student, 0, 10);
for (i = 0; i < count; ++i) {
printf("id:%d,name:%s,age:%d,address:%s\n", student[i].id, student[i].name, student[i].age,
student[i].address);
}
update2(db, 69, address);
queryTable(db, student, 0, 10);
for (i = 0; i < count; ++i) {
printf("id:%d,name:%s,age:%d,address:%s\n", student[i].id, student[i].name, student[i].age,
student[i].address);
}
address = "jiangsu-suzhou";
stu.id = 70;
memcpy(stu.address, address, strlen(address));
update3(db, &stu);
if (queryTable(db, student, 0, 10)) {
for (i = 0; i < count; ++i) {
printf("update3-> id:%d,name:%s,age:%d,address:%s\n", student[i].id, student[i].name, student[i].age,
student[i].address);
}
}
memset(student, 0, sizeof(student));
if (query(db, student, 0, 10) == SQLITE_OK) {
for (i = 0; i < count; ++i) {
printf("update-> id:%d,name:%s,age:%d,address:%s\n", student[i].id, student[i].name, student[i].age,
student[i].address);
}
}
sqlite3_close(db);
return 0;
}

all code

//
// Created by zhangrongxiang on 2018/2/23 14:10
// File sql
// #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlite3.h> int queryCallback(void *stu, int argc, char **argv, char **azColName); typedef struct STUDENT {
unsigned int id;
unsigned char name[16];
unsigned int age;
unsigned char address[64];
} Student; void generateStudent(Student *student, int count) {
// int count = 100;
// Student student[count];
int i = 0;
for (; i < count; i++) {
unsigned char name[16] = {0};
unsigned char address[64] = {0};
sprintf((char *) name, "zing%d", i);
sprintf((char *) address, "jiangsu-wuxi-%d", i);
student[i].id = (unsigned int) (1000 + i);
memcpy(student[i].name, name, sizeof(name));
student[i].age = (unsigned int) (10 + i);
memcpy(student[i].address, address, sizeof(address));
printf("%d-%s-%d-%s\n", student[i].id, student[i].name, student[i].age, student[i].address);
}
// printf("----------==ok---------\n");
// for (i = 0; i < count; ++i) {
// printf("id:%d,name:%s,age:%d,address:%s\n", student[i].id, student[i].name, student[i].age, student[i].address);
// }
} int createTable(sqlite3 *db) {
char *result = {0};
char *sql = "CREATE TABLE IF NOT EXISTS student("\
"`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," \
"`name` TEXT NOT NULL," \
"`age` INT NOT NULL," \
"`address` TEXT NOT NULL);";
// *table = *sql;
int r = sqlite3_exec(db, sql, NULL, 0, &result);
if (r == SQLITE_OK) {
printf("create table success!\n");
}
return r;
} int insertTable(sqlite3 *db, Student student) {
char sql[128], *result = {0};
sprintf(sql, "INSERT INTO student (`name`,`age`,`address`) VALUES('%s','%d','%s')", student.name, student.age,
student.address);
int rc = sqlite3_exec(db, sql, NULL, 0, &result);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", result);
sqlite3_free(result);
} else {
printf("insert ok!\n");
}
return rc;
} /*
* @param stu
* @param argc
* @param argv
* @param azColName
* @return
*/
int queryCallback(void *stu, int argc, char **argv, char **azColName) {
static int j;
Student *student = stu;
student[j].id = (unsigned int) atoi(argv[0]);
memcpy(student[j].name, argv[1], sizeof(student[j].name));
student[j].age = (unsigned int) atoi(argv[2]);
memcpy(student[j].address, argv[3], sizeof(student[j].address));
j++;
// printf("\n");
// for (i = 0; i < argc; i++) {
// printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
// }
// printf("argc-->%d\n",argc);
return 0;
} int query(sqlite3 *db, Student *student, int index, int count) {
char sql[128];
sprintf(sql, "select * from student order by id desc limit %d,%d;", index, count);
char **err = NULL;
return sqlite3_exec(db, sql, queryCallback, student, err);
} int queryTable(sqlite3 *db, Student *student, int index, int count) {
char sql[128];
if (count > 10)
count = 10;
sprintf(sql, "select * from student order by id desc limit %d,%d", index, count);
printf("%s\n", sql);
char *err = NULL;
int col, row, i, j = 0;
char **result;
int rs = sqlite3_get_table(db, sql, &result, &row, &col, &err);
if (rs == SQLITE_OK) {
printf("row %d, col %d\n", row, col);
// id-name-age-address
// printf("%s-%s-%s-%s\n", result[0], result[1], result[2], result[3]);
// 1-zing0-10-jiangsu-wuxi-0
// printf("%s-%s-%s-%s\n", result[4], result[5], result[6], result[7]);
// printf("%s-%s-%s-%s\n", result[1120], result[1121], result[1122], result[1123]);
// for (i = 0; i < (row + 1) * col; i++) {
// printf("%s\n", result[i]);
// }
for (i = 0; i < (row + 1) * col; i++) {
if (i % col == 0 && i >= col) {
student[j].id = (unsigned int) atoi(result[i]);
memcpy(student[j].name, result[i + 1], sizeof(student[i].name));
student[j].age = (unsigned int) atoi(result[i + 2]);
memcpy(student[j].address, result[i + 3], sizeof(student[i].address));
// printf("id:%d,name:%s,age:%d,address:%s\n", student[j].id, student[j].name, student[j].age, student[j].address);
// printf("id:%s,name:%s,age:%s,address:%s\n", result[i], result[i + 1], result[i + 2], result[i + 3]);
j++;
}
}
} else {
printf("error\n");
}
return rs;
} int update(sqlite3 *db, Student student) { char sql[128];
sprintf(sql, "UPDATE student set address = '%s' where ID=%d;", student.address, student.id);
char *err = NULL;
/* Execute SQL statement */
int rc = sqlite3_exec(db, sql, NULL, (void *) 0, &err);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", err);
sqlite3_free(err);
} else {
fprintf(stdout, "Operation done successfully\n");
}
return rc;
} int update2(sqlite3 *db, int id, char *address) { char sql[128];
sprintf(sql, "UPDATE student set address = '%s' where ID=%d;", address, id);
char *err = NULL;
/* Execute SQL statement */
int rc = sqlite3_exec(db, sql, NULL, (void *) 0, &err);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", err);
sqlite3_free(err);
} else {
fprintf(stdout, "Operation done successfully\n");
}
return rc;
} int update3(sqlite3 *db, void *data) {
Student *student = (Student *) data;
printf("--> %d\n", student->id);
printf("--> %s\n", student->address);
char sql[128];
sprintf(sql, "UPDATE student set address = '%s' where ID=%d;", student->address, student->id);
char *err = NULL;
/* Execute SQL statement */
int rc = sqlite3_exec(db, sql, NULL, (void *) 0, &err);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", err);
sqlite3_free(err);
} else {
fprintf(stdout, "Operation done successfully\n");
}
return rc;
} int main() {
int i = 0;
const char *name = "test.db";
sqlite3 *db;
int count = 10;
Student student[count]; int r = sqlite3_open(name, &db);
if (r == 0) {
printf("open sb success\n");
}
// generateStudent(student, count);
// printf("----------==ok---------\n");
// for (i = 0; i < count; ++i) {
// printf("id:%d,name:%s,age:%d,address:%s\n", student[i].id, student[i].name, student[i].age, student[i].address);
// }
// createTable(db);
// for (i = 0; i < count; ++i) {
// insertTable(db, student[i]);
// }
if (queryTable(db, student, 2, 10) == SQLITE_OK) {
for (i = 0; i < count; ++i) {
printf("id:%d,name:%s,age:%d,address:%s\n", student[i].id, student[i].name, student[i].age,
student[i].address);
}
} Student stu;
stu.id = 68;
char *address = "jiangsu-xuzhou";
memcpy(stu.address, address, strlen(address));
update(db, stu);
queryTable(db, student, 0, 10);
for (i = 0; i < count; ++i) {
printf("id:%d,name:%s,age:%d,address:%s\n", student[i].id, student[i].name, student[i].age,
student[i].address);
}
update2(db, 69, address);
queryTable(db, student, 0, 10);
for (i = 0; i < count; ++i) {
printf("id:%d,name:%s,age:%d,address:%s\n", student[i].id, student[i].name, student[i].age,
student[i].address);
}
address = "jiangsu-suzhou";
stu.id = 70;
memcpy(stu.address, address, strlen(address));
update3(db, &stu);
if (queryTable(db, student, 0, 10)) {
for (i = 0; i < count; ++i) {
printf("update3-> id:%d,name:%s,age:%d,address:%s\n", student[i].id, student[i].name, student[i].age,
student[i].address);
}
}
memset(student, 0, sizeof(student));
if (query(db, student, 0, 10) == SQLITE_OK) {
for (i = 0; i < count; ++i) {
printf("update-> id:%d,name:%s,age:%d,address:%s\n", student[i].id, student[i].name, student[i].age,
student[i].address);
}
}
sqlite3_close(db);
return 0;
}

See

All rights reserved

C 扩展库 - sqlite3 API CRUD的更多相关文章

  1. C 扩展库 - sqlite3 API

    sqlite3 API Summary sqlite3 The database connection object. Created by sqlite3_open() and destroyed ...

  2. C 扩展库 - mysql API CRUD

    CRUD table create table if not exists `student` ( `id` int auto_increment, `name` varchar(16) not nu ...

  3. C 扩展库 - mysql API

    MySQL API C API Data Structures MYSQL This structure represents handler for one database connection. ...

  4. C 扩展库 - mysql API general outline

    Application programs should use this general outline for interacting with MySQL Initialize the MySQL ...

  5. 【小结】有关mysql扩展库和mysqli扩展库的crud操作封装

    现阶段php如果要操作mysql数据库 php给我们提供了3套库 1.mysql扩展库   面向过程操作 2.mysqli扩展库  面向对象操作和面向过程操作并存  安全性和效率高于mysql扩展库 ...

  6. 安装php扩展库

    无法加载'pdo_mysql' ,因为需要pdo这个module.PHP Warning: Cannot load module 'pdo_mysql' because required module ...

  7. 『Python CoolBook』C扩展库_其一_用法讲解

    不依靠其他工具,直接使用Python的扩展API来编写一些简单的C扩展模块. 本篇参考PythonCookbook第15节和Python核心编程完成,值得注意的是,Python2.X和Python3. ...

  8. php数据库编程---mysql扩展库

    1, Java有一种方式操作数据库, PHP有三种方式来操作mysql数据库.(1)mysql扩展库:(2)mysqli扩展库:(3)pdo: 2, mysql扩展库和mysql数据库区别 3, my ...

  9. GLEW扩展库【转】

    http://blog.sina.com.cn/s/blog_4aff14d50100ydsy.html 一.关于GLEW扩展库: GLEW是一个跨平台的C++扩展库,基于OpenGL图形接口.使用O ...

随机推荐

  1. 咏南LINUX中间件

    咏南LINUX中间件 什么是跨平台? DELPHI跨平台已经不是停留在理论,而是可以实用了. 同一套中间件既能在WINDOWS服务器上面部署,又可以在LINUX服务器上面部署. 而做到这一切,只需要一 ...

  2. Python学习-5.Python的变量与数据类型及字符串的分割与连接

    在Python中,变量类型是固定的,一旦声明就不能修改其类型(在Python里感觉不应该用声明,而应该用使用) 正确: var = 1 print(var) var = 2 print(var) 依次 ...

  3. 详解CSS盒模型

    原文地址:http://luopq.com/2015/10/26/CSS-Box-Model/ 本文主要是学习CSS盒模型的笔记,总结了一些基本概念,知识点和细节. 一些基本概念 HTML的大多数元素 ...

  4. Vsual Studio 高版本打开低版本项目的方法

    https://www.cnblogs.com/jmliao/p/5594179.html

  5. 使用NPOI时ICSharpCode.SharpZipLib版本冲突问题解决

    系统原来引用的ICSharpCode.SharpZipLib是0.84版本的, 添加了2.3版本的NPOI引用后,报版本冲突错误,因为NPOI用的ICSharpCode.SharpZipLib是0.8 ...

  6. c#进阶之lambda表达式

    阅读之前,先确保对委托有基本的了解,传送门 c#进阶之浅析委托和事件. lambda表达式雏形第一步 在委托那篇文章,绑定的的方法都是具名函数,为了简化书写,可以换成匿名函数 public deleg ...

  7. windows 10 自适应布局

    https://msdn.microsoft.com/library/windows/apps/dn894631.aspx Use visual state triggers to build UI ...

  8. C#基础笔记(第二十二天)

    1.单例模式1)将构造函数私有化2)提供一个静态方法,返回一个对象3)创建一个单例 2.XML可扩展的标记语言 HTMLXML:存储数据 不是单独.net的东西,是一个单独的,JAVA什么的都也用不需 ...

  9. redis5.0.4-cluster集群搭建及jedis客户端操作

    一.去官网下载redis5.0 https://redis.io/download 然后解压安装 $ .tar.gz $ cd redis- $ make 二.准备配置文件 打开redis-5.0.4 ...

  10. 为解决Samba windows 无法访问 尝试过的方法

    1, 通过   vi /etc/sysconfig/selinux 把 SELINUX=enforcing   修改为SELINUX= disable 退出保存,并且重启.(设置了) 2, 把wind ...