自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

實(shí)例講解C++連接各種數(shù)據(jù)庫,包含SQL Server、MySQL、Oracle

數(shù)據(jù)庫
C++ 是一種通用的編程語言,可以使用不同的庫和驅(qū)動(dòng)程序來連接各種數(shù)據(jù)庫。以下是一些示例代碼,演示如何使用 C++ 連接 SQL Server、MySQL、Oracle、ACCESS、SQLite 和 PostgreSQL、MongoDB 數(shù)據(jù)庫。

連接 SQL Server 數(shù)據(jù)庫

要使用 C++ 連接 SQL Server 數(shù)據(jù)庫,可以使用 Microsoft 的 ADODB 庫。以下是一個(gè)示例代碼:

#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"); // 創(chuàng)建 Connection 對(duì)象
    _bstr_t strConnect = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=MyDatabase;User ID=sa;Password=123456"; // 連接字符串
    pConnection->Open(strConnect, NULL, NULL, NULL); // 連接數(shù)據(jù)庫

    if (pConnection->State) {
        _CommandPtr pCommand("ADODB.Command"); // 創(chuàng)建 Command 對(duì)象
        _bstr_t strSQL = "SELECT * FROM MyTable"; // SQL 查詢語句
        pCommand->ActiveConnection = pConnection; // 設(shè)置連接對(duì)象
        pCommand->CommandText = strSQL; // 設(shè)置 SQL 語句
        _RecordsetPtr pRecordset("ADODB.Recordset"); // 創(chuàng)建 Recordset 對(duì)象
        pRecordset->Open(pCommand.GetInterfacePtr(), _variant_t((IDispatch *) pConnection, true), adOpenUnspecified, adLockUnspecified, -1); // 執(zhí)行查詢并返回結(jié)果集

        while (!pRecordset->EndOfFile) { // 遍歷結(jié)果集
            // 處理數(shù)據(jù)
        }
    }

    pConnection->Close(); // 關(guān)閉連接
    CoUninitialize(); // 關(guān)閉 COM 庫
    return 0;
}

連接 MySQL 數(shù)據(jù)庫

要使用 C++ 連接 MySQL 數(shù)據(jù)庫,可以使用 MySQL Connector/C++ 庫。以下是一個(gè)示例代碼:

#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 驅(qū)動(dòng)程序?qū)嵗?    con = driver->connect("tcp://localhost:3306", "user", "password"); // 連接數(shù)據(jù)庫
    stmt = con->createStatement(); // 創(chuàng)建 Statement 對(duì)象
    res = stmt->executeQuery("SELECT * FROM MyTable"); // 執(zhí)行查詢并返回結(jié)果集

    while (res->next()) { // 遍歷結(jié)果集
        // 處理數(shù)據(jù)
    }

    delete res; // 刪除結(jié)果集對(duì)象
    delete stmt; // 刪除 Statement 對(duì)象
    delete con; // 刪除連接對(duì)象
    return 0;
}

連接 Oracle 數(shù)據(jù)庫

要使用 C++ 連接 Oracle 數(shù)據(jù)庫,可以使用 Oracle 提供的 ODBC 驅(qū)動(dòng)程序。以下是一個(gè)示例代碼:

#include <iostream>
#include <windows.h>
#include <occi.h>

using namespace oracle::occi;

int main() {
    Environment *env = Environment::createEnvironment(); // 創(chuàng)建 OCCI 環(huán)境
    Connection *conn = env->createConnection("DRIVER={Oracle ODBC Driver};SERVER=localhost;DATABASE=MyDatabase;UID=user;PWD=password"); // 連接數(shù)據(jù)庫

    if (conn->isValid()) {
        Statement *stmt = conn->createStatement("SELECT * FROM MyTable"); // 創(chuàng)建 Statement 對(duì)象
        ResultSet *res = stmt->executeQuery(); // 執(zhí)行查詢并返回結(jié)果集

        while (res->next()) { // 遍歷結(jié)果集
            // 處理數(shù)據(jù)
        }
    }

    conn->close(); // 關(guān)閉連接
    env->terminate(); // 關(guān)閉 OCCI 環(huán)境
    return 0;
}

連接 Access 數(shù)據(jù)庫

要使用 C++ 連接 Access 數(shù)據(jù)庫,可以使用 Microsoft 的 ADODB 庫。以下是一個(gè)示例代碼:

#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"); // 創(chuàng)建 Connection 對(duì)象
    _bstr_t strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\MyDatabase.mdb;Persist Security Info=False"; // 連接字符串
    pConnection->Open(strConnect, "", "", adConnectUnspecified); // 連接數(shù)據(jù)庫

    if (pConnection->State) {
        _CommandPtr pCommand("ADODB.Command"); // 創(chuàng)建 Command 對(duì)象
        _bstr_t strSQL = "SELECT * FROM MyTable"; // SQL 查詢語句
        pCommand->ActiveConnection = pConnection; // 設(shè)置連接對(duì)象
        pCommand->CommandText = strSQL; // 設(shè)置 SQL 語句
        _RecordsetPtr pRecordset("ADODB.Recordset"); // 創(chuàng)建 Recordset 對(duì)象
        pRecordset->Open(pCommand.GetInterfacePtr(), _variant_t((IDispatch *) pConnection, true), adOpenUnspecified, adLockUnspecified, -1); // 執(zhí)行查詢并返回結(jié)果集

        while (!pRecordset->EndOfFile) { // 遍歷結(jié)果集
            // 處理數(shù)據(jù)
        }
    }

    pConnection->Close(); // 關(guān)閉連接
    CoUninitialize(); // 關(guān)閉 COM 庫
    return 0;
}

