- PostgreSQL 教程
- PostgreSQL - 首頁
- PostgreSQL - 概述
- PostgreSQL - 環境設定
- PostgreSQL - 語法
- PostgreSQL - 資料型別
- PostgreSQL - 建立資料庫
- PostgreSQL - 選擇資料庫
- PostgreSQL - 刪除資料庫
- PostgreSQL - 建立表
- PostgreSQL - 刪除表
- PostgreSQL - 模式
- PostgreSQL - 插入查詢
- PostgreSQL - 選擇查詢
- PostgreSQL - 運算子
- PostgreSQL - 表示式
- PostgreSQL - WHERE 子句
- PostgreSQL - AND & OR 子句
- PostgreSQL - 更新查詢
- PostgreSQL - 刪除查詢
- PostgreSQL - LIKE 子句
- PostgreSQL - LIMIT 子句
- PostgreSQL - ORDER BY 子句
- PostgreSQL - GROUP BY
- PostgreSQL - WITH 子句
- PostgreSQL - HAVING 子句
- PostgreSQL - DISTINCT 關鍵字
- 高階 PostgreSQL
- PostgreSQL - 約束
- PostgreSQL - 連線
- PostgreSQL - UNION 子句
- PostgreSQL - NULL 值
- PostgreSQL - 別名語法
- PostgreSQL - 觸發器
- PostgreSQL - 索引
- PostgreSQL - ALTER TABLE 命令
- TRUNCATE TABLE 命令
- PostgreSQL - 檢視
- PostgreSQL - 事務
- PostgreSQL - 鎖
- PostgreSQL - 子查詢
- PostgreSQL - 自動遞增
- PostgreSQL - 許可權
- 日期/時間函式 & 運算子
- PostgreSQL - 函式
- PostgreSQL - 有用函式
- PostgreSQL 介面
- PostgreSQL - C/C++
- PostgreSQL - Java
- PostgreSQL - PHP
- PostgreSQL - Perl
- PostgreSQL - Python
- PostgreSQL 有用資源
- PostgreSQL - 快速指南
- PostgreSQL - 有用資源
- PostgreSQL - 討論
PostgreSQL - C/C++ 介面
本教程將使用libpqxx庫,它是 PostgreSQL 的官方 C++ 客戶端 API。libpqxx 的原始碼在 BSD 許可證下可用,因此您可以自由下載、傳遞給他人、更改、出售、將其包含在您自己的程式碼中,以及與您選擇的任何人共享您的更改。
安裝
最新的 libpqxx 版本可從連結 下載 Libpqxx 下載。因此,請下載最新版本並按照以下步驟操作 -
wget http://pqxx.org/download/software/libpqxx/libpqxx-4.0.tar.gz tar xvfz libpqxx-4.0.tar.gz cd libpqxx-4.0 ./configure make make install
在開始使用 C/C++ PostgreSQL 介面之前,請在您的 PostgreSQL 安裝目錄中找到pg_hba.conf檔案並新增以下行 -
# IPv4 local connections: host all all 127.0.0.1/32 md5
您可以使用以下命令啟動/重新啟動 postgres 伺服器(如果它未執行) -
[root@host]# service postgresql restart Stopping postgresql service: [ OK ] Starting postgresql service: [ OK ]
C/C++ 介面 API
以下是重要的介面例程,它們可以滿足您從 C/C++ 程式中使用 PostgreSQL 資料庫的要求。如果您正在尋找更復雜的應用程式,那麼您可以檢視 libpqxx 官方文件,或者您可以使用商業 API。
| 序號 | API & 描述 |
|---|---|
| 1 | pqxx::connection C( const std::string & dbstring ) 這是一個 typedef,將用於連線到資料庫。這裡,dbstring 提供了連線到資料庫所需的引數,例如dbname = testdb user = postgres password=pass123 hostaddr=127.0.0.1 port=5432。 如果連線設定成功,則它使用連線物件建立 C,該物件提供各種有用的公共函式。 |
| 2 | C.is_open() 方法 is_open() 是連線物件的公共方法,並返回布林值。如果連線處於活動狀態,則此方法返回 true,否則返回 false。 |
| 3 | C.disconnect() 此方法用於斷開已開啟的資料庫連線。 |
| 4 | pqxx::work W( C ) 這是一個 typedef,將用於使用連線 C 建立事務物件,該物件最終將用於以事務模式執行 SQL 語句。 如果事務物件建立成功,則將其分配給變數 W,該變數將用於訪問與事務物件相關的公共方法。 |
| 5 |
W.exec(const std::string & sql) 事務物件的此公共方法將用於執行 SQL 語句。 |
| 6 |
W.commit() 事務物件的此公共方法將用於提交事務。 |
| 7 |
W.abort() 事務物件的此公共方法將用於回滾事務。 |
| 8 | pqxx::nontransaction N( C ) 這是一個 typedef,將用於使用連線 C 建立非事務物件,該物件最終將用於以非事務模式執行 SQL 語句。 如果事務物件建立成功,則將其分配給變數 N,該變數將用於訪問與非事務物件相關的公共方法。 |
| 9 | N.exec(const std::string & sql) 非事務物件的此公共方法將用於執行 SQL 語句並返回結果物件,該物件實際上是一個迭代器,包含所有返回的記錄。 |
連線到資料庫
以下 C 程式碼段顯示瞭如何連線到在本地機器上的埠 5432 上執行的現有資料庫。這裡,我使用了反斜槓 \ 用於續行。
#include <iostream>
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
int main(int argc, char* argv[]) {
try {
connection C("dbname = testdb user = postgres password = cohondob \
hostaddr = 127.0.0.1 port = 5432");
if (C.is_open()) {
cout << "Opened database successfully: " << C.dbname() << endl;
} else {
cout << "Can't open database" << endl;
return 1;
}
C.disconnect ();
} catch (const std::exception &e) {
cerr << e.what() << std::endl;
return 1;
}
}
現在,讓我們編譯並執行上述程式以連線到我們的資料庫testdb,它已在您的模式中可用,並且可以使用使用者postgres和密碼pass123訪問。
您可以根據您的資料庫設定使用使用者 ID 和密碼。請記住按給定的順序保留 -lpqxx 和 -lpq!否則,連結器會抱怨缺少以“PQ.”開頭的函式。
$g++ test.cpp -lpqxx -lpq $./a.out Opened database successfully: testdb
建立表
以下 C 程式碼段將用於在先前建立的資料庫中建立表 -
#include <iostream>
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
int main(int argc, char* argv[]) {
char * sql;
try {
connection C("dbname = testdb user = postgres password = cohondob \
hostaddr = 127.0.0.1 port = 5432");
if (C.is_open()) {
cout << "Opened database successfully: " << C.dbname() << endl;
} else {
cout << "Can't open database" << endl;
return 1;
}
/* Create SQL statement */
sql = "CREATE TABLE COMPANY(" \
"ID INT PRIMARY KEY NOT NULL," \
"NAME TEXT NOT NULL," \
"AGE INT NOT NULL," \
"ADDRESS CHAR(50)," \
"SALARY REAL );";
/* Create a transactional object. */
work W(C);
/* Execute SQL query */
W.exec( sql );
W.commit();
cout << "Table created successfully" << endl;
C.disconnect ();
} catch (const std::exception &e) {
cerr << e.what() << std::endl;
return 1;
}
return 0;
}
當編譯並執行上述程式時,它將在您的 testdb 資料庫中建立 COMPANY 表,並顯示以下語句 -
Opened database successfully: testdb Table created successfully
INSERT 操作
以下 C 程式碼段顯示瞭如何在上面示例中建立的 COMPANY 表中建立記錄 -
#include <iostream>
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
int main(int argc, char* argv[]) {
char * sql;
try {
connection C("dbname = testdb user = postgres password = cohondob \
hostaddr = 127.0.0.1 port = 5432");
if (C.is_open()) {
cout << "Opened database successfully: " << C.dbname() << endl;
} else {
cout << "Can't open database" << endl;
return 1;
}
/* Create SQL statement */
sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " \
"VALUES (1, 'Paul', 32, 'California', 20000.00 ); " \
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) " \
"VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); " \
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \
"VALUES (3, 'Teddy', 23, 'Norway', 20000.00 );" \
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)" \
"VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 );";
/* Create a transactional object. */
work W(C);
/* Execute SQL query */
W.exec( sql );
W.commit();
cout << "Records created successfully" << endl;
C.disconnect ();
} catch (const std::exception &e) {
cerr << e.what() << std::endl;
return 1;
}
return 0;
}
當編譯並執行上述程式時,它將在 COMPANY 表中建立給定的記錄,並顯示以下兩行 -
Opened database successfully: testdb Records created successfully
SELECT 操作
以下 C 程式碼段顯示瞭如何從上面示例中建立的 COMPANY 表中獲取和顯示記錄 -
#include <iostream>
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
int main(int argc, char* argv[]) {
char * sql;
try {
connection C("dbname = testdb user = postgres password = cohondob \
hostaddr = 127.0.0.1 port = 5432");
if (C.is_open()) {
cout << "Opened database successfully: " << C.dbname() << endl;
} else {
cout << "Can't open database" << endl;
return 1;
}
/* Create SQL statement */
sql = "SELECT * from COMPANY";
/* Create a non-transactional object. */
nontransaction N(C);
/* Execute SQL query */
result R( N.exec( sql ));
/* List down all the records */
for (result::const_iterator c = R.begin(); c != R.end(); ++c) {
cout << "ID = " << c[0].as<int>() << endl;
cout << "Name = " << c[1].as<string>() << endl;
cout << "Age = " << c[2].as<int>() << endl;
cout << "Address = " << c[3].as<string>() << endl;
cout << "Salary = " << c[4].as<float>() << endl;
}
cout << "Operation done successfully" << endl;
C.disconnect ();
} catch (const std::exception &e) {
cerr << e.what() << std::endl;
return 1;
}
return 0;
}
當編譯並執行上述程式時,它將產生以下結果 -
Opened database successfully: testdb ID = 1 Name = Paul Age = 32 Address = California Salary = 20000 ID = 2 Name = Allen Age = 25 Address = Texas Salary = 15000 ID = 3 Name = Teddy Age = 23 Address = Norway Salary = 20000 ID = 4 Name = Mark Age = 25 Address = Rich-Mond Salary = 65000 Operation done successfully
UPDATE 操作
以下 C 程式碼段顯示瞭如何使用 UPDATE 語句更新任何記錄,然後從我們的 COMPANY 表中獲取和顯示更新的記錄 -
#include <iostream>
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
int main(int argc, char* argv[]) {
char * sql;
try {
connection C("dbname = testdb user = postgres password = cohondob \
hostaddr = 127.0.0.1 port = 5432");
if (C.is_open()) {
cout << "Opened database successfully: " << C.dbname() << endl;
} else {
cout << "Can't open database" << endl;
return 1;
}
/* Create a transactional object. */
work W(C);
/* Create SQL UPDATE statement */
sql = "UPDATE COMPANY set SALARY = 25000.00 where ID=1";
/* Execute SQL query */
W.exec( sql );
W.commit();
cout << "Records updated successfully" << endl;
/* Create SQL SELECT statement */
sql = "SELECT * from COMPANY";
/* Create a non-transactional object. */
nontransaction N(C);
/* Execute SQL query */
result R( N.exec( sql ));
/* List down all the records */
for (result::const_iterator c = R.begin(); c != R.end(); ++c) {
cout << "ID = " << c[0].as<int>() << endl;
cout << "Name = " << c[1].as<string>() << endl;
cout << "Age = " << c[2].as<int>() << endl;
cout << "Address = " << c[3].as<string>() << endl;
cout << "Salary = " << c[4].as<float>() << endl;
}
cout << "Operation done successfully" << endl;
C.disconnect ();
} catch (const std::exception &e) {
cerr << e.what() << std::endl;
return 1;
}
return 0;
}
當編譯並執行上述程式時,它將產生以下結果 -
Opened database successfully: testdb Records updated successfully ID = 2 Name = Allen Age = 25 Address = Texas Salary = 15000 ID = 3 Name = Teddy Age = 23 Address = Norway Salary = 20000 ID = 4 Name = Mark Age = 25 Address = Rich-Mond Salary = 65000 ID = 1 Name = Paul Age = 32 Address = California Salary = 25000 Operation done successfully
DELETE 操作
以下 C 程式碼段顯示瞭如何使用 DELETE 語句刪除任何記錄,然後從我們的 COMPANY 表中獲取和顯示剩餘的記錄 -
#include <iostream>
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
int main(int argc, char* argv[]) {
char * sql;
try {
connection C("dbname = testdb user = postgres password = cohondob \
hostaddr = 127.0.0.1 port = 5432");
if (C.is_open()) {
cout << "Opened database successfully: " << C.dbname() << endl;
} else {
cout << "Can't open database" << endl;
return 1;
}
/* Create a transactional object. */
work W(C);
/* Create SQL DELETE statement */
sql = "DELETE from COMPANY where ID = 2";
/* Execute SQL query */
W.exec( sql );
W.commit();
cout << "Records deleted successfully" << endl;
/* Create SQL SELECT statement */
sql = "SELECT * from COMPANY";
/* Create a non-transactional object. */
nontransaction N(C);
/* Execute SQL query */
result R( N.exec( sql ));
/* List down all the records */
for (result::const_iterator c = R.begin(); c != R.end(); ++c) {
cout << "ID = " << c[0].as<int>() << endl;
cout << "Name = " << c[1].as<string>() << endl;
cout << "Age = " << c[2].as<int>() << endl;
cout << "Address = " << c[3].as<string>() << endl;
cout << "Salary = " << c[4].as<float>() << endl;
}
cout << "Operation done successfully" << endl;
C.disconnect ();
} catch (const std::exception &e) {
cerr << e.what() << std::endl;
return 1;
}
return 0;
}
當編譯並執行上述程式時,它將產生以下結果 -
Opened database successfully: testdb Records deleted successfully ID = 3 Name = Teddy Age = 23 Address = Norway Salary = 20000 ID = 4 Name = Mark Age = 25 Address = Rich-Mond Salary = 65000 ID = 1 Name = Paul Age = 32 Address = California Salary = 25000 Operation done successfully