程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> SQLite C/C++ 教程

SQLite C/C++ 教程

編輯:C++入門知識

2 C/C++ Interface APIs

3連接到數據庫

4創建表

5插入操作

6更新操作

7刪除操作

 

 

 

 

API 

sqlite3_open(const char *filename, sqlite3 **ppDb)

 

 

 

sqlite3_exec(sqlite3*,const char *sql, sqlite_callback, void *data, char **errmsg)

 

 

 

sqlite3_close(sqlite3*)

 

 

 

以下代碼要求提示:

    每一例子創建的數據庫,都是基於前一個數據庫創建的。否則以下代碼均不能運行!!

#include <stdlib.h>

#include <sqlite3.h>

 

int main(int argc, char* argv[])

{

sqlite3 *db;

char *zErrMsg = 0;

int rc;

 

rc = sqlite3_open("test.db", &db);

 

if( rc ){

fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));

exit(0);

}else{

fprintf(stderr, "Opened database successfully\n");

}

sqlite3_close(db);

}

-rwxr-xr-x. 1 root root 7383 May  8 02:06 a.out
-rw-r--r--. 1 root root  323 May  8 02:05 test.c
-rw-r--r--. 1 root root    0 May  8 02:06 test.db

 

 

 

 

#include <stdlib.h>

#include <sqlite3.h>

 

static int callback(void *NotUsed, int argc, char **argv, char **azColName){

int i;

for(i=0; i<argc; i++){

printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");

}

printf("\n");

return 0;

}

 

int main(int argc, char* argv[])

{

sqlite3 *db;

char *zErrMsg = 0;

int rc;

char *sql;

 

/* Open database */

rc = sqlite3_open("test.db", &db);

if( rc ){

fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));

exit(0);

}else{

fprintf(stdout, "Opened database successfully\n");

}

 

/*** 產生SQL語句 ***/

sql = "CREATE TABLE COMPANY(" \

"ID INT PRIMARY KEY NOT NULL," \

"NAME TEXT NOT NULL," \

"AGE INT NOT NULL," \

"ADDRESS CHAR(50)," \

"SALARY REAL );";

 

/*** 執行SQLite語句***/

rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);

if( rc != SQLITE_OK ){

fprintf(stderr, "SQL error: %s\n", zErrMsg);

sqlite3_free(zErrMsg);

}else{

fprintf(stdout, "Table created successfully\n");

}

sqlite3_close(db);

return 0;

}

-rwxr-xr-x. 1 root root 9567 May 8 02:31 a.out
-rw-r--r--. 1 root root 1207 May  8 02:31 test.c
-rw-r--r--. 1 root root 3072 May  8 02:31 test.db

 

 

#include <stdlib.h>

#include <sqlite3.h>

 

static int callback(void *NotUsed, int argc, char **argv, char **azColName){

int i;

for(i=0; i<argc; i++){

printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");

}

printf("\n");

return 0;

}

 

int main(int argc, char* argv[])

{

sqlite3 *db;

char *zErrMsg = 0;

int rc;

char *sql;

 

/* Open database */

rc = sqlite3_open("test.db", &db);

if( rc ){

fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));

exit(0);

}else{

fprintf(stderr, "Opened database successfully\n");

}

 

/* Create SQL statement */

sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "\

"VALUES (1, 'Paul', 32, 'California', 20000.00 ); "\

"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "\

"VALUES (2, 'Allen', 25, 'Texas', 15000.00 );      "\

"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "\

"VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );     "\

"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "\

"VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 ); ";

 

/* Execute SQL statement */

rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);

if( rc != SQLITE_OK ){

fprintf(stderr, "SQL error: %s\n", zErrMsg);

sqlite3_free(zErrMsg);

}else{

fprintf(stdout, "Records created successfully\n");

}

sqlite3_close(db);

return 0;

}

Opened database successfully
Records created successfully

 

 

 

 

 

 

 

 

 

 

 

#include <stdlib.h>

#include <sqlite3.h>

static int callback(void *data, int argc, char **argv, char **azColName){

int i;

fprintf(stderr, "%s: ", (const char*)data);

for(i=0; i<argc; i++){

printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");

}

printf("\n");

return 0;

}

int main(int argc, char* argv[])

{

sqlite3 *db;

char *zErrMsg = 0;

int rc;

char *sql;

const char* data = "Callback function called";

 

/* Open database */

rc = sqlite3_open("test.db", &db);

if( rc ){

fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));

exit(0);

}else{

fprintf(stderr, "Opened database successfully\n");

}

 

/* Create merged SQL statement */

sql = "UPDATE COMPANY set SALARY = 25000.00 where ID=1; " \

"SELECT * from COMPANY";

 

/* Execute SQL statement */

rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);

if( rc != SQLITE_OK ){

fprintf(stderr, "SQL error: %s\n", zErrMsg);

sqlite3_free(zErrMsg);

}else{

fprintf(stdout, "Operation done successfully\n");

}

sqlite3_close(db);

return 0;

}

 

#include <stdlib.h>

#include <sqlite3.h>

 

static int callback(void *data, int argc, char **argv, char **azColName){

int i;

fprintf(stderr, "%s: ", (const char*)data);

for(i=0; i<argc; i++){

printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");

}

printf("\n");

return 0;

}

 

int main(int argc, char* argv[])

{

sqlite3 *db;

char *zErrMsg = 0;

int rc;

char *sql;

const char* data = "Callback function called";

 

/* Open database */

rc = sqlite3_open("test.db", &db);

if( rc ){

fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));

exit(0);

}else{

fprintf(stderr, "Opened database successfully\n");

}

 

/* Create merged SQL statement */

sql = "DELETE from COMPANY where ID=2; " \

"SELECT * from COMPANY";

 

/* Execute SQL statement */

rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);

if( rc != SQLITE_OK ){

fprintf(stderr, "SQL error: %s\n", zErrMsg);

sqlite3_free(zErrMsg);

}else{

fprintf(stdout, "Operation done successfully\n");

}

sqlite3_close(db);

return 0;

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved