《Mysql必讀介紹一個(gè)針對(duì)C++程序的MySQL訪問(wèn)庫(kù)soci》要點(diǎn):
本文介紹了Mysql必讀介紹一個(gè)針對(duì)C++程序的MySQL訪問(wèn)庫(kù)soci,希望對(duì)您有用。如果有疑問(wèn),可以聯(lián)系我們。
一直以來(lái),筆者都在不停尋找一種更人性化的數(shù)據(jù)庫(kù)訪問(wèn)方式(并不是說(shuō)默認(rèn)的方式不好,而是有時(shí)候的確在模塊化設(shè)計(jì)中不太方便).
后來(lái)有幸在php中找到codeigniter的ActiveReord,詳細(xì)參考這篇文章: 抽離CodeIgniter的數(shù)據(jù)庫(kù)訪問(wèn)類!
然而c++卻始終用著最原始的方式,昨天趁著項(xiàng)目要用的機(jī)會(huì),在網(wǎng)上搜索了好久,總算讓我找到兩套c++的數(shù)據(jù)庫(kù)訪問(wèn)框架:MYSQL數(shù)據(jù)庫(kù)
???soci
???litesqlMYSQL數(shù)據(jù)庫(kù)
兩套代碼我都拿下來(lái)看了一下,litesql實(shí)現(xiàn)了一套完整的代碼自動(dòng)生成,功能強(qiáng)大,但是也很重;soci相對(duì)要輕量,但是同樣也實(shí)現(xiàn)了數(shù)據(jù)結(jié)構(gòu)到數(shù)據(jù)庫(kù)表的映射.本人還是比較喜歡輕量的東西,所以最終選擇了soci.經(jīng)過(guò)這兩天的試用,感覺(jué)非常不錯(cuò).MYSQL數(shù)據(jù)庫(kù)
官方的文檔也很詳細(xì),所以這里就用我寫的單元測(cè)試代碼來(lái)做一下簡(jiǎn)單的講解:
首先創(chuàng)建庫(kù)表:
MYSQL數(shù)據(jù)庫(kù)
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.簡(jiǎn)單的select單條記錄
MYSQL數(shù)據(jù)庫(kù)
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會(huì)為i_ok,同值sex被賦值;如果失敗則反之MYSQL數(shù)據(jù)庫(kù)
2.簡(jiǎn)單的select多條記錄
MYSQL數(shù)據(jù)庫(kù)
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ù)是一個(gè)vector.其實(shí)用多個(gè)vector這種方式并不是一個(gè)很好的選擇,后面會(huì)介紹基于數(shù)據(jù)結(jié)構(gòu)的方式.MYSQL數(shù)據(jù)庫(kù)
3.簡(jiǎn)單的insert
MYSQL數(shù)據(jù)庫(kù)
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都有兩個(gè)同樣的問(wèn)題:
a)affect_rows(操作的行數(shù))沒(méi)有辦法返回
b)操作的id無(wú)法知道,尤其對(duì)于insert的主鍵是自增的情況下,無(wú)法知道插入的主鍵的值是多少.MYSQL數(shù)據(jù)庫(kù)
update和delete都與insert相似,這里就不再多說(shuō).MYSQL數(shù)據(jù)庫(kù)
接下來(lái)是這個(gè)框架的很重要的一個(gè)特性,即數(shù)據(jù)庫(kù)表與數(shù)據(jù)結(jié)構(gòu)綁定:MYSQL數(shù)據(jù)庫(kù)
首先我們需要定義一個(gè)結(jié)構(gòu)體,并告知soci怎么讓列名和數(shù)據(jù)結(jié)構(gòu)的字段對(duì)應(yīng)起來(lái):
MYSQL數(shù)據(jù)庫(kù)
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)于
MYSQL數(shù)據(jù)庫(kù)
template<> struct type_conversion<Person> template<> struct type_conversion<Person>
這里,官方的文檔是是有誤的,我查了好長(zhǎng)時(shí)間,按照上面的寫法來(lái)寫即可.MYSQL數(shù)據(jù)庫(kù)
1.用數(shù)據(jù)結(jié)構(gòu)來(lái)select
MYSQL數(shù)據(jù)庫(kù)
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)來(lái)進(jìn)行insert
MYSQL數(shù)據(jù)庫(kù)
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(); } }
整個(gè)就是這樣~~下面是文中代碼文件的下載路徑:
http://code.google.com/p/vimercode/source/browse/#svn%2Ftrunk%2Fsoci_testMYSQL數(shù)據(jù)庫(kù)
另外,雖然python下的mysql訪問(wèn)也算比較簡(jiǎn)單,但還是想知道是否有更Pythonic的庫(kù)或接口,如果有朋友知道,歡迎不吝告知.
MYSQL數(shù)據(jù)庫(kù)
轉(zhuǎn)載請(qǐng)注明本頁(yè)網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/5945.html