C++ 是一种通用的编程语言,可以使用不同的库和驱动程序来连接各种数据库。以下是一些示例代码,演示如何使用 C++ 连接 SQL Server、MySQL、Oracle、ACCESS、SQLite 和 PostgreSQL、MongoDB 数据库。
要使用 C++ 连接 SQL Server 数据库,可以使用 Microsoft 的 ADODB 库。以下是一个示例代码:- #include <iostream>
- #import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "EndOfFile")
- int main() {
- CoInitialize(NULL); // 初始化 COM 库
- _ConnectionPtr pConnection("ADODB.Connection"); // 创建 Connection 对象
- _bstr_t strConnect = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=MyDatabase;User ID=sa;Password=123456"; // 连接字符串
- pConnection->Open(strConnect, NULL, NULL, NULL); // 连接数据库
- if (pConnection->State) {
- _CommandPtr pCommand("ADODB.Command"); // 创建 Command 对象
- _bstr_t strSQL = "SELECT * FROM MyTable"; // SQL 查询语句
- pCommand->ActiveConnection = pConnection; // 设置连接对象
- pCommand->CommandText = strSQL; // 设置 SQL 语句
- _RecordsetPtr pRecordset("ADODB.Recordset"); // 创建 Recordset 对象
- pRecordset->Open(pCommand.GetInterfacePtr(), _variant_t((IDispatch *) pConnection, true), adOpenUnspecified, adLockUnspecified, -1); // 执行查询并返回结果集
- while (!pRecordset->EndOfFile) { // 遍历结果集
- // 处理数据
- }
- }
- pConnection->Close(); // 关闭连接
- CoUninitialize(); // 关闭 COM 库
- return 0;
- }
复制代码 要使用 C++ 连接 MySQL 数据库,可以使用 MySQL Connector/C++ 库。以下是一个示例代码:- #include <mysql_driver.h>
- #include <mysql_connection.h>
- #include <cppconn/driver.h>
- #include <cppconn/exception.h>
- #include <cppconn/resultset.h>
- #include <cppconn/statement.h>
- int main() {
- sql::mysql::MySQL_Driver *driver;
- sql::Connection *con;
- sql::Statement *stmt;
- sql::ResultSet *res;
- driver = sql::mysql::get_mysql_driver_instance(); // 获取 MySQL 驱动程序实例
- con = driver->connect("tcp://localhost:3306", "user", "password"); // 连接数据库
- stmt = con->createStatement(); // 创建 Statement 对象
- res = stmt->executeQuery("SELECT * FROM MyTable"); // 执行查询并返回结果集
- while (res->next()) { // 遍历结果集
- // 处理数据
- }
- delete res; // 删除结果集对象
- delete stmt; // 删除 Statement 对象
- delete con; // 删除连接对象
- return 0;
- }
复制代码 要使用 C++ 连接 Oracle 数据库,可以使用 Oracle 提供的 ODBC 驱动程序。以下是一个示例代码:- #include <iostream>
- #include <windows.h>
- #include <occi.h>
- using namespace oracle::occi;
- int main() {
- Environment *env = Environment::createEnvironment(); // 创建 OCCI 环境
- Connection *conn = env->createConnection("DRIVER={Oracle ODBC Driver};SERVER=localhost;DATABASE=MyDatabase;UID=user;PWD=password"); // 连接数据库
- if (conn->isValid()) {
- Statement *stmt = conn->createStatement("SELECT * FROM MyTable"); // 创建 Statement 对象
- ResultSet *res = stmt->executeQuery(); // 执行查询并返回结果集
- while (res->next()) { // 遍历结果集
- // 处理数据
- }
- }
- conn->close(); // 关闭连接
- env->terminate(); // 关闭 OCCI 环境
- return 0;
- }
复制代码 要使用 C++ 连接 Access 数据库,可以使用 Microsoft 的 ADODB 库。以下是一个示例代码:- #include <iostream>
- #import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF", "EndOfFile")
- int main() {
- CoInitialize(NULL); // 初始化 COM 库
- _ConnectionPtr pConnection("ADODB.Connection"); // 创建 Connection 对象
- _bstr_t strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\MyDatabase.mdb;Persist Security Info=False"; // 连接字符串
- pConnection->Open(strConnect, "", "", adConnectUnspecified); // 连接数据库
- if (pConnection->State) {
- _CommandPtr pCommand("ADODB.Command"); // 创建 Command 对象
- _bstr_t strSQL = "SELECT * FROM MyTable"; // SQL 查询语句
- pCommand->ActiveConnection = pConnection; // 设置连接对象
- pCommand->CommandText = strSQL; // 设置 SQL 语句
- _RecordsetPtr pRecordset("ADODB.Recordset"); // 创建 Recordset 对象
- pRecordset->Open(pCommand.GetInterfacePtr(), _variant_t((IDispatch *) pConnection, true), adOpenUnspecified, adLockUnspecified, -1); // 执行查询并返回结果集
- while (!pRecordset->EndOfFile) { // 遍历结果集
- // 处理数据
- }
- }
- pConnection->Close(); // 关闭连接
- CoUninitialize(); // 关闭 COM 库
- return 0;
- }
复制代码 要使用 C++ 连接 SQLite 数据库,可以使用SQLite C++库。以下是一个示例代码:- #include <iostream>
- #include <sqlite3.h>
- #include <cassert>
- int main() {
- sqlite3* db;
- int rc;
- std::string sql;
- // 打开数据库
- rc = sqlite3_open("MyDatabase.db", &db);
- assert(rc == SQLITE_OK);
- // 创建表格
- sql = "CREATE TABLE IF NOT EXISTS MyTable(id INTEGER PRIMARY KEY, name TEXT);";
- rc = sqlite3_exec(db, sql.c_str(), NULL, NULL, NULL);
- assert(rc == SQLITE_OK);
- // 插入数据
- sql = "INSERT INTO MyTable(name) VALUES('hello');";
- rc = sqlite3_exec(db, sql.c_str(), NULL, NULL, NULL);
- assert(rc == SQLITE_OK);
- // 查询数据
- sql = "SELECT * FROM MyTable;";
- rc = sqlite3_exec(db, sql.c_str(), callback, 0, 0);
- assert(rc == SQLITE_OK);
- // 关闭数据库
- sqlite3_close(db);
- return 0;
- }
复制代码 要使用 C++ 连接 PostgreSQL 数据库,可以使用 PostgreSQL C++ 驱动程序。以下是一个示例代码:
[code]#include #include #include void callback(void* arg, int argc, char** argv, char** cols) { for (int i = 0; i < argc; i++) { std::cout |