本程序运行在已经移植了thttpd web服务器,sqlite3数据库的嵌入式linux系统上
#include <stdio.h> #include <stdlib.h> #include <sqlite3.h>
#define _DEBUG_
int main( ) { sqlite3 *db; char *zErrMsg = 0; int rc; rc = sqlite3_open("my.db", &db); //打开my.db数据库
printf("Content-type: text/html\n\n"); printf("\n"); printf("sqlite3 database\n"); printf("\n"); if (rc) { printf("Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); exit(1); } else printf("You have open a sqlite3 database named my.db\n"); char *sql="CREATE TABLE mydata(\ //建立mydada表 ID INTEGER PRIMARY KEY,\ T1 INTEGER, \ T2 INTEGER, \ Time VARCHAR(12)\ )"; sqlite3_exec(db,sql,0,0,&zErrMsg); //执行上面的sql语句
#ifdef _DEBUG_ printf("%s",zErrMsg); #endif sql="INSERT INTO \"mydata\"VALUES(1,17,18,'20090326')"; //插入第一条记录
sqlite3_exec(db,sql,0,0,&zErrMsg); sql="INSERT INTO \"mydata\"VALUES(2,19,20,'20090327')"; //插入第二条记录
sqlite3_exec(db,sql,0,0,&zErrMsg); int nrow=0,ncolumn=0; char **result; sql="SELECT * FROM mydata"; //查询所有mydata表中的所有记录
sqlite3_get_table(db,sql,&result,&nrow,&ncolumn,&zErrMsg); //获取mydata表的相关信息,包括行,列 等信息
int i; printf("row:%d column:%d\n",nrow,ncolumn); printf("the result:\n\n"); for(i=0;i<(nrow+1)*ncolumn;) { printf("\t\t%s\t\t%s\t\t%s\t\t%s\n",result[i++],result[i++],result[i++],result[i++]); } sqlite3_free_table(result); sqlite3_close(db); printf("\n"); printf("\n"); return 0; }
|