#include
#include
#include
#define OK 1
#define ERROR 0
//#define int Status
/*Status insert(void); //插入
Status update(void); //修改
Status delete(void); //删除
Status query(void); //显示
*/
void insert(void)
{
sqlite3 *db;
char *errmsg;
int ret = sqlite3_open("info.db",&db);
if(SQLITE_OK != ret)
{
perror("sqlite3_open");
exit(1);
}
int id,age;
char name[20];
printf("insert : Please input:id name age\n");
scanf("%d %s %d",&id,name,&age);
char sql[1024] = {0};
sprintf(sql,"insert into stu values(%d,'%s',%d);",id,name,age);
// insert into stu values(1,'Li',18);
ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
if(SQLITE_OK != ret)
{
printf("error : %s\n",errmsg);
exit(2);
}
ret = sqlite3_close(db);
if(SQLITE_OK != ret)
{
perror("sqlite3_close");
exit(3);
}
printf("插入成功\n");
}
void update(void)
{
sqlite3 *db;
char *errmsg;
int ret = sqlite3_open("info.db",&db);
if(SQLITE_OK != ret)
{
perror("sqlite3_open");
exit(1);
}
int id;
char name[20];
printf("update : Please input:id name\n");
scanf("%d %s",&id,name);
char sql[1024] = {0};
sprintf(sql,"update stu set name = '%s' where id = %d",name,id);
ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
if(SQLITE_OK != ret)
{
perror("sqlite_exec");
printf("error : %s\n",errmsg);
exit(2);
}
ret = sqlite3_close(db);
if(SQLITE_OK != ret)
{
perror("sqlite3_close");
exit(3);
}
printf("修改成功\n");
}
void delete(void)
{
sqlite3 *db;
char *errmsg;
int ret = sqlite3_open("info.db",&db);
if(SQLITE_OK != ret)
{
perror("sqlite3_open");
exit(1);
}
int id;
printf("delete : Please input:id\n");
scanf("%d",&id);
char sql[1024] = {0};
sprintf(sql,"delete from stu where id = %d;",id);
ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
if(SQLITE_OK != ret)
{
perror("sqlite_exec");
printf("error : %s\n",errmsg);
exit(2);
}
ret = sqlite3_close(db);
if(SQLITE_OK != ret)
{
perror("sqlite3_close");
exit(3);
}
printf("删除成功\n");
}
void query(void)
{
sqlite3 *db;
char *errmsg;
char **result;
int row,column;
int ret = sqlite3_open("info.db",&db);
if(SQLITE_OK != ret)
{
perror("sqlite3_open");
exit(1);
}
char sql[1024] = "select * from stu;";
ret = sqlite3_get_table(db,sql,&result,&row,&column,&errmsg);
if(SQLITE_OK != ret)
{
printf("error : %s\n",errmsg);
exit(2);
}
printf("row = %d,column = %d\n",row,column);
int i,j;
for(i = 0;i <= row;i++)
{
for(j = 0;j < column;j++)
{
printf("%10s|",result[i*column + j]);
}
printf("\n");
}
sqlite3_free_table(result);
ret = sqlite3_close(db);
if(SQLITE_OK != ret)
{
exit(3);
}
printf("显示成功\n");
}
int main()
{
sqlite3 *db;
char *errmsg;
int sel;
int ret = sqlite3_open("info.db",&db);
if(SQLITE_OK != ret)
{
perror("sqlite3_open");
exit(1);
}
char *sql = "create table stu(id integer primary key,name text,age integer)";
ret = sqlite3_exec(db,sql,NULL,NULL,&errmsg);
if(SQLITE_OK != ret)
{
printf("error : %s\n",errmsg);
exit(2);
}
ret = sqlite3_close(db);
if(SQLITE_OK != ret)
{
exit(3);
}
while(1)
{
printf("\t************************************\n");
printf("\t\tmenu:\n");
printf("\t\t1:插入联系人\n");
printf("\t\t2:修改联系人信息\n");
printf("\t\t3:删除联系人信息\n");
printf("\t\t4:显示联系人信息\n");
printf("\t\t5:退出\n");
printf("\t************************************\n");
scanf("%d",&sel);
switch(sel)
{
case 1:
insert();
break;
case 2:
update();
break;
case 3:
delete();
break;
case 4:
query();
break;
case 5:
printf("欢迎再次光临!\n");
return 0;
}
}
}