連接 SQLite 數(shù)據(jù)庫

要使用 C++ 連接 SQLite 數(shù)據(jù)庫,可以使用SQLite C++庫。以下是一個(gè)示例代碼:

#include <iostream>
#include <sqlite3.h>
#include <cassert>

int main() {
    sqlite3* db;
    int rc;
    std::string sql;

    // 打開數(shù)據(jù)庫
    rc = sqlite3_open("MyDatabase.db", &db);
    assert(rc == SQLITE_OK);

    // 創(chuàng)建表格
    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);

    // 插入數(shù)據(jù)
    sql = "INSERT INTO MyTable(name) VALUES('hello');";
    rc = sqlite3_exec(db, sql.c_str(), NULL, NULL, NULL);
    assert(rc == SQLITE_OK);

    // 查詢數(shù)據(jù)
    sql = "SELECT * FROM MyTable;";
    rc = sqlite3_exec(db, sql.c_str(), callback, 0, 0);
    assert(rc == SQLITE_OK);

    // 關(guān)閉數(shù)據(jù)庫
    sqlite3_close(db);
    return 0;
}

連接 PostgreSQL 數(shù)據(jù)庫

要使用 C++ 連接 PostgreSQL 數(shù)據(jù)庫,可以使用 PostgreSQL C++ 驅(qū)動(dòng)程序。以下是一個(gè)示例代碼:

#include <iostream>
#include <postgresql/libpq-fe.h>
#include <cassert>

void callback(void* arg, int argc, char** argv, char** cols) {
    for (int i = 0; i < argc; i++) {
        std::cout << cols[i] << ": " << argv[i] << std::endl;
    }
}

int main() {
    PGconn* conn = PQconnectdb("host=localhost dbname=MyDatabase user=user password=password");
    assert(PQstatus(conn) == CONNECTION_OK);

    // 執(zhí)行查詢
    PGresult* res = PQexec(conn, "SELECT * FROM MyTable");
    assert(PQresultStatus(res) == PGRES_TUPLES_OK);

    // 遍歷結(jié)果集
    for (int i = 0; i < PQntuples(res); i++) {
        callback(NULL, PQnfields(res), PQgetvalue(res, i), PQgetisnull(res, i));
    }

    // 關(guān)閉連接
    PQfinish(conn);
    return 0;
}

連接MongoDB數(shù)據(jù)庫的示例:

#include <iostream>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <bsoncxx/json.hpp>
#include <bsoncxx/types.hpp>

int main() {
    mongocxx::instance instance{};
    mongocxx::client conn{mongocxx::uri{"mongodb://localhost:27017"}};

    // 連接到數(shù)據(jù)庫
    mongocxx::database db = conn["MyDatabase"];

    // 創(chuàng)建文檔
    bsoncxx::builder::stream::document doc{};
    doc << "name" << "John Doe"
        << "age" << 30
        << "email" << "johndoe@example.com";

    // 插入文檔到集合
    db["MyCollection"].insert(doc.view());

    // 查詢文檔
    mongocxx::cursor cursor = db["MyCollection"].find({});
    while (cursor) {
        bsoncxx::document::view doc = cursor->view();
        std::cout << doc["name"].get_string() << std::endl;
        std::cout << doc["age"].get_int32() << std::endl;
        std::cout << doc["email"].get_string() << std::endl;
        cursor++;
    }

    return 0;
}

這個(gè)示例使用了MongoDB C++驅(qū)動(dòng)程序來連接到MongoDB數(shù)據(jù)庫,創(chuàng)建文檔并將其插入到集合中,然后查詢并打印文檔的內(nèi)容。

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2011-05-18 10:26:34

java

2011-05-26 09:27:59

JDBC連接數(shù)據(jù)庫

2019-12-25 14:55:35

數(shù)據(jù)庫MySQLOracle

2009-06-30 14:37:08

數(shù)據(jù)庫JSP

2017-06-29 14:12:13

SQL ServerMysqlOracle

2009-08-03 14:17:18

C#連接AccessC#連接SQL Ser

2010-10-26 15:54:02

連接oracle數(shù)據(jù)庫

2010-02-05 09:23:38

C++連接SQL數(shù)據(jù)庫

2010-06-12 15:53:22

MySQL數(shù)據(jù)庫

2011-04-26 10:37:04

C++MySQL

2011-04-26 10:24:27

C++MySQL

2010-02-02 17:27:16

C++連接MySqL數(shù)

2011-03-08 08:59:01

SQL Server數(shù)數(shù)據(jù)移動(dòng)

2010-04-08 18:45:35

Oracle數(shù)據(jù)庫

2011-07-18 10:01:59

C# ADO.NETSQL Server數(shù)

2022-11-04 08:34:27

Oracle數(shù)據(jù)庫

2011-08-22 13:04:47

SQL Server數(shù)函數(shù)

2010-10-26 15:21:11

連接Oracle數(shù)據(jù)庫

2015-10-30 14:00:33

adosybaseodbc

2011-08-09 09:31:39

SQL Server數(shù)connectionS
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)