作业
题目一
完成插入
#include <string.h>
#include <stdlib.h>
#include <sqlite3.h>
int sq_insert(sqlite3 *db);
int main(int argc, const char *argv[])
{
//打开数据库
sqlite3 *db = NULL;
if(sqlite3_open("./my.db",&db) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_open:%s %d __%d--\n",\
sqlite3_errmsg(db),sqlite3_errcode(db),__LINE__);
return -1;
}
printf("open database my.db success\n");
//创建表格create table if not exists stu (id int,name char,score long);
char sql[128] = "create table if not exists stu (id int,name char,score float)";
char* errmsg = NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_open:%s %d __%d--\n",\
errmsg,sqlite3_errcode(db),__LINE__);
return -1;
}
printf("create table stu success\n");
char choose = 0;
while(1)
{
printf("----------------------\n");
printf("--------1.插入--------\n");
printf("--------2.删除--------\n");
printf("--------3.修改--------\n");
printf("--------4.查询--------\n");
printf("--------5.退出--------\n");
printf("----------------------\n");
printf("请输入>>>");
choose = getchar();
while(getchar() != 10);
switch(choose)
{
case '1':
sq_insert(db);
break;
case '2':
break;
case '3':
break;
case '4':
break;
case '5':
goto END;
default:
printf("输入错误,请重新输入\n");
}
}
END:
//关闭数据库
if(sqlite3_close(db) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_close:%s %d __%d--\n",\
sqlite3_errmsg(db),sqlite3_errcode(db),__LINE__);
return -1;
}
return 0;
}
int sq_insert(sqlite3* db)
{
char sql[128] = "";
char* errmsg = NULL;
char name[128] = "";
int id = 0;
float score = 0;
bzero(name,sizeof(name));
printf("请输入id name score\n");
scanf("%d %s %e",&id,name,&score);
while(getchar() != 10);
sprintf(sql,"insert into stu values(\"%d\",\"%s\",\"%f\");",id,name,score);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("sqlite3_exec failed:%s __%d__\n", errmsg, __LINE__);
return -1;
}
return 0;
}
题目二
将dict.txt导入到数据库
#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
int main(int argc, const char *argv[])
{
//打开数据库
sqlite3 *db = NULL;
if(sqlite3_open("./dict.db", &db) != SQLITE_OK)
{
printf("sqlite3_open failed:%s __%d__\n", sqlite3_errmsg(db), __LINE__);
return -1;
}
printf("创建表格 dict.db 成功 __%d__\n", __LINE__);
//创建表格
char sql[128] = "create table if not exists dict (word char, mean char);";
char* errmsg = NULL;
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("sqlite3_exec failed:%s __%d__\n", errmsg, __LINE__);
return -1;
}
printf("create table dict success __%d__\n", __LINE__);
//打开文件
FILE* fp = fopen("./dict.txt", "r");
if(NULL == fp)
{
perror("fopen");
return -1;
}
//循环读取文件中的数据,一行一行的读取
char buf[256] = "";
char word[32] = "";
char mean[200] = "";
int count = 1;
int i = 0;
while(1)
{
if(fgets(buf, sizeof(buf), fp) == NULL)
break;
buf[strlen(buf)-1] = 0;
//分离单词和意思
bzero(word, sizeof(word));
bzero(mean, sizeof(mean));
for(i=0; i<strlen(buf)-2; i++)
{
//判断当前位置不是空格,下一个位置以及下下个位置是空格
if(buf[i] != ' ' && buf[i+1] == ' ' && buf[i+2]==' ')
{
strncpy(word, buf, i+1);
}
//判断当前位置及下一个位置是空格,下下个位置不是空格
else if(buf[i]==' ' && buf[i+1]==' ' && buf[i+2]!=' ')
{
strcpy(mean, buf+i+2);
break;
}
}
sprintf(sql,"insert into dict values(\"%s\", \"%s\");", word, mean);
printf("sql= %s\n", sql);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("sqlite3_exec failed:%s __%d__\n", errmsg, __LINE__);
return -1;
}
}
//关闭文件
fclose(fp);
//关闭数据库,释放对应的内存空间
if(sqlite3_close(db) != SQLITE_OK)
{
printf("sqlite3_close failed:%s __%d__\n", sqlite3_errmsg(db), __LINE__);
return -1;
}
printf("slqite3 sq.db close success\n");
return 0;
}
思维导图