国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

數(shù)據(jù)庫MySql類庫系列(八)-預(yù)處理執(zhí)行Sql方式的示例

2019-11-06 06:11:33
字體:
供稿:網(wǎng)友

本文是對之前預(yù)處理執(zhí)行Sql方式的示例程序TestDB

基于前文的DBService,PRepareOperatorSelect,PrepareOperatorUpdate

首先是數(shù)據(jù)表定義:

還是一個簡單的賬號表,包括3個字段:帳號名(最長20個字符,主鍵),賬號密碼(最長20個字符),賬號id(無符號整數(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;

下面演示對這張表,以預(yù)處理執(zhí)行sql的方式,實(shí)現(xiàn)增、刪、改、查

首先,實(shí)現(xiàn)一個PrepareDBService,繼承DBService

實(shí)現(xiàn)4個接口:

	// 分別實(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]);

對應(yīng)4個操作對象(2種:增刪改對應(yīng)PrepareOperatorUpdate,查找對應(yīng)PrepareOperatorSelect)

// 對應(yīng)增、刪、改、查四種操作的預(yù)處理的綁定參數(shù)/綁定結(jié)果void PrepareSelectAccount();void PrepareInsertAccount();void PrepareUpdateAccount();void PrepareDeleteAccount();	// 對應(yīng)增、刪、改、查四種操作對象common::db::PrepareOperatorSelect m_select_account;common::db::PrepareOperatorUpdate m_insert_account;common::db::PrepareOperatorUpdate m_update_account;common::db::PrepareOperatorUpdate m_delete_account;

主函數(shù):

1、插入一個賬號,賬號名=Test001,密碼=0000001的賬號

2、更新這個賬號的密碼,改為1111111

3、查詢這個賬號名=Test001的賬號信息(賬號名,密碼,id),此時密碼應(yīng)為第2步已經(jīng)修改后的密碼,如果該表此前沒有插入過記錄,此時id應(yīng)該為1,每執(zhí)行一次插入id+1

4、刪除這個賬號名=Test001的賬號

5、再次查詢這個賬號名=Test001的賬號信息,此時應(yīng)該沒有對應(yīng)的數(shù)據(jù)

執(zhí)行結(jié)果截圖:

DBService的子類實(shí)現(xiàn):

PrepareDBService.h:

#ifndef __PrepareDBService_H__#define	__PrepareDBService_H__#include "DBService.h"#include "PrepareOperatorSelect.h"#include "PrepareOperatorUpdate.h"class PrepareDBService : public common::db::DBService{public:	PrepareDBService();	virtual ~PrepareDBService();public:	// 最大賬號,密碼字符串長度為20個字符	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:	// 賬號屬性類型	enum AccountPropertyType	{		account_name = 0,		// 帳號名稱		account_key,			// 賬號密碼		account_id,				// 賬號id		account_property_count,	};	unsigned long m_property_len[account_property_count];	void InitPropertyLen();	// 賬號信息綁定值	char m_account_name[MaxAccountLen];	char m_account_key[MaxAccountLen];	unsigned int m_account_id;	// 各個預(yù)處理的綁定參數(shù)/綁定結(jié)果	void PrepareSelectAccount();	void PrepareInsertAccount();	void PrepareUpdateAccount();	void PrepareDeleteAccount();	// 對應(yīng)增、刪、改、查四種操作對象	common::db::PrepareOperatorSelect m_select_account;	common::db::PrepareOperatorUpdate m_insert_account;	common::db::PrepareOperatorUpdate m_update_account;	common::db::PrepareOperatorUpdate m_delete_account;};#endif

PrepareDBService.cpp:

#include "PrepareDBService.h"PrepareDBService::PrepareDBService(){}PrepareDBService::~PrepareDBService(){}bool PrepareDBService::ProcessStart(){	//預(yù)處理sql	InitPropertyLen();	PrepareSelectAccount();	PrepareInsertAccount();	PrepareUpdateAccount();	PrepareDeleteAccount();	return true;}void PrepareDBService::ProcessStop(){	m_select_account.Release();	m_insert_account.Release();	m_update_account.Release();	m_delete_account.Release();}void PrepareDBService::InitPropertyLen(){	m_property_len[account_name] = sizeof(m_account_name);	m_property_len[account_key] = sizeof(m_account_key);	m_property_len[account_id] = sizeof(m_account_id);}void PrepareDBService::PrepareSelectAccount(){	//預(yù)處理sql	m_select_account.BindSql(m_Connect,		"select * from account where account_name = ?;");	m_select_account.BindResult("%s,%s,%u",		m_account_name, &m_property_len[account_name],		m_account_key, &m_property_len[account_key],		&m_account_id);	m_select_account.BindParameter("%s",		m_account_name, &m_property_len[account_name]);}bool PrepareDBService::SelectAccount(const char(&name)[MaxAccountLen], char(&key)[MaxAccountLen], unsigned int& id){	boost::mutex::scoped_lock lock(m_Lock);	strcpy(m_account_name, name);	if (m_select_account.DoOperator())	{		if (m_select_account.FetchResult())		{			strcpy(key, m_account_key);			id = m_account_id;			m_select_account.FreeResult();			return true;		}		else		{			return false;		}	}	else	{		return false;	}}void PrepareDBService::PrepareInsertAccount(){	m_insert_account.BindSql(m_Connect,		"insert into account(account_name, account_key) values(?, ?);");	m_insert_account.BindParameter("%s,%s",		m_account_name, &m_property_len[account_name],		m_account_key, &m_property_len[account_key]);}bool PrepareDBService::InsertAccount(const char(&name)[MaxAccountLen], const char(&key)[MaxAccountLen]){	boost::mutex::scoped_lock lock(m_Lock);	strcpy(m_account_name, name);	strcpy(m_account_key, key);	return m_insert_account.DoOperator();}void PrepareDBService::PrepareUpdateAccount(){	m_update_account.BindSql(m_Connect,		"update account set account_key = ? where account_name = ?;");	m_update_account.BindParameter("%s,%s",		m_account_key, &m_property_len[account_key],		m_account_name, &m_property_len[account_name]);}bool PrepareDBService::UpdateAccount(const char(&name)[MaxAccountLen], const char(&key)[MaxAccountLen]){	boost::mutex::scoped_lock lock(m_Lock);	strcpy(m_account_name, name);	strcpy(m_account_key, key);	return m_update_account.DoOperator();}void PrepareDBService::PrepareDeleteAccount(){	m_delete_account.BindSql(m_Connect,		"delete from account where account_name = ?;");	m_delete_account.BindParameter("%s",		m_account_name, &m_property_len[account_name]);}bool PrepareDBService::DeleteAccount(const char(&name)[MaxAccountLen]){	boost::mutex::scoped_lock lock(m_Lock);	strcpy(m_account_name, name);	return m_delete_account.DoOperator();}

主函數(shù)TestDB.cpp:

#include <iostream>#include "PrepareDBService.h"void PrepareAccount(){	char accountName[PrepareDBService::MaxAccountLen] = { 0 };	char accountKey[PrepareDBService::MaxAccountLen] = { 0 };	unsigned int accountId = 0;	PrepareDBService 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[]){	PrepareAccount();	system("pause");	return 0;}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 江源县| 云霄县| 柞水县| 新巴尔虎左旗| 龙胜| 张家界市| 楚雄市| 泸西县| 保德县| 抚顺县| 汾西县| 淳化县| 分宜县| 大名县| 友谊县| 六枝特区| 确山县| 阜平县| 株洲市| 连山| 织金县| 奇台县| 江华| 外汇| 中西区| 定兴县| 普安县| 阳高县| 井陉县| 板桥市| 石泉县| 宁波市| 吉木乃县| 龙门县| 福州市| 通榆县| 湛江市| 西充县| 芦溪县| 东山县| 石嘴山市|