2.DB-Mysql++实例

以下为自己写过的测试代码,确认OK

以下为自己的数据库:


遍历查询代码

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <mysql++.h>
using std::cout;  
using std::endl; 

#ifdef _DEBUG
#pragma comment(lib, "..\\debug\\mysqlpp.lib")
#else
#pragma comment(lib, "..\\release\\mysqlpp.lib")
#endif

int _tmain(int argc, _TCHAR* argv[])
{
	const char* db = "ppdb", *server = "192.168.75.129", *user = "root", *pass = "123456";
	mysqlpp::Connection conn(false);	// 连接数据库
	if (conn.connect(db, server, user, pass))
	{
		// 从ppdb表中获取所有字段数据表并显示
		mysqlpp::Query query = conn.query("select * from ppdb_resources");
		if (mysqlpp::StoreQueryResult res = query.store())
		{
			std::cout << "We have:" << std::endl;
			for (size_t i = 0; i < res.num_rows(); ++i)
			{
				for (size_t j=0; j < res.num_fields(); ++j)
				{
					mysqlpp::String temp = res[i][j];
					cout << '\t' << temp;
				}
				cout <<endl;
			}
		}
		else
		{
			std::cout << "Failed to get item list: " << query.error() << endl;
		}
	}

	getchar();
	return 0;
}
注意 ,你想获取第5列第2行的数据,你可以这样写 res[1][4]

另blob数据同样是存在String结构中的,它是一个类似驱动unicode_string的结构体,使用len+buf


通过下标查询代码

把上面的for循环可以改成:只查询res_name这一列的,我们使用对象字段标示进行访问,而不再使用下标。这样做会略微慢一些,但是可读性很好
			for (size_t i = 0; i < res.num_rows(); ++i)
			{
				mysqlpp::String temp = res[i]["res_name"];
				cout << '\t' << temp;
			}

使用输入流查询

		mysqlpp::Query query(&conn);
		std::string cmd_only = "res_name";
		query<<"select res_name from ppdb_resources";
		if (mysqlpp::StoreQueryResult res = query.store())
		{
			for (size_t i = 0; i < res.num_rows(); ++i)
			{
				mysqlpp::String temp = res[i][0];
				cout << '\t' << temp;
			}
		}

绑定BLOB数据

插入一行数据

		const char* db = "ppdb", *server = "192.168.75.129", *user = "root", *pass = "123456";
		mysqlpp::Connection con;
		con.connect(db, server, user, pass);
		Query query = con.query();
		char *pbuf = new char[10];memset(pbuf,0x10,10);
		string img_data;img_data.assign(pbuf,10);
		// INSERT INTO ppdb_resources(res_file,res_name,res_desc,res_hash,res_size,res_seed,res_piece,res_stamp) VALUES('SecureFXPortable.exe','SecureFXPortable.exe','',?,69553,?,1,UNIX_TIMESTAMP())
		query << "INSERT INTO ppdb_resources(res_file,res_name,res_desc,res_hash,res_size,res_seed,res_piece,res_stamp) VALUES('SecureFXPortable.exe','SecureFXPortable.exe','',\""
			<< mysqlpp::escape << img_data <<"\",69553,\""
			<< mysqlpp::escape << img_data <<"\",1,UNIX_TIMESTAMP())";
		SimpleResult res = query.execute();
特别注意<< mysqlpp::escape << img_data <<前后都要使用"\", 说得更明白点,就是blob前后必须加上""

res.copacetic_ = true表示执行成功

更新blob数据

	// UPDate
		query << "update ppdb_resources set res_seed =\""
			<<mysqlpp::escape << img_data <<"\"where res_uid=76";
		SimpleResult res = query.execute();


读取bolb数据

		mysqlpp::Query query = conn.query("select res_hash from ppdb_resources where res_uid=79");
		mysqlpp::StoreQueryResult res = query.store();
		if (res)
		{
			mysqlpp::sql_blob_null blob = res[0][0];
			int iLength = blob.data.length();
			const char* p = blob.data.data();
			printf("%s",p);
		}

SSQLS

sql_create_9(ppdb_resources,
			 1, 9,
			 mysqlpp::sql_int_unsigned, res_uid,
			 mysqlpp::sql_varchar,		res_file,
			 mysqlpp::sql_varchar,		res_name,
			 mysqlpp::sql_varchar_null, res_desc,
			 mysqlpp::sql_tinyblob,		res_hash,
			 mysqlpp::sql_int,			res_size,
			 mysqlpp::sql_mediumblob,	res_seed,
			 mysqlpp::sql_int,			res_piece,
			 mysqlpp::sql_bigint,		res_stamp);

int _tmain(int argc, _TCHAR* argv[])
{
	const char* db = "ppdb", *server = "192.168.75.129", *user = "root", *pass = "123456";
	mysqlpp::Connection conn(false);	// 连接数据库
	if (conn.connect(db, server, user, pass))
	{
		String data;
		char *pBuf = new char[20];
		memset(pBuf,0x20,20);
		data.assign(pBuf,20);
		Query query = conn.query("select res_hash from ppdb_resources where res_uid=10");
		mysqlpp::StoreQueryResult res = query.store();
		if (res)
		{
			ppdb_resources row = res[0];
			ppdb_resources orig_row = row;
			row.res_hash = data;
			query.update(orig_row, row);
			SimpleResult res = query.execute();
			res;
		}
	}
		String data;
		char *pBuf = new char[20];
		memset(pBuf,0x20,20);
		data.assign(pBuf,20);
	
		ppdb_resources img(10, "insert.exe","insert.exe",
			mysqlpp::null,data,
			12345,data,15,1234567841);
		Query query = conn.query();
		query.insert(img);
		SimpleResult res = query.execute();




你可能感兴趣的:(2.DB-Mysql++实例)