Mysql LONGBLOB 형식 저장 바 이 너 리 데이터(수정+디 버 깅+정리)
//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))//
{
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
");
exit(0);
}
if (mysql_stmt_prepare(stmt, INSERT_QUERY, strlen(INSERT_QUERY)))
{
fprintf(stderr, "
mysql_stmt_prepare(), INSERT failed");
fprintf(stderr, "
%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, "
param bind failed");
fprintf(stderr, "
%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, "
send_long_data failed");
fprintf(stderr, "
%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, "
send_long_data failed");
fprintf(stderr, "
%s", mysql_stmt_error(stmt));
exit(0);
}
/* Now, execute the query */
if (mysql_stmt_execute(stmt))
{
fprintf(stderr, "
mysql_stmt_execute failed");
fprintf(stderr, "
%s", mysql_stmt_error(stmt));
exit(0);
}
}
int main()
{
test();
//sleep(1);
return 0;
}
실행 결과:
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바 my sql 에 이모 티 콘 저장my sql 에 emoji 표정 을 저장 하려 면 utf8mb 4 문자 집합 을 사용 해 야 합 니 다. 이것 은 4 바이트 저장 입 니 다. 최소 지원 버 전 은 5.5.3 + 입 니 다. 그렇지 않 으 면 새로운...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.