#include <stdio.h>
#include <sqlite3.h>
int main()
{
sqlite3 * db = NULL;
int result = sqlite3_open("student.db", &db);
if (result != SQLITE_OK)
{
printf ("1open student.db error!\n");
return -1;
}
char *errmsg;
result = sqlite3_exec(db, "create table test(ID INTEGER, name TEXT, sex TEXT, age INTEGER, primary key(ID))", NULL, NULL, &errmsg);
if (result != SQLITE_OK && result != 1)
{
printf ("create test table error : %d(%s)!\n", result, errmsg);
return -1;
}
char **resultp = NULL;
int nrow, ncolumn;
result = sqlite3_get_table(db, "select ID,name from test where sex='F'", &resultp, &nrow, &ncolumn, &errmsg);
if (result != SQLITE_OK)
{
printf ("select test table error : %d(%s)!\n", result, errmsg);
return -1;
}
int i;
int count = 0;
for (i = 0; i < (nrow+1)*ncolumn; i++)
{
count++;
printf ("%8s ", resultp[i]);
if (count % ncolumn == 0)
{
printf ("\n");
}
}
printf ("\n");
sqlite3_close(db);
return 0;
}
#include <sqlite3.h>
int main()
{
sqlite3 * db = NULL;
int result = sqlite3_open("student.db", &db);
if (result != SQLITE_OK)
{
printf ("1open student.db error!\n");
return -1;
}
char *errmsg;
result = sqlite3_exec(db, "create table test(ID INTEGER, name TEXT, sex TEXT, age INTEGER, primary key(ID))", NULL, NULL, &errmsg);
if (result != SQLITE_OK && result != 1)
{
printf ("create test table error : %d(%s)!\n", result, errmsg);
return -1;
}
char **resultp = NULL;
int nrow, ncolumn;
result = sqlite3_get_table(db, "select ID,name from test where sex='F'", &resultp, &nrow, &ncolumn, &errmsg);
if (result != SQLITE_OK)
{
printf ("select test table error : %d(%s)!\n", result, errmsg);
return -1;
}
int i;
int count = 0;
for (i = 0; i < (nrow+1)*ncolumn; i++)
{
count++;
printf ("%8s ", resultp[i]);
if (count % ncolumn == 0)
{
printf ("\n");
}
}
printf ("\n");
sqlite3_close(db);
return 0;
}
本文通过一个简单的C语言程序展示了如何使用SQLite3库来创建数据库、建立表,并执行查询操作。程序首先尝试打开或创建一个名为'student.db'的数据库文件,接着创建一个名为'test'的表,最后从该表中选择性别为'F'的所有记录。
853

被折叠的 条评论
为什么被折叠?



