以前直接使用mysql API写数据比较麻烦,最近才发现有一个比较好C++封装库mysql++,下载来编译还比较顺利,遇到两个问题:

1、编译例子出错,要将mysql++.bkl文件222行之后增加“<sources>lib/utility.cpp</sources>”,再生成Makefile文件,再编译就没有问题了;

2、例子load_jpeg运行之后BLOB数据没有写入数据库,需要做如下修改:在代码

data.data = static_cast<const stringstream*>(
        &(stringstream() << img_file.rdbuf()))->str();

之后增加:data.is_null = false;

另外一种方式:

/***********************************************************************
 load_jpeg.cpp - Example showing how to insert BLOB data into the
    database from a file.

 Copyright (c) 1998 by Kevin Atkinson, (c) 1999-2001 by MySQL AB, and
 (c) 2004-2009 by Educational Technology Resources, Inc.  Others may
 also hold copyrights on code in this file.  See the CREDITS.txt file
 in the top directory of the distribution for details.

 This file is part of MySQL++.

 MySQL++ is free software; you can redistribute it and/or modify it
 under the terms of the GNU Lesser General Public License as published
 by the Free Software Foundation; either version 2.1 of the License, or
 (at your option) any later version.

 MySQL++ is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with MySQL++; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 USA
***********************************************************************/#include "cmdline.h"
#include "images.h"
#include "printdata.h"#include <fstream>using namespace std;
using namespace mysqlpp;// This is just an implementation detail for the example.  Skip down to
// main() for the concept this example is trying to demonstrate.  You
// can simply assume that, given a BLOB containing a valid JPEG, it
// returns true.
static bool
is_jpeg(const mysqlpp::sql_blob& img, const char** whynot)
{// See http://stackoverflow.com/questions/2253404/ for// justification for the various tests.const unsigned char* idp =reinterpret_cast<const unsigned char*>(img.data());if (img.size() < 125) {*whynot = "a valid JPEG must be at least 125 bytes";}else if ((idp[0] != 0xFF) || (idp[1] != 0xD8)) {*whynot = "file does not begin with JPEG sigil bytes";}else if ((memcmp(idp + 6, "JFIF", 4) != 0) &&(memcmp(idp + 6, "Exif", 4) != 0)) {*whynot = "file does not contain JPEG type word";}else {*whynot = 0;return true;}return false;
}// Skip to main() before studying this.  This is a little too
// low-level to bother with on your first pass thru the code.
static bool
load_jpeg_file(const mysqlpp::examples::CommandLine& cmdline,mysqlpp::sql_blob_null& data, string& img_name)
{if (cmdline.extra_args().size() == 0) {// Nothing for us to do here.  Caller will insert NULL BLOB.return true;}// Got a file's name on the command line, so open it.img_name = cmdline.extra_args()[0];ifstream img_file(img_name.c_str(), ios::binary);if (img_file) {// Slurp file contents into RAM with minimum copying.  (Idiom// explained here: http://stackoverflow.com/questions/116038/)//// By loading the file into a C++ string (stringstream::str())// and assigning that directly to a mysqlpp::sql_blob, we avoid// truncating the binary data at the first null character.data.data = static_cast<const stringstream*>(&(stringstream() << img_file.rdbuf()))->str();// Check JPEG data for sanity.const char* error;if (is_jpeg(data.data, &error)) {return true;}else {cerr << '"' << img_name << "\" isn't a JPEG: " <<error << '!' << endl;}}cmdline.print_usage("[jpeg_file]");return false;
}int
main(int argc, char *argv[])
{// Get database access parameters from command linemysqlpp::examples::CommandLine cmdline(argc, argv);if (!cmdline) {return 1;}try {// Establish the connection to the database server.mysqlpp::Connection con(mysqlpp::examples::db_name,cmdline.server(), cmdline.user(), cmdline.pass());// Load the file named on the command linemysqlpp::sql_blob_null data;string img_name("NULL");if (load_jpeg_file(cmdline, data, img_name)) {images img(mysqlpp::null, data);// Insert image data or SQL NULL into the images.data BLOB// column.  The key here is that we're holding the raw// binary data in a mysqlpp::sql_blob, which avoids data// conversion problems that can lead to treating BLOB data// as C strings, thus causing null-truncation.  The fact// that we're using SSQLS here is a side issue, simply// demonstrating that mysqlpp::Null<mysqlpp::sql_blob> is// now legal in SSQLS, as of MySQL++ 3.0.7.ostringstream os1;os1<<img.value_list()<<std::endl;//std::cout<<os1.str();Query query = con.query();ostringstream os;os<<img.data.data;//query << "INSERT INTO images (data) VALUES (\""  << mysqlpp::escape << os.str() << "\")";//query.execute();query.insert(img);SimpleResult res = query.execute();// Report successful insertioncout << "Inserted \"" << img_name <<"\" into images table, " << img.data.data.size() <<" bytes, ID " << res.insert_id() << endl;}}catch (const BadQuery& er) {// Handle any query errorscerr << "Query error: " << er.what() << endl;return -1;}catch (const BadConversion& er) {// Handle bad conversionscerr << "Conversion error: " << er.what() << endl <<"\tretrieved data size: " << er.retrieved <<", actual size: " << er.actual_size << endl;return -1;}catch (const Exception& er) {// Catch-all for any other MySQL++ exceptionscerr << "Error: " << er.what() << endl;return -1;}return 0;
}

转载于:https://www.cnblogs.com/haichang/archive/2011/04/03/2004359.html

使用mysql++写入BLOB数据相关推荐

  1. mysql插入blob报错_java如何向mysql写入blob数据?

    表结构如下 package entity; import java.io.Serializable; public class Person implements Serializable{ priv ...

  2. mysql++读写BLOB数据

    mysql++读写BLOB数据 1.使用sql_create_n宏函数,建立数据库表字段与对象.    #define sql_create_2(NAME, CMP, CONTR, T1, I1, T ...

  3. Mysql 查询blob数据很慢,关于oracle中clob字段查询慢的问题及解决方法

    关于oracle中clob字段查询慢的问题及解决方法 最近在用oracle的过程中用到了对blob字段模糊查询的问题,对oracle来说,我并不是高手,找了很多的资料终于能够查出来了. blob字段直 ...

  4. Mysql 查询blob数据很慢_blob存入数据库很慢

    {"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],&q ...

  5. Mysql写入数据时,adapter 日志报ES连接错误

    Mysql写入数据时,adapter 日志报ES连接错误 日志如下: 2021-09-20 13:51:03.795 [pool-1-thread-1] ERROR c.a.otter.canal.a ...

  6. java aes mysql blob_使用带有ORDER子句的AES_DECRYPT在MySQL中返回BLOB数据

    我正在创建一个系统,用户可以通过PHP使用MySQL数据库存储消息,我使用MySQL AES_ENCRYPT函数来加密这些消息的内容. 这是我的帖子表: CREATE TABLE IF NOT EXI ...

  7. mysql 数据缓冲区,MySQL写入缓冲区在数据库中的作用( Change Buffer )

    原标题:MySQL写入缓冲区在数据库中的作用( Change Buffer ) 介绍另外一种重要的数据变更日志,也就是InnoDB change buffer.Change buffer的主要目的是将 ...

  8. mysql百万数据写入_快速写入百万数据

    1.快速写入百万数据 mysql原生操作 mysql原生操作写入500万数据! # 建库 create database mydb01; # 使用库 use mydb01; # 建表 create t ...

  9. 向mysql写入时间_Python向Mysql写入时间类型数据

    mysql中字段包括date和datetime两种时间类型,分别介绍如何使用Python向mysql写入上述两种时间类型的数据(主要为sql语句): 1.date类型 date = datetime. ...

最新文章

  1. 跨域?拒绝说概念(内含demo)
  2. linux文件删除漏洞,OpenStack Glance v1 API任意文件删除漏洞(CVE-2012-4573)
  3. Java 关于File使用
  4. Greenplum【环境搭建 02】GP 数据库 web 监控工具 Greenplum Command Center v6.6.0 安装配置及问题处理(一篇学会部署配置启动 GPCC 避坑指南)
  5. CodeDay 北京站报名倒计时
  6. 写MySQL存储过程实现动态执行SQL
  7. (数据科学学习手札30)朴素贝叶斯分类器的原理详解Python与R实现
  8. 你不出去卖我拿什么养你_玉树枝干“胳膊粗”,至少养了20年,给钱也不卖!...
  9. vim上下左右键输出A B
  10. [转]OpenCV学习笔记大集锦
  11. uni app input添加获取验证码按钮_FILEX | 如何在UniSwap挖到UNI?
  12. 日志的打印 —— Java 支持
  13. python开发软件的实例-Python 开发工具和框架安装实例步骤
  14. SpringCloud 配置服务器
  15. MAC硬盘空间减少的隐藏杀手,VM到底是什么?
  16. html信纸效果,纯css3实现信纸/同学录效果(代码示例)
  17. html网页制作代码大全:庆余年——电影网站7页,不包含js 有登陆注册,表格 table布局 ,有的登录注册页面,内嵌 css
  18. 项目: 生命游戏(C语言)
  19. Cortex-M0和Cortex-M0+的异常类型
  20. android百度地图poi路线规划,百度地图开发之poi检索,线路规划(示例代码)

热门文章

  1. 华为杯数学建模优秀论文_数学建模经典例题(2016年国赛B题与优秀论文)
  2. php 类 和 函数,PHP函数和类
  3. 60-100-032-使用-MySQL大小写敏感的解决方法
  4. ERROR streaming.StreamExecution:createConsumer(ConsumerStrategy.scala:63)
  5. 95-138-010-源码-Function-ReduceFunction
  6. 07-霸道的Oracle内存使用
  7. 02=windows下安装PostgreSQL(The database cluster initialisation failed)
  8. Django ORM 常用的查询方法
  9. Java forEach() 方法的三种用法
  10. Mybatis源码解析:sql参数处理(1)