介紹一個針對C++程序的MySQL訪問庫soci
一直以來,筆者都在不停尋找一種更人性化的數(shù)據(jù)庫訪問方式(并不是說默認的方式不好,而是有時候的確在模塊化設(shè)計中不太方便)。
后來有幸在php中找到codeigniter的ActiveReord,詳細參考這篇文章: 抽離CodeIgniter的數(shù)據(jù)庫訪問類!
然而c++卻始終用著最原始的方式,昨天趁著項目要用的機會,在網(wǎng)上搜索了好久,總算讓我找到兩套c++的數(shù)據(jù)庫訪問框架:
兩套代碼我都拿下來看了一下,litesql實現(xiàn)了一套完整的代碼自動生成,功能強大,但是也很重;soci相對要輕量,但是同樣也實現(xiàn)了數(shù)據(jù)結(jié)構(gòu)到數(shù)據(jù)庫表的映射。本人還是比較喜歡輕量的東西,所以最終選擇了soci。經(jīng)過這兩天的試用,感覺非常不錯。
官方的文檔也很詳細,所以這里就用我寫的單元測試代碼來做一下簡單的講解:
首先創(chuàng)建庫表:
create database soci; CREATE TABLE `tb_test` ( `id` int(11) NOT NULL auto_increment, `name` varchar(32) default "", `sex` int(11) default 0, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`) ); create database soci; CREATE TABLE `tb_test` ( `id` int(11) NOT NULL auto_increment, `name` varchar(32) default "", `sex` int(11) default 0, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`) );
1.簡單的select單條記錄
TEST(soci,select_one)
{
try
{
session sql(mysql, "host=localhost db=soci user=dantezhu");
indicator ind;
string name = "dandan";
int sex;
sql << "select sex from tb_test where name = :name",
into(sex, ind), use(name);
ASSERT_EQ(ind, i_ok) << name;
}
catch (exception const &e)
{
FAIL()<<e.what();
}
}
TEST(soci,select_one)
{
try
{
session sql(mysql, "host=localhost db=soci user=dantezhu");
indicator ind;
string name = "dandan";
int sex;
sql << "select sex from tb_test where name = :name",
into(sex, ind), use(name);
ASSERT_EQ(ind, i_ok) << name;
}
catch (exception const &e)
{
FAIL()<<e.what();
}
}
select的結(jié)果,如果成功則ind會為i_ok,同值sex被賦值;如果失敗則反之
2.簡單的select多條記錄
TEST(soci,select_multi2)
{
try
{
session sql(mysql, "db=soci user=dantezhu");
indicator ind;
int count;
sql << "select count(*) from tb_test", into(count, ind);
ASSERT_EQ(ind, i_ok) << count;
if (count == 0)
{
SUCCEED();
return;
}
int sex = 1;
vector<string> vec_name(count);
vector<int> vec_sex(count);
sql << "select name,sex from tb_test where sex = :sex",
into(vec_name), into(vec_sex), use(sex);
}
catch (exception const &e)
{
FAIL()<<e.what();
}
}
TEST(soci,select_multi2)
{
try
{
session sql(mysql, "db=soci user=dantezhu");
indicator ind;
int count;
sql << "select count(*) from tb_test", into(count, ind);
ASSERT_EQ(ind, i_ok) << count;
if (count == 0)
{
SUCCEED();
return;
}
int sex = 1;
vector<string> vec_name(count);
vector<int> vec_sex(count);
sql << "select name,sex from tb_test where sex = :sex",
into(vec_name), into(vec_sex), use(sex);
}
catch (exception const &e)
{
FAIL()<<e.what();
}
}
與select單條記錄唯一的區(qū)別即,into()的參數(shù)是一個vector。其實用多個vector這種方式并不是一個很好的選擇,后面會介紹基于數(shù)據(jù)結(jié)構(gòu)的方式。
3.簡單的insert
TEST(soci,insert_exist)
{
try
{
session sql(mysql, "db=soci user=dantezhu");
string name = "dandan";
int sex = 1;
sql << "insert into tb_test(name, sex) values(:name, :sex)",
use(name), use(sex);
}
catch (exception const &e)
{
SUCCEED()<<e.what();
}
}
TEST(soci,insert_exist)
{
try
{
session sql(mysql, "db=soci user=dantezhu");
string name = "dandan";
int sex = 1;
sql << "insert into tb_test(name, sex) values(:name, :sex)",
use(name), use(sex);
}
catch (exception const &e)
{
SUCCEED()<<e.what();
}
}
insert,update,delete都有兩個同樣的問題:
a)affect_rows(操作的行數(shù))沒有辦法返回
b)操作的id無法知道,尤其對于insert的主鍵是自增的情況下,無法知道插入的主鍵的值是多少。
update和delete都與insert相似,這里就不再多說。
接下來是這個框架的很重要的一個特性,即數(shù)據(jù)庫表與數(shù)據(jù)結(jié)構(gòu)綁定:
首先我們需要定義一個結(jié)構(gòu)體,并告知soci怎么讓列名和數(shù)據(jù)結(jié)構(gòu)的字段對應(yīng)起來:
struct Person
{
int id;
std::string name;
int sex;
};
namespace soci
{
template<> struct type_conversion<Person>
{
typedef values base_type;
static void from_base(values const & v, indicator /* ind */, Person & p)
{
p.id = v.get<int>("id");
p.name = v.get<std::string>("name");
p.sex = v.get<int>("sex");
}
static void to_base(const Person & p, values & v, indicator & ind)
{
v.set("id", p.id);
v.set("name", p.name);
v.set("sex", p.sex);
ind = i_ok;
}
};
}
struct Person
{
int id;
std::string name;
int sex;
};
namespace soci
{
template<> struct type_conversion<Person>
{
typedef values base_type;
static void from_base(values const & v, indicator /* ind */, Person & p)
{
p.id = v.get<int>("id");
p.name = v.get<std::string>("name");
p.sex = v.get<int>("sex");
}
static void to_base(const Person & p, values & v, indicator & ind)
{
v.set("id", p.id);
v.set("name", p.name);
v.set("sex", p.sex);
ind = i_ok;
}
};
}
關(guān)于
template<> struct type_conversion<Person> template<> struct type_conversion<Person>
這里,官方的文檔是是有誤的,我查了好長時間,按照上面的寫法來寫即可。
1.用數(shù)據(jù)結(jié)構(gòu)來select
TEST(soci,select_obj_one)
{
try
{
session sql(mysql, "db=soci user=dantezhu");
indicator ind;
int count;
sql << "select count(*) from tb_test", into(count, ind);
ASSERT_EQ(ind, i_ok) << count;
string name = "dandan";
Person p;
sql << "select id,name,sex from tb_test where name = :name",
into(p, ind), use(name);
ASSERT_EQ(ind, i_ok) << name;
if (sql.got_data())
{
cout<< p.id
<< ","
<< p.name
<< ","
<< p.sex
<< endl;
}
}
catch (exception const &e)
{
FAIL()<<e.what();
}
}
TEST(soci,select_obj_one)
{
try
{
session sql(mysql, "db=soci user=dantezhu");
indicator ind;
int count;
sql << "select count(*) from tb_test", into(count, ind);
ASSERT_EQ(ind, i_ok) << count;
string name = "dandan";
Person p;
sql << "select id,name,sex from tb_test where name = :name",
into(p, ind), use(name);
ASSERT_EQ(ind, i_ok) << name;
if (sql.got_data())
{
cout<< p.id
<< ","
<< p.name
<< ","
<< p.sex
<< endl;
}
}
catch (exception const &e)
{
FAIL()<<e.what();
}
}
2.用數(shù)據(jù)結(jié)構(gòu)來進行insert
TEST(soci,insert_obj_noexist)
{
try
{
session sql(mysql, "db=soci user=dantezhu");
Person p = {
0,
"niuniu",
2
};
sql << "insert into tb_test(name, sex) values(:name, :sex)",
use(p);
}
catch (exception const &e)
{
FAIL()<<e.what();
}
}
TEST(soci,insert_obj_noexist)
{
try
{
session sql(mysql, "db=soci user=dantezhu");
Person p = {
0,
"niuniu",
2
};
sql << "insert into tb_test(name, sex) values(:name, :sex)",
use(p);
}
catch (exception const &e)
{
FAIL()<<e.what();
}
}
整個就是這樣~~下面是文中代碼文件的下載路徑:
http://code.google.com/p/vimercode/source/browse/#svn%2Ftrunk%2Fsoci_test
另外,雖然python下的mysql訪問也算比較簡單,但還是想知道是否有更Pythonic的庫或接口,如果有朋友知道,歡迎不吝告知。
相關(guān)文章
MySQL調(diào)優(yōu)之索引在什么情況下會失效詳解
索引的失效,會大大降低sql的執(zhí)行效率,日常中又有哪些常見的情況會導(dǎo)致索引失效?下面這篇文章主要給大家介紹了關(guān)于MySQL調(diào)優(yōu)之索引在什么情況下會失效的相關(guān)資料,需要的朋友可以參考下2022-10-10
MySQL 數(shù)據(jù)庫定時備份的幾種方式(全面)
在操作數(shù)據(jù)過程中,可能會導(dǎo)致數(shù)據(jù)錯誤,甚至數(shù)據(jù)庫奔潰,而有效的定時備份能很好地保護數(shù)據(jù)庫。本篇文章主要講述了幾種方法進行 MySQL 定時備份數(shù)據(jù)庫。2021-09-09
MySQL 索引的優(yōu)缺點以及創(chuàng)建索引的準則
這篇文章主要介紹了MySQL 索引的優(yōu)劣以及創(chuàng)建索引的準則,幫助大家更好的理解和使用MySQL 索引,感興趣的朋友可以了解下2020-09-09
MySQL/Oracle數(shù)據(jù)庫的字符串轉(zhuǎn)日期、日期轉(zhuǎn)字符串
在許多數(shù)據(jù)庫中,你可以使用內(nèi)置的函數(shù)或操作符來進行字符串和日期之間的相互轉(zhuǎn)換,這篇文章主要給大家介紹了關(guān)于MySQL/Oracle數(shù)據(jù)庫的字符串轉(zhuǎn)日期、日期轉(zhuǎn)字符串的相關(guān)資料,需要的朋友可以參考下2024-07-07
詳解MySQL誤操作后怎樣進行數(shù)據(jù)恢復(fù)
在大家日常操作數(shù)據(jù)庫時候難免會因為“大意”而誤操作,那么誤操作后怎樣進行數(shù)據(jù)恢復(fù)呢,下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)。2016-08-08
Mysql聯(lián)表update數(shù)據(jù)的示例詳解
這篇文章主要介紹了Mysql聯(lián)表update數(shù)據(jù)的示例詳解,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
詳解讓MySQL和Redis數(shù)據(jù)保持一致的四種策略
在分布式系統(tǒng)中,保證Redis和MySQL之間的數(shù)據(jù)一致性是一個復(fù)雜且重要的問題,下面這篇文章主要給大家介紹了關(guān)于讓MySQL和Redis數(shù)據(jù)保持一致的四種策略,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-07-07
MySQL數(shù)據(jù)遷移使用MySQLdump命令
今天小編就為大家分享一篇關(guān)于MySQL數(shù)據(jù)遷移使用MySQLdump命令,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10

