通讯录 (纯数据库操作)

#include
#include
#include


void inquire_nocb(sqlite3 *db);


int main()
{
sqlite3 *db;
char *sql;
char *errmsg;
char sql1[100];
char name[20];
char sex[10];
int ret,age;

ret=sqlite3_open("data",&db);
if(ret != SQLITE_OK)
{
printf("open error");
exit(1);
}

sql="create table data(name, sex, age)";
ret=sqlite3_exec(db,sql,NULL,NULL,&errmsg);
if(ret != SQLITE_OK)
{
printf("creat error");
exit(1);
}

printf("please input name sex  age:\n");
scanf("%s %s %d",name,sex,&age);
sprintf(sql1,"insert into data values('%s','%s',%d);",name,sex,age);

ret=sqlite3_exec(db,sql1,NULL,NULL,&errmsg);
if(ret != SQLITE_OK)
{
printf("insert  error");
exit(1);
}

sqlite3_close(db);
}


void inquire_nocb(sqlite3 *db)
{
int nrow,ncolumn;
char **azresult;
char *sql;
char *errmsg;
int ret;
int i;

sql = "select * from data;";


ret = sqlite3_get_table(db,sql,&azresult,&nrow,&ncolumn,&errmsg);


if(ret != SQLITE_OK)
{
printf("get table error:%s",errmsg);
exit(-1);
}


printf("nrow = %d,column = %d\n",nrow,ncolumn);


for(i = ncolumn;i < (nrow + 1) * ncolumn;i++)
{
printf("%10s",azresult[i]);
if((i + 1) % ncolumn == 0)
{
printf("\n");
}
}
sqlite3_free_table(azresult);
}

你可能感兴趣的:(通讯录 (纯数据库操作))