#include <stdio.h>
#include <sqlite3.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<sys/shm.h>
#include<sys/sem.h>
#include<time.h>
#include <sys/ioctl.h>
#define addr "/home/ubuntu/base/database.db"
void bianli(char *buf_table)
{
sqlite3 *mydb = NULL;
sqlite3_open(addr,&mydb);
char **result;
int row,column;
char *buf1=malloc(1024);
snprintf(buf1, 1024, "select *from %s;", buf_table);
sqlite3_get_table(mydb,buf1,&result, &row,&column,NULL);
for(int i = 0;i <(row+1)*column;i++)
{
printf("%-18s",result[i]);
if((i+1)%column == 0)
{
printf("\n");
}
}
}
void add_stdudent(char *buf_table)
{
sqlite3 *mydb = NULL;
sqlite3_open(addr,&mydb);
char **result;
int row,column;
char *buf=malloc(100);
char *buf1=malloc(1024);
char *buf_lie=malloc(100);
printf("输入要插入的列:");
fgets(buf_lie,100,stdin);
int len = strlen(buf_lie);
buf_lie[len-1] = '\0';
printf("输入插入的内容:");
fgets(buf,100,stdin);
len = strlen(buf);
buf[len-1] = '\0';
snprintf(buf1, 1024, "insert into %s(%s) values(%s);",buf_table,buf_lie,buf);
sqlite3_get_table(mydb,buf1,&result, &row,&column,NULL);
}
void delete_student(char *buf_table)
{
sqlite3 *mydb = NULL;
sqlite3_open(addr,&mydb);
char **result;
int row,column;
char *buf=malloc(100);
printf("根据名字删除猫猫:");
fgets(buf,100,stdin);
int len = strlen(buf);
buf[len-1] = '\0';
char *buf1=malloc(1024);
snprintf(buf1, 1024, "delete from %s where name=\"%s\";",buf_table,buf);
sqlite3_get_table(mydb,buf1,&result, &row,&column,NULL);
}
void change_student(char *buf_table)
{
sqlite3 *mydb = NULL;
sqlite3_open(addr,&mydb);
char **result;
int row,column;
int len;
char *buf_name=malloc(100);
char *buf_lie=malloc(100);
char *buf_zhi=malloc(100);
printf("输入要修改对象的名字:");
fgets(buf_name,100,stdin);
len = strlen(buf_name);
buf_name[len-1] = '\0';
printf("输入要改变的列名:");
fgets(buf_lie,100,stdin);
len = strlen(buf_lie);
buf_lie[len-1] = '\0';
printf("输入改变的列名的值:");
fgets(buf_zhi,100,stdin);
len = strlen(buf_zhi);
buf_zhi[len-1] = '\0';
char *buf1=malloc(1024);
snprintf(buf1, 1024, "update %s set %s=\"%s\" where name=\"%s\";",buf_table,buf_lie,buf_zhi,buf_name);
sqlite3_get_table(mydb,buf1,&result, &row,&column,NULL);
}
void get_student(char *buf_table)
{
sqlite3 *mydb = NULL;
sqlite3_open(addr,&mydb);
char **result;
int row,column;
int len;
char *buf_name=malloc(100);
char *buf_lie=malloc(100);
printf("输入获取对象的名字:");
fgets(buf_name,100,stdin);
len = strlen(buf_name);
buf_name[len-1] = '\0';
printf("输入要获取的列名:");
fgets(buf_lie,100,stdin);
len = strlen(buf_lie);
buf_lie[len-1] = '\0';
char *buf1=malloc(1024);
snprintf(buf1, 1024, "select %s from %s where name=\"%s\";",buf_lie,buf_table,buf_name);
//printf("%s",buf1);
sqlite3_get_table(mydb,buf1,&result, &row,&column,NULL);
for(int i = 0;i <(row+1)*column;i++)
{
printf("%-18s",result[i]);
if((i+1)%column == 0)
{
printf("\n");
}
}
}
int main(int argc,char **argv)
{
printf("1.遍历\n");
printf("2.添加\n");
printf("3.删除(名字)\n");
printf("4.更改\n");
printf("5.查询\n");
printf("输入要操作的表名:");
char *buf_table=malloc(100);
fgets(buf_table,100,stdin);
int len = strlen(buf_table);
buf_table[len-1] = '\0';
if(strcmp(buf_table,"cat")!=0 && strcmp(buf_table,"dog")!=0)
{
printf("没有这个表\n");
return -1;
}
printf("choose the num to choose func:\n");
int a;
while(1)
{
printf("输入功能:");
scanf("%d",&a);
getchar();
switch(a)
{
case 1:
bianli(buf_table);
break;
case 2:
add_stdudent(buf_table);
break;
case 3:
delete_student(buf_table);
break;
case 4:
change_student(buf_table);
break;
case 5:
get_student(buf_table);
break;
case 6:
return 0;
}
}
//switch(argv[1])
//{
//}
//add_stdudent();
//change_student();
//get_student();
}