一、简介
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/n/189331.html