Mysql LONGTEXT 類型儲存大檔案(二進位也可以) (修改+調試+整理)

來源:互聯網
上載者:User

#include "stdafx.h"
//是前一篇的姊妹篇
//代碼來自網路,我學習整理了一下,測試通過,下面的參數
//需要設定為你自己的
//在DBMS中線要建立資料庫www,table www,file欄位資料類型用LONGTEXT即可測試
//測試檔案c:\\test.iso,你可以找任何一個檔案修改為即可,我找的是一個exe程式,修改為test.iso而已
//最大測試過加入檔案大小為650M(一個正真的iso檔案)
//注意:還要修改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 <Winsock2.h>
#include <stdio.h>
#include <mysql.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define host "localhost" //mysql server
#define username "root"
#define password "674800"
#define database "www"
int port = 3306;
#pragma comment(lib,"libmysql.lib")
//得到檔案的大小(位元組數)
int get_file_size(char *path, off_t *size)
{
struct stat file_stats;
if(stat(path, &file_stats))
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 (argc != 2)
{
printf("Usage: %s srcfile\n", argv[0]);
exit(1);
}
*/
filename = "c:\\test.iso";
if ((get_file_size(filename, &size)) == -1) //得到檔案的大小
{
perror("get file size" );
exit(1);
}
if ((buf = (char *)malloc(sizeof(char)*(size+1))) == NULL)
{
perror("malloc buf" );
exit(1);
}
if ((fp = fopen(filename, "rb" )) == NULL) //讀檔案
{
perror("fopen file" );
exit(1);
}
if ((n = fread(buf, 1, size, fp)) < 0) //讀檔案失敗
{
perror("fread file" );
exit(1);
}
sql = (char *)malloc(sizeof(char)*n*2+256); //2n+1+strlen(other sql)
if (sql == NULL)
{
perror("malloc sql" );
exit(1);
}
conn = mysql_init(NULL);//生產一個mysql對象
if (conn == NULL)
{
printf("init mysql, %s\n", mysql_error(conn));
exit(1);
}
if ((mysql_real_connect(conn, host, username, password, database, port, NULL, 0)) == NULL) //串連伺服器
{
printf("connect mysql, %s\n", mysql_error(conn));
exit(1);
}
strcpy(sql, "insert into www(id, name, file) values(NULL, 'peter', " );
end = sql;
end += strlen(sql); //point sql tail
//convert NUL(ASCII 0)、'\n'、'\r'、'\''、'''、'"'和Control-Z and so on
*end++ = '\'';
end += mysql_real_escape_string(conn, end, buf, n);
*end++ = '\'';
*end++ = ')';
flag = mysql_real_query(conn, sql, (unsigned int)(end-sql));
if (flag != 0)
{
printf("insert failed, %s\n", mysql_error(conn));
exit(1);
}
if ((mysql_real_query(conn, "SELECT file FROM www where id=1", 31)) != 0)
{
printf("insert failed, %s\n", mysql_error(conn));
exit(1);
}
res_set = mysql_store_result(conn);
fclose(fp);
fp = NULL;
fp = fopen("c:\\123.iso", "wb" );
while ((row = mysql_fetch_row(res_set)) != NULL)
{
length = mysql_fetch_lengths(res_set);
for (i=0; i<mysql_num_fields(res_set); i++)
{
fwrite(row[0], 1, length[0], fp);
//printf("%s\n",row[0]);
}
}
fclose(fp);
mysql_close(conn);
free(sql);
free(buf);
sql = NULL;
return 0;
}

運行結果:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.