赞助商
近日一项目遇到需要在DB中存储二进制数据流类型文件的问题,发现常用的mysql API都用不了,再研究,方知有一套专门的API来干这种数据,功能相当强大的说。

以下即为范例代码 --- 按照说明编译即可用,稍加修改即可存储二进制文件
  1. view plaincopy to clipboardprint?
  2. /* 
  3. mysql数据库存储二进制数据 linux 

  4. 用途: 用 mysql_stmt_send_long_data()来向blob字段写入二进制数据流. 

  5. 注意点:需要注意的是bind结构的buffer_type字段,必须与要输入的数据类型相符, 
  6. 如:只写入一个long 数据,则用MYSQL_TYPE_LONG,写入字符流,用MYSQL_TYPE_STRING, 
  7. 写入2进制数据流,用MYSQL_TYPE_BLOB 
  8. 具体这个参数各字段的含义参见 mysql5.0手册 

  9. Compile: g++ -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient mysql_test.cpp 

  10. 准备工作: 
  11. create database test; 
  12. use test; 
  13. CREATE TABLE `bintest` ( 
  14. `id` int(11) NOT NULL default '0', 
  15. `data` blob 
  16. ) ENGINE=MyISAM; 
  17. */ 
  18.  
  19. #include <mysql.h> 
  20. #include <string.h> 
  21. #include <stdio.h> 
  22. #include <stdlib.h> 
  23.  
  24.  
  25. #define INSERT_QUERY "INSERT INTO bintest(id, data) VALUES(4, ?)" 
  26.  
  27. void test() 

  28. MYSQL_BIND bind[1]; 
  29. unsigned long      length; 
  30.  
  31. char blog_data[100] = {0}; 
  32. memset(blog_data, 0x01, sizeof(blog_data)); 
  33.  
  34. char* pos = blog_data; 
  35. int size = 50; 
  36.  
  37. MYSQL *mysql = mysql_init(NULL); 
  38. if (!mysql) return;   
  39. if (!mysql_real_connect(mysql, 
  40.   "192.168.xx.xxx", 
  41.   "root", 
  42.   "db_user_name", 
  43.   "test", 
  44.   3306, NULL, 0))   

  45.   int ret = mysql_errno(mysql);   
  46.   mysql_close(mysql); 
  47.   return; 

  48.  
  49. MYSQL_STMT *stmt = mysql_stmt_init(mysql); 
  50. if (!stmt) 

  51.   fprintf(stderr, " mysql_stmt_init(), out of memory\n"); 
  52.   exit(0); 

  53. if (mysql_stmt_prepare(stmt, INSERT_QUERY, strlen(INSERT_QUERY))) 

  54.   fprintf(stderr, "\n mysql_stmt_prepare(), INSERT failed"); 
  55.   fprintf(stderr, "\n %s", mysql_stmt_error(stmt)); 
  56.   exit(0); 

  57. memset(bind, 0, sizeof(bind)); 
  58. //bind[0].buffer_type= MYSQL_TYPE_STRING; 
  59. //bind[0].buffer_type = MYSQL_TYPE_LONG; 
  60. bind[0].buffer = blog_data; 
  61. //bind[0].buffer_type = MYSQL_TYPE_TINY; 
  62. bind[0].buffer_type = MYSQL_TYPE_BLOB; 
  63. bind[0].length= &length; 
  64. bind[0].is_null= 0; 
  65.  
  66. /* Bind the buffers */ 
  67. if (mysql_stmt_bind_param(stmt, bind)) 

  68.   fprintf(stderr, "\n param bind failed"); 
  69.   fprintf(stderr, "\n %s", mysql_stmt_error(stmt)); 
  70.   exit(0); 

  71.  
  72. int rc =0; 
  73. /* Supply data in chunks to server */ 
  74. if (mysql_stmt_send_long_data(stmt,0, pos, size)) 

  75.   fprintf(stderr, "\n send_long_data failed"); 
  76.   fprintf(stderr, "\n %s", mysql_stmt_error(stmt)); 
  77.   exit(0); 

  78.  
  79. pos += size; 
  80.  
  81. /* Supply the next piece of data */ 
  82. if (mysql_stmt_send_long_data(stmt,0, pos, size)) 

  83.   fprintf(stderr, "\n send_long_data failed"); 
  84.   fprintf(stderr, "\n %s", mysql_stmt_error(stmt)); 
  85.   exit(0); 

  86.  
  87. /* Now, execute the query */ 
  88. if (mysql_stmt_execute(stmt)) 

  89.   fprintf(stderr, "\n mysql_stmt_execute failed"); 
  90.   fprintf(stderr, "\n %s", mysql_stmt_error(stmt)); 
  91.   exit(0); 
  92. }   

  93.  
  94.  
  95. int main() 

  96. test(); 
  97. //sleep(1); 
  98. return 0; 
  99. }
复制代码
文/galaxy_fxstar  出处/CSDN
赞助商
赞助商
TOP