?? sqlshujvku.txt
字號:
char * errMsg = NULL;
//打開一個數(shù)據(jù)庫,如果改數(shù)據(jù)庫不存在,則創(chuàng)建一個名字為databaseName的數(shù)據(jù)庫文件
int rc = sqlite3_open(databaseName, &pDB);
if(rc)
{
cout << " Open the database " << databaseName << " failed" << endl;
}
//如果創(chuàng)建成功,添加表
else
{
cout << "create the database successful!" << endl;
//creat the table
int i;
for(i=1; i<nTableNum; i++)
{
}
//插入一個表,返回值為SQLITE_OK為成功,否則輸出出錯信息
//函數(shù)參數(shù):第一個為操作數(shù)據(jù)庫的指針,第二句為SQL命令字符串
//第三個參數(shù)為callback函數(shù),這里沒有用,第四個參數(shù)為callback函數(shù)
//中的第一個參數(shù),第五個為出錯信息
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;
}
、、、、、、
//往表中添加數(shù)據(jù)
char chn[]="...";
char eng[]="...";
char value[500];
//定義一條參數(shù)SQL命令,其中chn,eng為需要插入的數(shù)據(jù)
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];
//定義一條查詢語句,其中條件為當(dāng)english為target時的中文記錄
//print_result_cb為callback函數(shù),在其中可以得到查詢的結(jié)果,具體見下文
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回調(diào)函數(shù)print_result_cb的編寫,其中data為sqlite3_exec中的第四個參數(shù), //第二個參數(shù)是欄的數(shù)目,第三個是欄的名字,第四個為查詢得到的值得。這兩個函數(shù)輸出所有查詢到的結(jié)果
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");
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -