Sqlite China  
首页 | 各种语言的sqlite编程 | sqlite研究 |sqlite应用实例与杂谈 | sqlite相关下载 | SQlite论坛
当前位置 : 主页>sqlite研究>列表
SQLite数据库编程--创建数据库
来源:工友 作者:工友 时间:2007-12-21

创建数据库:

char * errMsg = NULL;
//打开一个数据库,如果改数据库不存在,则创建一个名字为databaseName的数据库文件
int rc = sqlite3_open(databaseName, &pDB);
if(rc)
{
cout
<< " Open the database " << databaseName << " failed" << endl;
}
//如果创建成功,添加表
else
{
cout
<< "create the database successful!" << endl;
//creat the table
int i;
for(i=1; i<nTableNum; i++)
{

}
//插入一个表,返回值为SQLITE_OK为成功,否则输出出错信息
//函数参数:第一个为操作数据库的指针,第二句为SQL命令字符串
//第三个参数为callback函数,这里没有用,第四个参数为callback函数
//中的第一个参数,第五个为出错信息
rc
= sqlite3_exec(pDB, "CREATE TABLE chn_to_eng(chinese QString, english QString)",
0, 0, &errMsg);
if(rc == SQLITE_OK)
cout
<< "create the chn_to_eng table successful!" << endl;
else
cout
<< errMsg << endl;
//同上,插入另一个表
rc
= sqlite3_exec(pDB, "CREATE TABLE eng_to_chn(english QString, chinese QString)",
0, 0, &errMsg);
if(rc == SQLITE_OK)
cout
<< "create the eng_to_chn table successful!" << endl;
else
cout
<< errMsg << endl;

}
、、、、、、
//往表中添加数据
char chn[]="...";
char eng[]="...";
char value[500];
//定义一条参数SQL命令,其中chn,eng为需要插入的数据
sprintf(value, "
INSERT INTO chn_to_eng(chinese, english)
VALUES('%s', '%s')", chn, eng);

//use the SQLITE C/C++ API to create and adjust a database.
rc
= sqlite3_exec(pDB,
value,
0, 0, &errMsg);
//查询一条记录
char value[500];
//定义一条查询语句,其中条件为当english为target时的中文记录
//print_result_cb为callback函数,在其中可以得到查询的结果,具体见下文
sprintf(value, "
SELECT chinese FROM eng_to_chn where english='%s' ", target);
rc
= sqlite3_exec(pDB,
value,
print_result_cb,
0, &errMsg);
if(rc == SQLITE_OK)
{
// #ifdef_debug
cout
<< "select the record successful!" << endl;
// #endif
}
else
{
// #ifdef_debug
cout
<< errMsg << endl;
// #endif
return false;
}
.......
}
//callback回调函数print_result_cb的编写,其中data为sqlite3_exec中的第四个参数,
//第二个参数是栏的数目,第三个是栏的名字,第四个为查询得到的值得。这两个函数输出所有查询到的结果
int print_result_cb(void* data, int n_columns, char** column_values,
char** column_names)
{
static
int column_names_printed = 0;
int i;
if (!column_names_printed) {
print_row(n_columns, column_names);
column_names_printed
= 1;
}
print_row(n_columns, column_values);
return 0;
}

void print_row(
int n_values, char** values)
{
int i;
for (i = 0; i < n_values; ++i) {
if (i > 0) {
printf("t");
}
printf("
%s", values[i]);

}
printf("n");
}

大致过程就是如此,具体可以参考SQLITE的API函数说明,见www.sqlite.org

void print_row(int n_values, char** values)
{
int i;
for (i = 0; i < n_values; ++i) {
if (i > 0) {
printf("t");
}
printf("
%s", values[i]);

}
printf("n");
}

大致过程就是如此,具体可以参考SQLITE的API函数说明,见www.sqlite.org

void print_row(int n_values, char** values)
...
{
int i;
for (i = 0; i < n_values; ++i) {
if (i > 0) {
printf(
"t");
}

printf(
"%s", values[i]);

}

printf(
"n");
}


大致过程就是如此,具体可以参考SQLITE的API函数说明,见www.sqlite.org

char value[500];
//定义一条参数SQL命令,其中chn,eng为需要插入的数据
sprintf(value, "INSERT INTO chn_to_eng(chinese, english)
VALUES('%s', '%s')
", chn, eng);

//use the SQLITE C/C++ API to create and adjust a database.
rc = sqlite3_exec(pDB,
value,
0, 0, &errMsg);
//查询一条记录
char value[500];
//定义一条查询语句,其中条件为当english为target时的中文记录
//print_result_cb为callback函数,在其中可以得到查询的结果,具体见下文
sprintf(value, "SELECT chinese FROM eng_to_chn where english='%s' ", target);
rc
= sqlite3_exec(pDB,
value,
print_result_cb,
0, &errMsg);
if(rc == SQLITE_OK)
...
{
// #ifdef_debug
cout << "select the record successful!" << endl;
// #endif
}

else
...
{
// #ifdef_debug
cout << errMsg << endl;
// #endif
return false;
}

.......
}
//callback回调函数print_result_cb的编写,其中data为sqlite3_exec中的第四个参数,
//第二个参数是栏的数目,第三个是栏的名字,第四个为查询得到的值得。这两个函数输出所有查询到的结果

int print_result_cb(void* data, int n_columns, char** column_values,
char** column_names)
...
{
static int column_names_printed = 0;
int i;
if (!column_names_printed) {
print_row(n_columns, column_names);
column_names_printed
= 1;
}

print_row(n_columns, column_values);
return 0;
}


void print_row(int n_values, char** values)
...
{
int i;
for (i = 0; i < n_values; ++i) {
if (i > 0) {
printf(
"t");
}

printf(
"%s", values[i]);

}

printf(
"n");
}


大致过程就是如此,具体可以参考SQLITE的API函数说明,见www.sqlite.org

void print_row(int n_values, char** values)
...
{
int i;
for (i = 0; i < n_values; ++i) {
...
{
printf(
"t");
}

printf(
"%s", values[i]);

}

printf(
"n");
}


大致过程就是如此,具体可以参考SQLITE的API函数说明,见www.sqlite.org

void print_row(int n_values, char** values)
...
{
int i;
for (i = 0; i < n_values; ++i) {
if (i > 0) {
printf(
"t");
}

printf(
"%s", values[i]);

}

printf(
"n");
}


大致过程就是如此,具体可以参考SQLITE的API函数说明,见www.sqlite.org。

char value[500];
//定义一条参数SQL命令,其中chn,eng为需要插入的数据
sprintf(value, "INSERT INTO chn_to_eng(chinese, english)
VALUES('%s', '%s')
", chn, eng);

//use the SQLITE C/C++ API to create and adjust a database.
rc = sqlite3_exec(pDB,
value,
0, 0, &errMsg);
//查询一条记录
char value[500];
//定义一条查询语句,其中条件为当english为target时的中文记录
//print_result_cb为callback函数,在其中可以得到查询的结果,具体见下文
sprintf(value, "SELECT chinese FROM eng_to_chn where english='%s' ", target);
rc
= sqlite3_exec(pDB,
value,
print_result_cb,
0, &errMsg);
if(rc == SQLITE_OK)
...
{
// #ifdef_debug
cout << "select the record successful!" << endl;
// #endif
}

else
{
// #ifdef_debug
cout << errMsg << endl;
// #endif
return false;
}

.......
}
//callback回调函数print_result_cb的编写,其中data为sqlite3_exec中的第四个参数,
//第二个参数是栏的数目,第三个是栏的名字,第四个为查询得到的值得。这两个函数输出所有查询到的结果

int print_result_cb(void* data, int n_columns, char** column_values,
char** column_names)
...
{
static int column_names_printed = 0;
int i;
if (!column_names_printed) {
print_row(n_columns, column_names);
column_names_printed
= 1;
}

print_row(n_columns, column_values);
return 0;
}


void print_row(int n_values, char** values)
...
{
int i;
for (i = 0; i < n_values; ++i) {
if (i > 0) {
printf(
"t");
}

printf(
"%s", values[i]);

}

printf(
"n");
}

大致过程就是如此,具体可以参考SQLITE的API函数说明,见www.sqlite.org
void print_row(int n_values, char** values)
{
int i;
for (i = 0; i < n_values; ++i) {
if (i > 0) {
printf(
"t");
}

printf(
"%s", values[i]);

}

printf(
"n");
}


大致过程就是如此,具体可以参考SQLITE的API函数说明,见www.sqlite.org

void print_row(int n_values, char** values)
...
{
int i;
for (i = 0; i < n_values; ++i) {
if (i > 0) {
printf(
"t");
}

printf(
"%s", values[i]);

}

printf(
"n");
}



大致过程就是如此,具体可以参考SQLITE的API函数说明,见www.sqlite.org 。

(阅读次数:
上一篇:SQLite数据库编程--数据库安装 下一篇:在.NET C#中使用sqlite
[收藏] [推荐] [评论(1条)] [返回顶部] [打印本页] [关闭窗口]
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
§最新评论
热点文章
·SQLite数据库的体系结构
·SQLite与其他数据库的速度
·SQL 语言参考资料
·SQLite语法备忘录
·sqlite 的相关调查1
·嵌入式数据库SQLite的一份
·SQLite在嵌入式Web服务器
·点评主流开源数据库的技术
·基于ARM-Linux的SQLite嵌
·SQLite与其他数据库的速度
·SQLite数据库编程--创建数
·SQL 语法手册
·SQLite Mode 数据库交互的
·SQLITE3 使用总结(3)
·XXTEA加密算法为SQLite 3.
·SQLite 第三版总览(简介)
·SQLite 第三版中的数据类
·用sqlite 执行标准 sql 语
·System.Data.Sqlite 上手
·SQLite编译安装步骤
相关文章
·SQLite Mode 数据库交互的
·SQL 语言参考资料
·SQLite在嵌入式Web服务器
·SQL 语法手册
·System.Data.Sqlite 上手
·SQLite数据库编程--创建数
·SQLite数据库编程--数据库
·SQLite在TorqueScript中的
·关于sqlite_exec回调函数
·用sqlite 执行标准 sql 语

版权Power by DedeCms   后台登陆
Copyright @ 2007