#include <stdio.h>
#include <sqlite3.h>
int main()
{
sqlite3 *db; //声明sqlite关键结构指针
int ret = sqlite3_open("student.db", &db);
if( ret != SQLITE_OK )
{
printf ("数据库打开失败\n");
return -1;
}
char *errmsg;
const char *sq1 = "create table if not exists student(ID INTEGER, name TEXT, sex TEXT, age INTEGER, primary key(ID))";
// int sqlite3_exec(sqlite3*, const char *sql, sqlite3_callback, void *, char **errmsg );
ret = sqlite3_exec(db, sq1, NULL, NULL, &errmsg);
if( ret != SQLITE_OK )
{
printf ("exec1失败:%s\n", errmsg);
sqlite3_free(errmsg);
return -1;
}
int id = 1;
char name[20] = "liu";
char sex[10] = "M";
int age = 99;
char str[100];
sprintf (str, "insert into student values(NULL, '%s', '%s', %d)", name, sex, age);
ret = sqlite3_exec(db, str, NULL, NULL, &errmsg);
if( ret != SQLITE_OK )
{
printf ("exec2失败:%s\n", errmsg);
sqlite3_free(errmsg);
return -1;
}
sqlite3_close(db);
return 0;
}