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中線(xiàn)要?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é)果:
相關(guān)文章
centos mysql 修改數(shù)據(jù)庫(kù)目錄
centos mysql修改數(shù)據(jù)庫(kù)目錄的方法。2013-11-11
MySQL數(shù)據(jù)入庫(kù)時(shí)特殊字符處理詳解
本文是對(duì)MySQL數(shù)據(jù)入庫(kù)時(shí)特殊字符的處理進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-11-11
Mysql中自定義函數(shù)的創(chuàng)建和執(zhí)行方式
這篇文章主要介紹了Mysql中自定義函數(shù)的創(chuàng)建和執(zhí)行方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Mysql數(shù)據(jù)庫(kù)之sql基本語(yǔ)句小結(jié)
這篇文章主要介紹了Mysql數(shù)據(jù)庫(kù)之sql基本語(yǔ)句,結(jié)合實(shí)例形式總結(jié)分析了MySQL數(shù)據(jù)庫(kù)連接、登錄、查看以及數(shù)據(jù)庫(kù)、數(shù)據(jù)表等常見(jiàn)操作技巧,需要的朋友可以參考下2019-11-11
MySQL如何通過(guò)Navicat實(shí)現(xiàn)遠(yuǎn)程連接
這篇文章主要介紹了MySQL如何通過(guò)Navicat實(shí)現(xiàn)遠(yuǎn)程連接,幫助大家更好的理解和使用MySQL數(shù)據(jù)庫(kù),感興趣的朋友可以了解下2020-09-09

