日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

[poco] 访问数据库

發布時間:2023/12/9 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [poco] 访问数据库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

From:?https://blog.csdn.net/hl2015222050145/article/details/52335422?utm_source=blogxgwz6

poco訪問數據基本步驟:
a. ?創建會話(session)

b. 從DB中讀寫數據
c. 使用statements

d. 使用容器(Collection) (數據,集合...)
e. 使用limit限定
f. 如何使用復雜的數據類型(如何將一個C++對象映射到數據庫的表

創建會話:Session create(const std::string& connectorKey, const std::string& connectionString);
參數1:SQLite;MySQL;或者ODBC,ODBC支持Oracle, SQLite, DB2, SQLServer和PostgreSQL。
參數2:就是數據庫文件的路徑或者連接字符

基本操作(增刪查改):

增:

?? ?std::string aName("Peter");
?? ?ses << "INSERT INTO 表名 VALUES(" << aName << ")", now;
?? ?或者ses << "INSERT INTO 表名 VALUES(?)", use(aName), now;


查:


一般查找

?? ?std::string aName;
?? ?ses << "SELECT NAME FROM FORENAME", into(aName), now; // aName的默認值為空字符串
?? ?ses << "SELECT NAME FROM FORENAME", into(aName, "default"), now;


匹配查找 ?先into后use ?且不能使用常量 如:4

?? ?std::string aName;
?? ?std::string match("Peter")
?? ?ses << "SELECT NAME FROM FORENAME WHERE NAME=?", into(aName), use(match), now;
?? ?poco_assert (aName == match);


多字節匹配查找

?? ?std::string firstName("Peter";
?? ?std::string lastName("Junior");
?? ?int age = 0;
?? ?ses << "INSERT INTO PERSON VALUES (?, ?, ?)", use(firstName), use(lastName), use(age), now;
?? ?ses << "SELECT (firstname, lastname, age) FROM Person", into(firstName), into(lastName), into(age), now;


into語句允許定義一個默認值,主要針對數據庫查詢里面包含空時候

?? ?ses << "SELECT (firstname, lastname, age) FROM Person", into(firstName), into(lastName), into(age, -1), now;
?
改:
?? ?session << "update Person set Address = ? WHERE Name= ?", use(addr),use(name),now;


使用Statements:

用于保存SQl語句,使得多次查詢。

?? ?std::string aName("Peter");
?? ?Statement stmt = ( ses << "INSERT INTO FORENAME VALUES(?)", use(aName) );//括號必須


或者
?? ?Statement stmt(ses);
?? ?stmt << "INSERT INTO FORENAME VALUES(?)", use(aName);
eg:

?? ?std::string aName("Peter");
?? ?Statement stmt = ( ses << "INSERT INTO FORENAME VALUES(?)", use(aName) );
?? ?.
?? ?.
?? ?stmt.execute();
?? ?poco_assert (stmt.done());


容器使用:

默認支持以下容器類:
a. vector(數組): 無特別要求
b. set: 類型需要支持<操作符,注意:重復的key/value將忽略
c. ?multiset: 需支持<操作符
d. map: 需支持()操作符,并且按key返回對象,注意:重復的key/value將忽略
e. multimap: 需支持()操作符,并按key返加對象.

?? ??? ?std::vector<std::string> names;
?? ??? ?ses << "SELECT NAME FROM FORENAME", into(names), now;


Limit限定:針對查詢結果的個數限定

?? ??? ?std::vector<std::string> names;
?? ??? ?ses << "SELECT NAME FROM FORENAME", into(names), limit(50), now;
以上代碼將只返回50行的數據。(當然也可能什么都不返回),并追加到 names這個容量中。如果想確保50行記錄返回,需要設置limit的第二參數為true(默認為false):
?? ??? ?std::vector<std::string> names;
?? ??? ?ses << "SELECT NAME FROM FORENAME", into(names), limit(50, true), now;
?? ??? ?Statement stmt = (ses << "SELECT NAME FROM FORENAME", into(aName), lowerLimit(1), upperLimit(10));//限制范圍
?? ??? ?Statement stmt = (ses << "SELECT NAME FROM FORENAME", into(aName), range(1,10));


類作為保存變量:這個就是將自定義類作為容器查詢

?? ??? ?namespace Poco?
?? ??? ?{
?? ??? ??? ?namespace Data
?? ??? ??? ??? ? {
?? ??? ??? ??? ??? ?template <>
?? ??? ??? ??? ??? ?class TypeHandler<class Person>
?? ??? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ??? ?.....
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ?}


記錄集(RecordSet)

a. 遍歷數據表中所有的行與列
b. 獲取各列的元信息,比如名稱,類型,長度等.

?? ??? ??? ?Statement select(session);
?? ??? ??? ?select << "SELECT * FROM Person";
?? ??? ??? ?select.execute();
?? ??? ??? ?RecordSet rs(select);


遍歷結果:


?? ??? ??? ?bool more = rs.moveFirst();
?? ??? ??? ?while (more)
?? ??? ??? ?{
?? ??? ??? ??? ?for (std::size_t col = 0; col < cols; ++col)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?std::cout << rs[col].convert<std::string>() << " ";
?? ??? ??? ??? ?}
?? ??? ??? ??? ?std::cout << std::endl;
?? ??? ??? ??? ?more = rs.moveNext();
?? ??? ??? ?}


Tuples:在數據庫中的列類型已知的情閱下,Poco::Tuple以及Tuple數組提供了更簡便的方法獲取數據。

?? ??? ??? ?typedef Poco::Tuple<std::string, std::string, int> Person;
?? ??? ??? ?typedef std::vector<Person> People;
?
?? ??? ??? ?People people;
?? ??? ??? ?people.push_back(Person("Bart Simpson", "Springfield", 12));
?? ??? ??? ?people.push_back(Person("Lisa Simpson", "Springfield", 10));
?
?? ??? ??? ?Statement insert(session);
?? ??? ??? ?insert << "INSERT INTO Person VALUES(?, ?, ?)",
?? ??? ??? ?use(people), now;
?
?? ??? ??? ?當然,tuple也可以用于查詢:
?? ??? ??? ?Statement select(session);
?? ??? ??? ?select << "SELECT Name, Address, Age FROM Person", into(people), ?now;
?
?? ??? ??? ?for (People::const_iterator it = people.begin(); it != people.end(); ++it)
?? ??? ??? ?{
?? ??? ??? ??? ?std::cout << "Name: " << it->get<0>() <<
?? ??? ??? ??? ?", Address: " << it->get<1>() <<
?? ??? ??? ??? ?", Age: " << it->get<2>() <<std::endl;
?? ??? ??? ?}

參考鏈接:http://pocoproject.org/docs/00200-DataUserManual.html


?測試代碼:

#include "Poco/Data/Session.h"
#include "Poco/Data/MySQL/Connector.h"
#include <vector>
#include <iostream>
?
?
using namespace Poco::Data::Keywords;
using Poco::Data::Session;
using Poco::Data::Statement;
?
?
struct Person
{
?? ?std::string name;
?? ?std::string address;
?? ?int ? ? ? ? age;
};
?
?
int main(int argc, char** argv)
{
?? ?Poco::Data::MySQL::Connector::registerConnector();
?
?? ?// 創建 session
?? ?Session session("MySQL", "host=127.0.0.1;port=3306;db=demo;user=admin;password=admin;compress=true;auto-reconnect=true");
?
?? ?// 刪除已存在的表
?? ?session << "DROP TABLE IF EXISTS Person", now;
?
?? ?// 創建新表
?? ?session << "CREATE TABLE Person (Name char(20),Address char(30) ,Age integer)", now;//varchar 會報錯
?? ?// 插入數據
?? ?Person person =
?? ?{
?? ??? ?"Bart Simpson",
?? ??? ?"Springfield",
?? ??? ?12
?? ?};
?
?? ?Statement insert(session);
?? ?insert << "INSERT INTO Person VALUES(?, ?, ?)",
?? ??? ?use(person.name),
?? ??? ?use(person.address),
?? ??? ?use(person.age);
?
?? ?insert.execute();
?
?? ?person.name = "Lisa Simpson";
?? ?person.address = "Springfield";
?? ?person.age = 10;
?
?? ?insert.execute();
?
?? ?//查詢數據
?? ?Statement select(session);
?? ?select << "select Name, Address, Age FROM Person",
?? ??? ?into(person.name),
?? ??? ?into(person.address),
?? ??? ?into(person.age),
?? ??? ?range(0, 1); // ?只查詢前兩行
?
?
?? ?while (!select.done())
?? ?{
?? ??? ?select.execute();
?? ??? ?std::cout << person.name << " " << person.address << " " << person.age << std::endl;
?? ?}
?
?? ?std::string addr = "hubeilichuan";
?? ?std::string name = "Lisa Simpson";
?
?? ?session << "update Person set Address = ? WHERE Name= ?", use(addr),use(name),now;
?? ?// 另一種查詢方式
?? ?std::vector<std::string> names;
?? ?session << "SELECT Address FROM Person",
?? ??? ?into(names),
?? ??? ?now;
?
?
?? ?for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); ++it)
?? ?{
?? ??? ?std::cout << *it << std::endl;
?? ?}
?? ?getchar();
?? ?return 0;
}


測試工程鏈接:http://download.csdn.net/detail/hl2015222050145/9614574

編譯好的動態庫鏈接:http://download.csdn.net/detail/hl2015222050145/9614205
?

總結

以上是生活随笔為你收集整理的[poco] 访问数据库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。