本文是對(duì)之前直接執(zhí)行Sql方式的示例程序TestDB
基于前文的DBService,QueryOperatorSelect,QueryOperatorUpdate
首先是數(shù)據(jù)表定義:
一個(gè)簡(jiǎn)單的賬號(hào)表,包括3個(gè)字段:帳號(hào)名(最長(zhǎng)20個(gè)字符,主鍵),賬號(hào)密碼(最長(zhǎng)20個(gè)字符),賬號(hào)id(無(wú)符號(hào)整數(shù),自增字段)
sql如下:
CREATE TABLE `account` ( `account_name` varchar(20) NOT NULL, `account_key` varchar(20) NOT NULL, `account_id` int(11) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`account_name`), UNIQUE KEY `account_id_index` (`account_id`)) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;下面演示對(duì)這張表,以直接執(zhí)行sql的方式,實(shí)現(xiàn)增、刪、改、查首先,實(shí)現(xiàn)一個(gè)QueryDBService,繼承DBService
實(shí)現(xiàn)4個(gè)接口:
// 分別實(shí)現(xiàn)增、刪、改、查 bool SelectAccount(const char(&name)[MaxAccountLen], char(&key)[MaxAccountLen], unsigned int& id); bool InsertAccount(const char(&name)[MaxAccountLen], const char(&key)[MaxAccountLen]); bool UpdateAccount(const char(&name)[MaxAccountLen], const char(&key)[MaxAccountLen]); bool DeleteAccount(const char(&name)[MaxAccountLen]);對(duì)應(yīng)4個(gè)操作對(duì)象(2種:增刪改對(duì)應(yīng)QueryOperatorUpdate,查找對(duì)應(yīng)QueryOperatorSelect)// 對(duì)應(yīng)增、刪、改、查四種操作對(duì)象 common::db::QueryOperatorSelect m_select_account; common::db::QueryOperatorUpdate m_insert_account; common::db::QueryOperatorUpdate m_update_account; common::db::QueryOperatorUpdate m_delete_account;主函數(shù):1、插入一個(gè)賬號(hào),賬號(hào)名=Test001,密碼=0000001的賬號(hào)
2、更新這個(gè)賬號(hào)的密碼,改為1111111
3、查詢這個(gè)賬號(hào)名=Test001的賬號(hào)信息(賬號(hào)名,密碼,id),此時(shí)密碼應(yīng)為第2步已經(jīng)修改后的密碼,如果該表此前沒(méi)有插入過(guò)記錄,此時(shí)id應(yīng)該為1,每執(zhí)行一次插入id+1
4、刪除這個(gè)賬號(hào)名=Test001的賬號(hào)
5、再次查詢這個(gè)賬號(hào)名=Test001的賬號(hào)信息,此時(shí)應(yīng)該沒(méi)有對(duì)應(yīng)的數(shù)據(jù)
執(zhí)行結(jié)果截圖:
DBService的子類實(shí)現(xiàn):
QueryDBService.h:
#ifndef __QueryDBService_H__#define __QueryDBService_H__#include "DBService.h"#include "QueryOperatorSelect.h"#include "QueryOperatorUpdate.h"class QueryDBService : public common::db::DBService{public: QueryDBService(); virtual ~QueryDBService();public: // 最大賬號(hào),密碼字符串長(zhǎng)度為20個(gè)字符 static const unsigned int MaxAccountLen = 20;public: virtual bool ProcessStart(); virtual void ProcessStop(); // 分別實(shí)現(xiàn)增、刪、改、查 bool SelectAccount(const char(&name)[MaxAccountLen], char(&key)[MaxAccountLen], unsigned int& id); bool InsertAccount(const char(&name)[MaxAccountLen], const char(&key)[MaxAccountLen]); bool UpdateAccount(const char(&name)[MaxAccountLen], const char(&key)[MaxAccountLen]); bool DeleteAccount(const char(&name)[MaxAccountLen]);private: // 對(duì)應(yīng)增、刪、改、查四種操作對(duì)象 common::db::QueryOperatorSelect m_select_account; common::db::QueryOperatorUpdate m_insert_account; common::db::QueryOperatorUpdate m_update_account; common::db::QueryOperatorUpdate m_delete_account;};#endifQueryDBService.cpp:#include "QueryDBService.h"QueryDBService::QueryDBService(){}QueryDBService::~QueryDBService(){}bool QueryDBService::ProcessStart(){ return true;}void QueryDBService::ProcessStop(){ m_select_account.Release(); m_insert_account.Release(); m_update_account.Release(); m_delete_account.Release();}bool QueryDBService::SelectAccount(const char(&name)[MaxAccountLen], char(&key)[MaxAccountLen], unsigned int& id){ boost::mutex::scoped_lock lock(m_Lock); char sql[1024] = { 0 }; sprintf_s(sql, "select * from account where account_name = /"%s/";", name); if (m_select_account.DoOperator(m_Connect, sql)) { if (m_select_account.FetchResult()) { m_select_account.GetColumn(key, 1); m_select_account.GetColumn(id, 2); m_select_account.FreeResult(); return true; } else { return false; } } else { return false; }}bool QueryDBService::InsertAccount(const char(&name)[MaxAccountLen], const char(&key)[MaxAccountLen]){ boost::mutex::scoped_lock lock(m_Lock); char sql[1024] = { 0 }; sprintf_s(sql, "insert into account (account_name, account_key) values (/"%s/", /"%s/");", name, key); return m_insert_account.DoOperator(m_Connect, sql);}bool QueryDBService::UpdateAccount(const char(&name)[MaxAccountLen], const char(&key)[MaxAccountLen]){ boost::mutex::scoped_lock lock(m_Lock); char sql[1024] = { 0 }; sprintf_s(sql, "update account set account_key = /"%s/" where account_name = /"%s/";", key, name); return m_update_account.DoOperator(m_Connect, sql);}bool QueryDBService::DeleteAccount(const char(&name)[MaxAccountLen]){ boost::mutex::scoped_lock lock(m_Lock); char sql[1024] = { 0 }; sprintf_s(sql, "delete from account where account_name = /"%s/";", name); return m_delete_account.DoOperator(m_Connect, sql);}主函數(shù)TestDB.cpp:#include <iostream>#include "QueryDBService.h"void QueryAccount(){ char accountName[QueryDBService::MaxAccountLen] = { 0 }; char accountKey[QueryDBService::MaxAccountLen] = { 0 }; unsigned int accountId = 0; QueryDBService service; service.Start("127.0.0.1", 3306, "root", "root", "account"); /////////////////////////Insert///////////////////////// memset(accountName, 0x00, sizeof(accountName)); strcpy(accountName, "Test001"); memset(accountKey, 0x00, sizeof(accountKey)); strcpy(accountKey, "0000001"); if (service.InsertAccount(accountName, accountKey)) { std::cout << "InsertAccount name = " << accountName << ", key = " << accountKey << " success" << std::endl; } /////////////////////////Update///////////////////////// memset(accountName, 0x00, sizeof(accountName)); strcpy(accountName, "Test001"); memset(accountKey, 0x00, sizeof(accountKey)); strcpy(accountKey, "1111111"); if (service.UpdateAccount(accountName, accountKey)) { std::cout << "UpdateAccount name = " << accountName << ", key = " << accountKey << " success" << std::endl; } /////////////////////////Select///////////////////////// memset(accountName, 0x00, sizeof(accountName)); strcpy(accountName, "Test001"); if (service.SelectAccount(accountName, accountKey, accountId)) { std::cout << "SelectAccount name = " << accountName << ", key = " << accountKey << " , id = " << accountId << std::endl; } else { std::cout << "no result" << std::endl; } /////////////////////////Delete///////////////////////// memset(accountName, 0x00, sizeof(accountName)); strcpy(accountName, "Test001"); if (service.DeleteAccount(accountName)) { std::cout << "DeleteAccount name = " << accountName << " success" << std::endl; } /////////////////////////Select///////////////////////// memset(accountName, 0x00, sizeof(accountName)); strcpy(accountName, "Test001"); if (service.SelectAccount(accountName, accountKey, accountId)) { std::cout << "SelectAccount name = " << accountName << ", key = " << accountKey << " , id = " << accountId << std::endl; } else { std::cout << "no result" << std::endl; } service.Stop();}int main(int argc, char* argv[]){ QueryAccount(); system("pause"); return 0;}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注