#include
#include
#include "../sqlite3_lib/sqlite3.h"
/**
演示php调用C++进行数据操作的过程。
**/
#pragma comment(lib, "../sqlite3_lib/sqlite3.lib")
using namespace std;
static int _sql_callback(void * notused, int argc, char ** argv, char ** szColName)
{
int i;
for (i=0; i < argc; i++)
{
printf( "%s = %sn", szColName[i], argv[i] == 0 ? "NUL" : argv[i] );
}
return 0;
}
int main(int argc, char * argv[])
{
string name="";
int age=0;
string birthday="1985-05-16";
if (argc>3)
{
cout<<"参数个数是"<
name=argv[1];
age=atoi(argv[2]);
birthday=argv[3];
} else{
cout<<"没有获取足够的命令行参数,请手工输入"<
cout<<"请依次输入姓名,年龄,生日"<
cin>>name;
cin>>age;
cin>>birthday;
}
stringstream strStream;
strStream <
string s = strStream.str();
string isql="insert into users values('"+name+"',"+s+",'"+birthday+"');";
cout<
const char * sSQL1 = "create table users(userid varchar(20) PRIMARY KEY, age int, birthday datetime);";
const char * sSQL2 = "select * from users;";
sqlite3 * db = 0;
char * pErrMsg = 0;
int ret = 0;
// 连接数据库
ret = sqlite3_open("./db.db", &db);
if ( ret != SQLITE_OK )
{
fprintf(stderr, "无法打开数据库: %s", sqlite3_errmsg(db));
return(1);
}
printf("数据库连接成功!n");
// 执行建表SQL
sqlite3_exec(db, sSQL1, 0, 0, &pErrMsg );
if ( ret != SQLITE_OK )
{
fprintf(stderr, "SQL error: %sn", pErrMsg);
sqlite3_free(pErrMsg);
}
// 执行插入记录SQL
sqlite3_exec( db, isql.c_str(), 0, 0, &pErrMsg);
// 查询数据表
sqlite3_exec( db, sSQL2, _sql_callback, 0, &pErrMsg);
// 关闭数据库
sqlite3_close(db);
db = 0;
return 0;
}
|