国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

Mysql LONGBLOB 類型存儲二進制數據 (修改+調試+整理)

2024-07-24 12:43:58
字體:
來源:轉載
供稿:網友
在DBMS中線要創建數據庫test,table bintest,data字段數據類型用LONGBLOB即可測試
//測試文件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;
// 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/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);
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鲜城| 武冈市| 巫山县| 长葛市| 封丘县| 驻马店市| 监利县| 锦州市| 万安县| 洛南县| 商都县| 沂南县| 刚察县| 松潘县| 江津市| 盐亭县| 枣庄市| 安平县| 玉树县| 乌拉特中旗| 临桂县| 凤阳县| 德州市| 涪陵区| 焉耆| 泰来县| 顺昌县| 建水县| 自治县| 宿州市| 汕头市| 沭阳县| 涞源县| 宁河县| 江华| 曲阳县| 泰州市| 长海县| 项城市| 林芝县| 鞍山市|