Mysql LONGBLOB 類(lèi)型存儲(chǔ)二進(jìn)制數(shù)據(jù) (修改+調(diào)試+整理)
更新時(shí)間:2009年07月09日 14:24:50 作者:
代碼來(lái)自網(wǎng)絡(luò),我學(xué)習(xí)整理了一下,測(cè)試通過(guò),下面的參數(shù)需要設(shè)置為你自己的
在DBMS中線要?jiǎng)?chuàng)建數(shù)據(jù)庫(kù)test,table bintest,data字段數(shù)據(jù)類(lèi)型用LONGBLOB即可測(cè)試
//測(cè)試文件c:\\test.iso,你可以找任何一個(gè)文件修改為即可,我找的是一個(gè)exe程序,修改為test.iso而已
//最大測(cè)試過(guò)加入文件大小為650M(一個(gè)正真的iso文件)
//注意:還要修改my.ini文件中的max_allowed_packet字段,我設(shè)置的是
//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;
}
運(yùn)行結(jié)果:
//測(cè)試文件c:\\test.iso,你可以找任何一個(gè)文件修改為即可,我找的是一個(gè)exe程序,修改為test.iso而已
//最大測(cè)試過(guò)加入文件大小為650M(一個(gè)正真的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;
}
運(yùn)行結(jié)果:

您可能感興趣的文章:
- 深入解析MySQL中的longtext與longblob及應(yīng)用場(chǎng)景
- mysql如何顯示longblob解決方案
- Mysql的longblob字段插入數(shù)據(jù)問(wèn)題解決
- php中將圖片gif,jpg或mysql longblob或blob字段值轉(zhuǎn)換成16進(jìn)制字符串
- Mysql 查詢數(shù)據(jù)庫(kù)容量大小的方法步驟
- 解決MySQl查詢不區(qū)分大小寫(xiě)的方法講解
- 詳解MySQL查詢時(shí)區(qū)分字符串中字母大小寫(xiě)的方法
- MySql查詢不區(qū)分大小寫(xiě)解決方案(兩種)
- MySQL中查詢所有數(shù)據(jù)庫(kù)占用磁盤(pán)空間大小和單個(gè)庫(kù)中所有表的大小的sql語(yǔ)句
- MYSQL中查詢LONGBLOB類(lèi)型數(shù)據(jù)的大小的詳細(xì)示例
相關(guān)文章
MySQL Semisynchronous Replication介紹
這篇文章主要介紹了MySQL Semisynchronous Replication介紹,本文講解了Semisynchronous Replication 定義、,需要的朋友可以參考下2015-05-05MySQL中一條查詢SQL語(yǔ)句的完整執(zhí)行流程
通常我們?cè)谑褂肕ySQL時(shí),我們看到的只是輸入一條語(yǔ)句,返回一個(gè)結(jié)果,卻不知道這條語(yǔ)句在MySQL內(nèi)部的執(zhí)行過(guò)程,這篇文章主要給大家介紹了關(guān)于MySQL中一條查詢SQL語(yǔ)句的完整執(zhí)行流程,需要的朋友可以參考下2024-05-05MySQL長(zhǎng)字符截?cái)嗟膶?shí)現(xiàn)示例
本文主要介紹了MySQL長(zhǎng)字符截?cái)嗟膶?shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03mysql最大連接數(shù)設(shè)置技巧總結(jié)
在本篇文章里小編給大家分享了關(guān)于mysql最大連接數(shù)設(shè)置的相關(guān)知識(shí)點(diǎn)和技巧,需要的朋友們學(xué)習(xí)下。2019-03-03Mac OS下PHP環(huán)境搭建及PHP操作MySQL常用方法小結(jié)
MAMP從名字上也可以看出來(lái),是Mac OS的Apache+MySQL+PHP的集成環(huán)境包,本文就來(lái)簡(jiǎn)單說(shuō)一下Mac OS下PHP環(huán)境搭建及PHP操作MySQL的常用方法小結(jié).2016-05-05安裝配置mysql及Navicat prenium的詳細(xì)流程
這篇文章主要介紹了安裝配置mysql及Navicat Premium的詳細(xì)流程,配置方法也真的很簡(jiǎn)單,本文給大家詳細(xì)介紹mysql Navicat Premium安裝配置相關(guān)知識(shí)感興趣的朋友,一起學(xué)習(xí)吧2021-06-06linux下mysql自動(dòng)備份數(shù)據(jù)庫(kù)與自動(dòng)刪除臨時(shí)文件
mysql自動(dòng)備份數(shù)據(jù)庫(kù)與自動(dòng)刪除臨時(shí)文件,有需要的朋友可以參考下2013-02-02