資源簡介
mysql寫入或讀取一段數據塊時,數據類型為blob型,本代碼詳細介紹在mysqll中如何讀寫blob格式數據
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?“mysql_connection.h“
#include?“mysql_driver.h“
#include?
#include?
#include?
#include?
#include?
using?namespace?std;
class?DataBuf?:?public?std::streambuf
{
public:
????DataBuf(char*?d?size_t?s)
????{
????????setg(d?d?d+s);
????}
};
int?main(int?argc?const?char?**argv)
{
cout?<“Mysql?Connector/C++?Test“?<
try?{
sql::mysql::MySQL_Driver?*driver;
sql::Connection?*con;
sql::Statement?*stmt;
driver?=?sql::mysql::get_mysql_driver_instance();
con?=?driver->connect(“tcp://127.0.0.1:3306“?“root“?“123456“);
stmt?=?con->createStatement();
stmt->execute(“USE?test“);
stmt->execute(“DROP?TABLE?IF?EXISTS?test_a“);
stmt->execute(“CREATE?TABLE?test_a(id?INT?primary?key?label?CHAR(1))“);
stmt->execute(“INSERT?INTO?test_a(id?label)?VALUES?(1?‘a‘)“);
stmt->execute(“DROP?TABLE?IF?EXISTS?test_b“);
stmt->execute(“CREATE?TABLE?test_b(id?INT?primary?key?b?BLOB)“);
//寫入blob字段
sql::PreparedStatement*?pstmt;
pstmt?=?con->prepareStatement(“INSERT?INTO?test_b?VALUES(???)“);
int?a?=?1;
char?buf[64]?=?“I?am?a?good?boy!“;
//memcpy(buf&asizeof(a));
//memcpy(buf?+?sizeof(a)&asizeof(a));
//memcpy(buf?+?2?*?sizeof(a)&asizeof(a));
DataBuf?buffer(buf?strlen(buf));
std::istream?s(&buffer
評論
共有 條評論