#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <errno.h>
sqlite3 *db;
int insert_info(void)
{
int ret;
char *sql;
int num,chinese,english,match;
char *name;
float aver;
name = malloc(10);
scanf("%d,%d,%d,%d",&num,&chinese,&english,&match);
scanf("%s",name);
aver = ((float)(chinese + english + match))/3;
sql = sqlite3_mprintf("insert into t(num,name,chinese,english,match,aver) values(%d,%Q,%d,%d,%d,%f)",num,name,chinese,english,match,aver);
printf("%s\n",sql);
ret = sqlite3_exec(db,sql,NULL,NULL,NULL);
sqlite3_free(sql);
if(ret != SQLITE_OK)
{
perror("insert fail!\n");
free(name);
//
sqlite3_free(sql);
return 0;
}
free(name);
//
sqlite3_free(sql);
return 0;
}
int main(int argc , char *argv[])
{
int fp;
char *sql;
int ret;
fp = sqlite3_open("student.db",&db);
if(fp != SQLITE_OK)
{
printf("create fail!\n");
}
sql = sqlite3_mprintf("create table t(num integer primary key,name text,chinese integer,english integer,match integer,aver float)");
ret = sqlite3_exec(db,sql,NULL,NULL,NULL);
sqlite3_free(sql);
if(ret != SQLITE_OK)
{
printf("create tabe t fail!\n");
return 0;
}
insert_info();
return 0;
}