Mysql LONGTEXT 类型存储大文件_Mysql_脚本之家

发布时间:2019-12-12  栏目:计算机教程  评论:0 Comments

   process_result_set(mydata, result);

#include “stdafx.h” //是前一篇的姊妹篇
//代码来自网络,我学习整理了一下,测试通过,下面的参数
//需要设置为你自己的 //在DBMS中线要创建数据库www,table
www,file字段数据类型用LONGTEXT即可测试
//测试文件c:\\test.iso,你可以找任何一个文件修改为即可,我找的是一个exe程序,修改为test.iso而已
//最大测试过加入文件大小为650M
//注意:还要修改my.ini文件中的max_allowed_packet字段,我设置的是 复制代码 代码如下: //max_allowed_packet =
1024M //#define host “localhost” //mysql server //#define username
“root” //#define password “674800” //#define database “test” //int
port = 3306; #include #include #include #include #include #include
#include #define host “localhost” //mysql server #define username
“root” #define password “674800” #define database “www” int port =
3306; #pragma comment //得到文件的大小 int get_file_size(char *path,
off_t *size) { struct stat file_stats; if(stat return -1; *size =
file_stats.st_size; return 0; } int main(int argc, char *argv[]) {
char *filename=NULL; off_t size; MYSQL *conn=NULL; MYSQL_RES
*res_set=NULL; MYSQL_ROW row; MYSQL_FIELD *field=NULL; int i, flag;
char *sql; //sql语句 FILE *fp; char *buf; int n=256; char *end;
unsigned long *length; /* if { printf(“Usage: %s srcfile\n”,
argv[0]); exit; } */ filename = “c:\\test.iso”; if
((get_file_size == -1) //得到文件的大小 { perror; exit; } if malloc))
== NULL) { perror; exit; } if ((fp = fopen == NULL) //读文件 { perror;
exit; } if ((n = fread < 0) //读文件失败 { perror; exitmalloc;
//2n+1+strlen if { perror; exit; } conn =
mysql_init;//生产一个mysql对象 if { printf(“init mysql, %s\n”,
mysql_error; } if ((mysql_real_connect(conn, host, username,
password, database, port, NULL, 0)) == NULL) //连接服务器 {
printf(“connect mysql, %s\n”, mysql_error; } strcpy(sql, “insert into
www values; end = sql; end += strlen; //point sql tail //convert
NUL、’\n’、’\r’、’\”、”’、'”‘和Control-Z and so on *end++ = ‘\”;
end += mysql_real_escape_string; *end++ = ‘\”; *end++ = ‘)’; flag
= mysql_real_query(conn, sql, ; if { printf(“insert failed, %s\n”,
mysql_error; } if ((mysql_real_query(conn, “SELECT file FROM www
where id=1”, 31)) != 0) { printf(“insert failed, %s\n”, mysql_error; }
res_set = mysql_store_result; fp = NULL; fp = fopen; while ((row =
mysql_fetch_row { length = mysql_fetch_lengths; for (i=0; i

  }

 strcpy(p.user,”root”);

  return -1;

   printf(“connect database fail,%s\n”,mysql_error(&mydata));

 return 1;

 

 MYSQL_FIELD *fields = NULL;

 while((currow = mysql_fetch_row(result)) != NULL)

 }

 process_result_set(mydata,result);

  return -1;

 }

#define MYSQL_PORT 3306

  exit(mydata,”set names fail”);

 insert into fangl.admin(username,password)
values(‘fl1′,’fl1’),(‘fl2′,’fl2’),(‘fl3′,’fl3’);\

  /* more results? -1 = no, >0 = error, 0 = yes (keep looping) */

  {

   printf(“%s “,currow[i]?currow[i]:”NULL”);

  }

  mysql_free_result(result);

void process_result_set(MYSQL mysql, MYSQL_RES *result);

 if(result != NULL)

}

 else

 strcpy(p.password,”root”);

 //取得各字段名

  exit(mydata,”execute sql syntax fail”);

 system(“pause”);

  printf(“init mysql data stauct fail\n”);

  if(NULL == mysql_real_connect(&mydata,p.host,p.user,p.password,

 //取得查询结果

 //依次读取各条记录

   system(“pause”);

 }

 //初始化

 if(mysql_query(&mydata,queryName) != 0)

  for(int i = 0; i < mysql_num_fields(result); ++i)

 do {

  return -1;

 strcpy(queryName,”delete from fangl.admin where username=’fl1′”);

  return -1;

 //查询数据(支持单个字段和多个字段,输出排版)

   else  /* some error occurred */

 {

 

 for(int i=0;fields=mysql_fetch_field(result);i++)

  }

   p.db,p.port,NULL,CLIENT_MULTI_STATEMENTS))

  if (result)

    break;

   if (mysql_field_count(&mydata) == 0)

    printf(“%lld rows affected\n”,

 {

  /* did current statement return data? */

  printf(“%s “,fields->name);

void process_result_set(MYSQL mydata, MYSQL_RES *result)

   return -1;

 {

 {

本文出自 “lynn” 博客

#include “CMySQL.h”

}

 select * from fangl.admin”);

  return -1;

 MYSQL_ROW currow = NULL;

     mysql_affected_rows(&mydata));

 }

 strcpy(queryName,”show databases”);

 strcpy(p.host,”127.0.0.1″);

 }

  return -1;

 //初始化数据结构

  {

 int rowcount = mysql_num_rows(result);       //查询结果有多少行

   /* yes; process rows and free the result set */

留下评论

网站地图xml地图