一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|數(shù)據(jù)庫技術(shù)|

服務(wù)器之家 - 數(shù)據(jù)庫 - Mysql - Mysql LONGBLOB 類型存儲二進制數(shù)據(jù) (修改+調(diào)試+整理)

Mysql LONGBLOB 類型存儲二進制數(shù)據(jù) (修改+調(diào)試+整理)

2019-10-30 17:44mysql教程網(wǎng) Mysql

Mysql LONGBLOB 類型存儲二進制數(shù)據(jù) (修改+調(diào)試+整理),代碼來自網(wǎng)絡(luò),我學(xué)習(xí)整理了一下,測試通過,下面的參數(shù)需要設(shè)置為你自己的

在DBMS中線要創(chuàng)建數(shù)據(jù)庫test,table bintest,data字段數(shù)據(jù)類型用LONGBLOB即可測試 
//測試文件c:\\test.iso,你可以找任何一個文件修改為即可,我找的是一個exe程序,修改為test.iso而已 
//最大測試過加入文件大小為650M(一個正真的iso文件) 
//注意:還要修改my.ini文件中的max_allowed_packet字段,我設(shè)置的是 

復(fù)制代碼代碼如下:


//max_allowed_packet = 1024M 
//#define host "localhost" //mysql server 
//#define username "root" 
//#define password "674800" 
//#define database "test" 
//int port = 3306; 
// Mysql3.cpp : Defines the entry point for the console application. 
// 
#include "stdafx.h" 
#include <Winsock2.h> 
#include <mysql.h> 
#include <string.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#pragma comment(lib,"libmysql.lib") 
#define INSERT_QUERY "INSERT INTO bintest(id, data) VALUES(NULL, ?)" 
#define host "localhost" //mysql server 
#define username "root" 
#define password "674800" 
#define database "test" 
int port = 3306; 
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; 

void test() 

MYSQL_BIND bind[1]; 
unsigned long length; 
char* pos = NULL; 
off_t size; 
FILE* fp; 
char* filename = "c:\\test.iso"; 
if ((get_file_size(filename, &size)) == -1) //得到文件的大小 

perror("get file size" ); 
exit(1); 

if ((pos = (char *)malloc(sizeof(char)*(size+1))) == NULL) 

perror("malloc buf" ); 
exit(1); 

if ((fp = fopen(filename, "rb" )) == NULL) //讀文件 

perror("fopen file" ); 
exit(1); 

if ((fread(pos, 1, size, fp)) < 0) //讀文件失敗 

perror("fread file" ); 
exit(1); 

MYSQL *mysql = mysql_init(NULL); //mysql 初始化 
if (!mysql) 
return; 
if (!mysql_real_connect(mysql,host,username,password,"test",port,NULL,0))//鏈接服務(wù)器 

int ret = mysql_errno(mysql); 
mysql_close(mysql); 
return; 

MYSQL_STMT *stmt = mysql_stmt_init(mysql); 
if (!stmt) 

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

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

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

memset(bind, 0, sizeof(bind)); 
//bind[0].buffer_type= MYSQL_TYPE_STRING; 
//bind[0].buffer_type = MYSQL_TYPE_LONG; 
bind[0].buffer = pos; 
//bind[0].buffer_type = MYSQL_TYPE_TINY; 
bind[0].buffer_type = MYSQL_TYPE_BLOB; 
bind[0].length= &length; 
bind[0].is_null= 0; 
/* Bind the buffers */ 
if (mysql_stmt_bind_param(stmt, bind)) 

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

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

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

// pos += size; 
/* Supply the next piece of data */ 
if (mysql_stmt_send_long_data(stmt,0, pos, size)) 

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

/* Now, execute the query */ 
if (mysql_stmt_execute(stmt)) 

fprintf(stderr, "\n mysql_stmt_execute failed"); 
fprintf(stderr, "\n %s", mysql_stmt_error(stmt)); 
exit(0); 


int main() 

test(); 
//sleep(1); 
return 0; 


運行結(jié)果: 
Mysql LONGBLOB 類型存儲二進制數(shù)據(jù) (修改+調(diào)試+整理)

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美video丝袜连裤袜bd | 午夜在线观看免费观看 视频 | 亚洲AV综合99一二三四区 | 97影院秋霞国产精品 | 日本生活中的玛丽 | 亚洲国产精品网站久久 | 国内久久久| 视频一区在线免费观看 | 久久成人伊人欧洲精品AV | 四川女人偷人真实视频 | 黄a 大片a v 永久免费 | 欧美高清在线精品一区 | 国产成人精品午夜视频' | 日本黄a三级三级三级 | 饭冈加奈子黑人解禁在线播放 | 99久久爱热6在线播放 | 500福利第一导航 | 亚洲一卡2卡三卡4卡5卡组 | 99精品视频免费在线观看 | 黄 色 大 片 网站 | 4tube高清性欧美 | 二次元美女扒开内裤露尿口 | 免费黄色网站视频 | 国产拍拍视频一二三四区 | 日本高清视频网址 | 短篇艳妇系列 | a∨79成人网 | 美女免费观看一区二区三区 | 婷婷天天 | 日韩欧美色图 | 奇米9999| 69堂最新地域网名 | 99re在线视频观看 | 亚洲va在线va天堂成人 | 国产在线成人精品 | 国产 日韩 欧美 综合 | 日韩精选视频 | 精品久久久麻豆国产精品 | 日本aa大片在线播放免费看 | 舔大逼| 国产成人精品一区 |