Mysql LONGBLOB 类型存储二进制数据

  • A+
所属分类:数据库

代码来自网络,我学习整理了一下,测试通过,下面的参数需要设置为你自己的

在DBMS中线要创建数据库test,table bintest,data字段数据类型用LONGBLOB即可测试

//测试文件c:\\test.iso,你可以找任何一个文件修改为即可,我找的是一个exe程序,修改为test.iso而已

//最大测试过加入文件大小为650M(一个正真的iso文件)

//注意:还要修改my.ini文件中的max_allowed_packet字段,我设置的是

  1. //max_allowed_packet = 1024M 
  2. //#define host "localhost" //mysql server 
  3. //#define username "root" 
  4. //#define password "674800" 
  5. //#define database "test" 
  6. //int port = 3306; 
  7. // Mysql3.cpp : Defines the entry point for the console application. 
  8. // 
  9. #include "stdafx.h"
  10. #include <Winsock2.h>
  11. #include <mysql.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <fcntl.h>
  18. #pragma comment(lib,"libmysql.lib")
  19. #define INSERT_QUERY "INSERT INTO bintest(id, data) VALUES(NULL, ?)"
  20. #define host "localhost" //mysql server 
  21. #define username "root"
  22. #define password "674800"
  23. #define database "test"
  24. int port = 3306;
  25. int get_file_size(char *path, off_t *size)
  26. {
  27. struct stat file_stats;
  28. if(stat(path, &file_stats))
  29. return -1;
  30. *size = file_stats.st_size;
  31. return 0;
  32. }
  33. void test()
  34. {
  35. MYSQL_BIND bind[1];
  36. unsigned long length;
  37. char* pos = NULL;
  38. off_t size;
  39. FILE* fp;
  40. char* filename = "c:\\test.iso";
  41. if ((get_file_size(filename, &size)) == -1//得到文件的大小 
  42. {
  43. perror("get file size" );
  44. exit(1);
  45. }
  46. if ((pos = (char *)malloc(sizeof(char)*(size+1))) == NULL)
  47. {
  48. perror("malloc buf" );
  49. exit(1);
  50. }
  51. if ((fp = fopen(filename, "rb" )) == NULL) //读文件 
  52. {
  53. perror("fopen file" );
  54. exit(1);
  55. }
  56. if ((fread(pos, 1, size, fp)) < 0//读文件失败 
  57. {
  58. perror("fread file" );
  59. exit(1);
  60. }
  61. MYSQL *mysql = mysql_init(NULL); //mysql 初始化 
  62. if (!mysql)
  63. return;
  64. if (!mysql_real_connect(mysql,host,username,password,"test",port,NULL,0))//链接服务器 
  65. {
  66. int ret = mysql_errno(mysql);
  67. mysql_close(mysql);
  68. return;
  69. }
  70. MYSQL_STMT *stmt = mysql_stmt_init(mysql);
  71. if (!stmt)
  72. {
  73. fprintf(stderr, " mysql_stmt_init(), out of memory\n");
  74. exit(0);
  75. }
  76. if (mysql_stmt_prepare(stmt, INSERT_QUERY, strlen(INSERT_QUERY)))
  77. {
  78. fprintf(stderr, "\n mysql_stmt_prepare(), INSERT failed");
  79. fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
  80. exit(0);
  81. }
  82. memset(bind, 0, sizeof(bind));
  83. //bind[0].buffer_type= MYSQL_TYPE_STRING; 
  84. //bind[0].buffer_type = MYSQL_TYPE_LONG; 
  85. bind[0].buffer = pos;
  86. //bind[0].buffer_type = MYSQL_TYPE_TINY; 
  87. bind[0].buffer_type = MYSQL_TYPE_BLOB;
  88. bind[0].length= &length;
  89. bind[0].is_null= 0;
  90. /* Bind the buffers */
  91. if (mysql_stmt_bind_param(stmt, bind))
  92. {
  93. fprintf(stderr, "\n param bind failed");
  94. fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
  95. exit(0);
  96. }
  97. int rc =0;
  98. /* Supply data in chunks to server */
  99. if (mysql_stmt_send_long_data(stmt,0, pos, size))
  100. {
  101. fprintf(stderr, "\n send_long_data failed");
  102. fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
  103. exit(0);
  104. }
  105. // pos += size; 
  106. /* Supply the next piece of data */
  107. if (mysql_stmt_send_long_data(stmt,0, pos, size))
  108. {
  109. fprintf(stderr, "\n send_long_data failed");
  110. fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
  111. exit(0);
  112. }
  113. /* Now, execute the query */
  114. if (mysql_stmt_execute(stmt))
  115. {
  116. fprintf(stderr, "\n mysql_stmt_execute failed");
  117. fprintf(stderr, "\n %s", mysql_stmt_error(stmt));
  118. exit(0);
  119. }
  120. }
  121. int main()
  122. {
  123. test();
  124. //sleep(1); 
  125. return 0;
  126. }

运行结果:

Mysql LONGBLOB 类型存储二进制数据

图片引用自网络