一、簡介
c++access是一個開源的,基於c++語言的資料庫管理系統。它採用高效的B+樹索引結構,支持快速的查詢、添加、刪除和修改操作。它還提供了多種介面,包括命令行介面、ODBC介面、Java介面等,方便開發人員進行資料庫開發。
二、安裝和配置
1、安裝
git clone https://github.com/zzh8829/cpp_db.git cd cpp_db mkdir build && cd build cmake .. make
2、配置
配置文件為config.ini,包括以下參數:
- db_path:指定資料庫文件的路徑(默認值:./dbfile)
- cache_size:指定緩存區大小(默認值:1000)
- page_size:指定頁大小(默認值:4096)
示例代碼:
#include <cpp_db.hpp> cpp_db::connection conn{"sqlite3:dbfile=test.db"}; conn.execute("create table person (id integer primary key, first_name varchar(50), last_name varchar(50))"); conn.execute("insert into person (first_name, last_name) values (?, ?)", {"John", "Doe"}); auto res = conn.execute("select * from person"); for(auto row: res) { std::cout << row["id"].as<int>() << " " << row["first_name"].as<std::string>() << " " << row["last_name"].as<std::string>() << std::endl; }
三、數據類型
c++access支持的數據類型包括:
- 整型(int)
- 字元型(char)
- 字元串型(string)
- 浮點型(float)
- 雙精度浮點型(double)
- 布爾型(bool)
示例代碼:
#include <cpp_db.hpp> cpp_db::connection conn{"sqlite3:dbfile=test.db"}; conn.execute("create table person (id integer primary key, first_name varchar(50), last_name varchar(50), age integer, height float, married bool)"); conn.execute("insert into person (first_name, last_name, age, height, married) values (?, ?, ?, ?, ?)", {"John", "Doe", 30, 1.78, true}); auto res = conn.execute("select * from person"); for(auto row: res) { std::cout << row["id"].as<int>() << " " << row["first_name"].as<std::string>() << " " << row["last_name"].as<std::string>() << " " << row["age"].as<int>() << " " << row["height"].as<double>() << " " << row["married"].as<bool>() << std::endl; }
四、查詢語言
c++access支持標準的SQL查詢語言,包括以下關鍵詞:
- select:查詢
- from:從哪個表中查詢
- where:查詢條件
- order by:排序
- limit:限制查詢結果行數
示例代碼:
#include <cpp_db.hpp> cpp_db::connection conn{"sqlite3:dbfile=test.db"}; conn.execute("create table person (id integer primary key, first_name varchar(50), last_name varchar(50), age integer, height float, married bool)"); conn.execute("insert into person (first_name, last_name, age, height, married) values (?, ?, ?, ?, ?)", {"John", "Doe", 30, 1.78, true}); conn.execute("insert into person (first_name, last_name, age, height, married) values (?, ?, ?, ?, ?)", {"Jane", "Doe", 25, 1.60, false}); auto res = conn.execute("select first_name, last_name, age, height, married from person where age > ? order by age desc limit 1", {20}); for(auto row: res) { std::cout << row["first_name"].as<std::string>() << " " << row["last_name"].as<std::string>() << " " << row["age"].as<int>() << " " << row["height"].as<double>() << " " << row["married"].as<bool>() << std::endl; }
五、事務處理
事務是指對資料庫進行一系列操作的過程,這些操作是一個不可分割的整體,或者全部執行,或者全部不執行。c++access支持事務處理,保證資料庫的一致性。
示例代碼:
#include <cpp_db.hpp> cpp_db::connection conn{"sqlite3:dbfile=test.db"}; cpp_db::transaction(trans, conn); trans.execute("create table person (id integer primary key, first_name varchar(50), last_name varchar(50), age integer, height float, married bool)"); trans.execute("insert into person (first_name, last_name, age, height, married) values (?, ?, ?, ?, ?)", {"John", "Doe", 30, 1.78, true}); trans.rollback();
六、ODBC介面
ODBC是一種標準的資料庫訪問介面,可以通過ODBC介面連接到多種不同的資料庫。c++access提供了ODBC介面,可以方便地連接到其他資料庫系統。
示例代碼:
#include <cpp_db.hpp> cpp_db::connection conn{"odbc:DSN=test"}; conn.execute("create table person (id integer primary key, first_name varchar(50), last_name varchar(50))"); conn.execute("insert into person (first_name, last_name) values (?, ?)", {"John", "Doe"}); auto res = conn.execute("select * from person"); for(auto row: res) { std::cout << row["id"].as<int>() << " " << row["first_name"].as<std::string>() << " " << row["last_name"].as<std::string>() << std::endl; }
七、Java介面
c++access還提供了Java介面,可以方便地在Java應用程序中訪問資料庫。
示例代碼:
import java.sql.*; Connection conn = DriverManager.getConnection("jdbc:cpp_db:sqlite3:dbfile=test.db"); Statement stmt = conn.createStatement(); stmt.executeUpdate("create table person (id integer primary key, first_name varchar(50), last_name varchar(50))"); stmt.executeUpdate("insert into person (first_name, last_name) values ('John', 'Doe')"); ResultSet rs = stmt.executeQuery("select * from person"); while(rs.next()) { System.out.println(rs.getInt("id") + " " + rs.getString("first_name") + " " + rs.getString("last_name")); }
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/189331.html