数据库程序

来源:互联网 发布:java图形输出的编程题 编辑:程序博客网 时间:2024/05/29 16:14
#include <stdio.h>
#include <stdlib.h>


#include <sqlite3.h>




int handle_result(void *para, int column_count, char **column_value, char **column_name)
{
    printf("para = %s\n",(char *)para);
    printf("column count = %d\n",column_count);


    int i;


    for(i = 0; i < column_count; i++)
    {
        printf("%s:%s ",column_name[i],column_value[i]);
    }


    printf("\n");


    return 0;
}


int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        printf("Please input db file name!\n");
exit(1);
    }


    int i;
    int ret;
    int id;
    char name[20];
    int age;
    char *errmsg;
   
    char sql[1024];


    sqlite3 *db;
    
    ret = sqlite3_open(argv[1],&db);


    if(ret != SQLITE_OK)
    {
        perror("sqlite open error!");
exit(1);
    }
    

    sqlite3_exec(db,"create table student(id integer primary key,name text,age integer)",NULL,NULL,&errmsg);


    for(i = 0; i < 3; i++)
    {
        printf("Please input id:\n");
scanf("%d",&id);
printf("Please input name:\n");
scanf("%s",name);
printf("Please input age:\n");
scanf("%d",&age);
        
sprintf(sql,"insert into student(id,name,age) values(%d,'%s',%d)",id,name,age);
printf("%s\n",sql);
sqlite3_exec(db,sql,NULL,NULL,&errmsg);
    }
   
    
    sqlite3_exec(db,"select * from student",handle_result,"hello world",&errmsg);


    sqlite3_close(db);


    return 0;
}
1 0
原创粉丝点击