天天看點

linux c++資料庫sqlite3學習

linux下sqilte3安裝:sudo apt-get install sqlite3

1,常見的腳本操作:

QL 語言操作資料庫

.database 顯示目前資料庫

.table 顯示目前資料庫裡的表格

.q .exit 退出,

2,資料類型:

integer 整數 : int, short, long

text 文本 : string

real 浮點型: float, double

blob 二進制

//每個語句按;結束

3,常用語句

//建立資料庫在目前目錄下

sqlite3 usr.db

//建立表格

create table stu(id int, name text, age integer, score int);

//檢視表格所有内容:

select * from stu;

//查找表格裡某個内容

select * from stu where id = 1001; //id為1001的一行

select id from stu where name = ‘Tom’;//名字為Tom的id

//向表格裡增加内容:

insert into stu(id, name, age, score) values(1001, ‘Tom’, 12, 90);

//修改表格的内容

update stu set name = “Cindy” where id = 1002 and name = ‘Bob’;

//删除表格中的資料

delete from stu where id = 1001 and name = ‘Bob’;

//删除表格

drop table xiaoming;

id integer primary key(主鍵) autoincrement(自動增加)

4,sqlite3庫的下載下傳和編譯以及運用:

下載下傳庫代碼:https://download.csdn.net/download/qq_40008325/10978934

在程式運作的時候連結

建立動态庫

gcc -o libsqlite.so -fPIC --shared sqlite3.c

./a.out -->執行的時候需要連結到動态庫

//程式編譯的過程已經把靜态庫連結到了可執行檔案裡

建立靜态庫

gcc -c sqlite3.c -o sqlite3.o

ar -r sqlite3.a sqlite3.o

./a.out

編譯

連結靜态庫

g++ sqlite.cpp sqlite3.a -lpthread -ldl

自定義的檔案

連結動态庫

g++ sqlite.cpp -L ./ -lsqlite -lpthread -ldl

自定義的檔案

//如果執行的時候找不到動态庫,可以cp libsqlite.so /lib/

編譯環境的構造:

下載下傳連接配接中的代碼,按上面編譯出動态庫和靜态庫,拷貝生成的庫到相應檔案下(可以自己指定路徑)友善連接配接,而後拷貝sqlite3.h到我的項目檔案下,友善頭檔案的查找。

#include <iostream>
#include <cstdio>
#include "sqlite3.h"
using namespace std;

//把sqlite3資料庫的c語言操作形式改成一個c++的類

Sqlite sq("usr.db");
sq.exec("insert into stu values(200,'a', 32,33);");
sq.exec("select * from stu;");
class Sqlite
{
	sqlite3* pdb;
	char** buf;
	int r;
	int c;
public:
	Sqlite() {
	
	}
	~Sqlite() { }
	int exec(char* sql) {
		
	}
	int row() {
		return r;
	}

};
int main()
{
	sqlite3* pdb;
	int ok;
	ok = sqlite3_open("usr.db", &pdb);
	if (ok == SQLITE_OK) {
		cout << "打開資料庫成功" << endl;
	} else {
		cout << "打開資料庫失敗" << endl;
		return 0;
	}
	//char sql[1024];
	string sql;
	char** buf = NULL;
	int row = 0;
	int col = 0;
	sql = "insert into stu values(1005, 'Lisa', 12, 89);";
	//增删改
	ok = sqlite3_exec(pdb, sql.c_str(), NULL, NULL, NULL);
	sql = "select * from stu;";
	//查
	ok = sqlite3_get_table(pdb, sql.c_str(), &buf, &row, &col, NULL);
	if (ok == SQLITE_OK) {
		cout << "操作成功" << endl;
	} else {
		cout << "操作失敗" << endl;
		return 0;
	}
//	buf == char* []; buf指向一個一維char*數組
//	[0]  [1]    [2]   [3]     [4] ...
//	"id" "name" "age" "score" 
//	"1001" "Tom" "12"  "90"
//	"1002" "Tom" "12"  "90"
//	row = 2; col = 4; row不包含表頭
	for (int i=0; i<(row+1)*col; i++) {
		cout << buf[i] << " ";
		if ((i+1) % col == 0)
			cout << endl;
	}
	if (buf != NULL)
		sqlite3_free_table(buf); //釋放buf指向的表格

	sqlite3_close(pdb);
	return 0;
}
           

sqlite3的簡單封裝

#include <iostream>
#include "sqlite3.h"
#include <cstdio>
using namespace std;
struct Student {
	int id;
	string name;
	int age;
	int score;
};
struct Teacher {
	int id;
	string name;
	int age;
	string subject;
};
class Sqlite
{
protected:
	sqlite3* pdb;
	int r;
	int c;
	int ok;
	char** buf;
public:
	Sqlite(const char* database) : 
		r(0), c(0), buf(NULL), ok(SQLITE_ERROR) 
	{
		ok = sqlite3_open(database, &pdb);
		if (ok == SQLITE_OK) 
			cout << "打開成功" << endl;
		else
			cout << "打開失敗" << endl;
	}
	~Sqlite() {
		if (buf != NULL) {
			cout << "釋放表格" <<endl;
			sqlite3_free_table(buf);
		}
		if (is_open()) {
			sqlite3_close(pdb);
			cout << "關閉資料庫" << endl;
		}
	}
	int exec(const string& s) {
		if (! is_open())
			return SQLITE_ERROR;
		if (s[0] == 's') {
			if (buf != NULL)
				sqlite3_free_table(buf);
			return sqlite3_get_table(pdb, s.c_str(), &buf, &r, &c, NULL); 
		} else {
			return sqlite3_exec(pdb, s.c_str(), NULL, NULL, NULL);
		}
	}
	void show_table() {
		if (buf == NULL)
			return;
		for (int i=0; i<(r+1)*c; i++)
		{
			cout << buf[i] << " ";
			if ((i+1) %c == 0)
				cout << endl;
		}
	}
	bool is_open() {
		return ok == SQLITE_OK;
	}
	char** operator[](int n) {
		return buf + c*(n);
	}
};
//is-a
class StudentSqlite : public Sqlite
{
	char sql[1024];
public:
	StudentSqlite(const char* db) : Sqlite(db) { }
	void insert(const Student& s) {
		sprintf(sql, "insert into stu values(%d, '%s', %d, %d);", s.id, s.name.c_str(), s.age, s.score);
		if (exec(sql) != SQLITE_OK) {
			cout << "增加學生失敗" << endl;
		}
	}
};
//has-a
class TeacherSqlite 
{
	char sql[1024];
	Sqlite sqlite;
public:	
	TeacherSqlite(const char* db) : sqlite(db) { }
	void insert(const Teacher& t) {
		sprintf(sql, "insert into tea values(%d, '%s', %d, \'%s\');", t.id, t.name.c_str(), t.age, t.subject.c_str());
		if (sqlite.exec(sql) != SQLITE_OK) {
			cout << "增加教師失敗" << endl;
		}
	}
	void show_table() {
		sqlite.exec("select * from tea;");
		sqlite.show_table();
	}
};
           

繼續閱讀