Mysql LONGTEXT 类型存储大文件(二进制也可以) (修改+调试+整理)

  • A+
所属分类:数据库

MySql2.cpp : Defines the entry point for the console application.

#include "stdafx.h"

//是前一篇的姐妹篇

//代码来自网络,我学习整理了一下,测试通过,下面的参数

//需要设置为你自己的

//在DBMS中线要创建数据库www,table www,file字段数据类型用LONGTEXT即可测试

//测试文件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. #include <Winsock2.h>
  8. #include <stdio.h>
  9. #include <mysql.h>
  10. #include <stdlib.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #define host "localhost" //mysql server 
  15. #define username "root"
  16. #define password "674800"
  17. #define database "www"
  18. int port = 3306;
  19. #pragma comment(lib,"libmysql.lib")
  20. //得到文件的大小(字节数) 
  21. int get_file_size(char *path, off_t *size)
  22. {
  23. struct stat file_stats;
  24. if(stat(path, &file_stats))
  25. return -1;
  26. *size = file_stats.st_size;
  27. return 0;
  28. }
  29. int main(int argc, char *argv[])
  30. {
  31. char *filename=NULL;
  32. off_t size;
  33. MYSQL *conn=NULL;
  34. MYSQL_RES *res_set=NULL;
  35. MYSQL_ROW row;
  36. MYSQL_FIELD *field=NULL;
  37. int i, flag;
  38. char *sql; //sql语句 
  39. FILE *fp;
  40. char *buf;
  41. int n=256;
  42. char *end;
  43. unsigned long *length;
  44. /* if (argc != 2) 
  45. printf("Usage: %s srcfile\n", argv[0]); 
  46. exit(1); 
  47. */
  48. filename = "c:\\test.iso";
  49. if ((get_file_size(filename, &size)) == -1//得到文件的大小 
  50. {
  51. perror("get file size" );
  52. exit(1);
  53. }
  54. if ((buf = (char *)malloc(sizeof(char)*(size+1))) == NULL)
  55. {
  56. perror("malloc buf" );
  57. exit(1);
  58. }
  59. if ((fp = fopen(filename, "rb" )) == NULL) //读文件 
  60. {
  61. perror("fopen file" );
  62. exit(1);
  63. }
  64. if ((n = fread(buf, 1, size, fp)) < 0//读文件失败 
  65. {
  66. perror("fread file" );
  67. exit(1);
  68. }
  69. sql = (char *)malloc(sizeof(char)*n*2+256); //2n+1+strlen(other sql) 
  70. if (sql == NULL)
  71. {
  72. perror("malloc sql" );
  73. exit(1);
  74. }
  75. conn = mysql_init(NULL);//生产一个mysql对象 
  76. if (conn == NULL)
  77. {
  78. printf("init mysql, %s\n", mysql_error(conn));
  79. exit(1);
  80. }
  81. if ((mysql_real_connect(conn, host, username, password, database, port, NULL, 0)) == NULL) //连接服务器 
  82. {
  83. printf("connect mysql, %s\n", mysql_error(conn));
  84. exit(1);
  85. }
  86. strcpy(sql, "insert into www(id, name, file) values(NULL, 'peter', " );
  87. end = sql;
  88. end += strlen(sql); //point sql tail 
  89. //convert NUL(ASCII 0)、'\n'、'\r'、'\''、'''、'"'和Control-Z and so on 
  90. *end++ = '\'';
  91. end += mysql_real_escape_string(conn, end, buf, n);
  92. *end++ = '\'';
  93. *end++ = ')';
  94. flag = mysql_real_query(conn, sql, (unsigned int)(end-sql));
  95. if (flag != 0)
  96. {
  97. printf("insert failed, %s\n", mysql_error(conn)); 
  98. exit(1); 
  99. if ((mysql_real_query(conn, "SELECT file FROM www where id=1", 31)) != 0) 
  100. printf("insert failed, %s\n", mysql_error(conn)); 
  101. exit(1); 
  102. res_set = mysql_store_result(conn); 
  103. fclose(fp); 
  104. fp = NULL; 
  105. fp = fopen("c:\\123.iso", "wb" ); 
  106. while ((row = mysql_fetch_row(res_set)) != NULL) 
  107. length = mysql_fetch_lengths(res_set); 
  108. for (i=0; i<mysql_num_fields(res_set); i++) 
  109. fwrite(row[0], 1, length[0], fp); 
  110. //printf("%s\n",row[0]);
  111. }
  112. }
  113. fclose(fp);
  114. mysql_close(conn);
  115. free(sql);
  116. free(buf);
  117. sql = NULL;
  118. return 0;
  119. }

运行结果:

Mysql LONGTEXT 类型存储大文件(二进制也可以) (修改+调试+整理)

图片引用自网络