在C 中读取二进制数据,然后写入MySQL
Read Binary Data in C++ and Write to MySQL
我有一个插入函数,该功能打开图像文件并插入数据库。
void insertBlob()
{
sql::Driver *driver;
sql::Connection *con;
sql::PreparedStatement *pstmt;
ifstream file ("/home/malper/NetBeansProjects/BlobTesting/2", ios::in | ios::binary | ios::ate);
ifstream::pos_type fileSize;
char* fileContents;
char buff[1024];
if(file.is_open())
{
fileSize = file.tellg();
fileContents = new char[fileSize];
file.seekg(0, ios::beg);
if(!file.read(fileContents, fileSize))
{
cout << "fail to read" << endl;
}
file.close();
}
istringstream str(fileContents);
/////////////////////////////////////////////////
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://192.168.2.5:3306", "root", "root");
/* Connect to the MySQL test database */
con->setSchema("test");
pstmt=con->prepareStatement("INSERT INTO Dao VALUES(57,4,'2014-12-23 11:55:54',?,1,1)");
pstmt->setBlob(1,&str);
pstmt->executeUpdate();
}
数据库部分运行良好,但是在打开文件后我无法将二进制图像归于Stringstream。如何将二进制存储在此功能中?
std :: stringstream构造函数期望std :: string作为参数。这可以由filecontent制成:
std::string tempContent(fileContents, fileSize);
std::stringstream str(tempContent);
这仍然很奇怪,因为没有制作字符串来处理二进制数据。
您需要的是二进制数据的ASCII表示。
幸运的是,您不必组成一个,base64是一种标准格式。
可以从base64来回转换数据的解决方案:这应该是一个很好的开始。
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- C++MySQL C api用户输入行
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 这是我尝试让用户将值输入到数组中.然后将其隐藏为大量的星号
- boost::asio如何生成多个协同程序,然后加入它们
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- 在std::thread中,joinable()然后join()线程安全吗
- 使用mysql c++连接器的未定义引用
- 无法在C++中建立与MySQL数据库的连接
- C++:如何读取分离变量,然后读取向量
- mysql C++ connector [mysql-connector-c++-8.0.19-linux-glibc2
- 为什么我的递归函数按降序打印,然后按升序打印?
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- 等待整个 omp 块完成,然后再调用第二个函数
- CMake:如何将库 A 链接到库 B,然后将可执行文件链接到库 A
- Poco FindMySql.cmake 无法找到 MySQL 源代码
- MySQL C++连接器链接错误
- 如何存储用户输入的所有数据,然后在他们想要查看所有数据时显示它们
- 在C 中读取二进制数据,然后写入MySQL