欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2009年07月09日 14:24:50   作者:  
代碼來自網(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é)果:

相關(guān)文章

  • centos mysql 修改數(shù)據(jù)庫目錄

    centos mysql 修改數(shù)據(jù)庫目錄

    centos mysql修改數(shù)據(jù)庫目錄的方法。
    2013-11-11
  • 解決MySQL無法遠(yuǎn)程連接的方法

    解決MySQL無法遠(yuǎn)程連接的方法

    這篇文章主要介紹了解決MySQL無法遠(yuǎn)程連接的方法,文中給出的解決方案主要針對IP限制時出現(xiàn)的該種情況,需要的朋友可以參考下
    2015-04-04
  • MySQL表的碎片整理和空間回收的方法

    MySQL表的碎片整理和空間回收的方法

    本文主要介紹了MySQL表的碎片整理和空間回收的方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • MySQL數(shù)據(jù)入庫時特殊字符處理詳解

    MySQL數(shù)據(jù)入庫時特殊字符處理詳解

    本文是對MySQL數(shù)據(jù)入庫時特殊字符的處理進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2013-11-11
  • Mysql中自定義函數(shù)的創(chuàng)建和執(zhí)行方式

    Mysql中自定義函數(shù)的創(chuàng)建和執(zhí)行方式

    這篇文章主要介紹了Mysql中自定義函數(shù)的創(chuàng)建和執(zhí)行方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 詳細(xì)分析MySQL主從復(fù)制

    詳細(xì)分析MySQL主從復(fù)制

    這篇文章主要介紹了MySQL主從復(fù)制的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)MySQL,感興趣的朋友可以了解下
    2020-08-08
  • Mysql數(shù)據(jù)庫之sql基本語句小結(jié)

    Mysql數(shù)據(jù)庫之sql基本語句小結(jié)

    這篇文章主要介紹了Mysql數(shù)據(jù)庫之sql基本語句,結(jié)合實例形式總結(jié)分析了MySQL數(shù)據(jù)庫連接、登錄、查看以及數(shù)據(jù)庫、數(shù)據(jù)表等常見操作技巧,需要的朋友可以參考下
    2019-11-11
  • mysql外鍵基本功能與用法詳解

    mysql外鍵基本功能與用法詳解

    這篇文章主要介紹了mysql外鍵基本功能與用法,結(jié)合實例形式詳細(xì)分析了mysql外鍵的基本概念、功能、用法及操作注意事項,需要的朋友可以參考下
    2020-04-04
  • MySQL9.0更新了哪些特性

    MySQL9.0更新了哪些特性

    MySQL 9.0作為MySQL數(shù)據(jù)庫管理系統(tǒng)的一個重要更新版本,帶來了多項新特性和改進(jìn),下面我們就一起來看看更新了哪些特性,感興趣的可以了解一下
    2024-08-08
  • MySQL如何通過Navicat實現(xiàn)遠(yuǎn)程連接

    MySQL如何通過Navicat實現(xiàn)遠(yuǎn)程連接

    這篇文章主要介紹了MySQL如何通過Navicat實現(xiàn)遠(yuǎn)程連接,幫助大家更好的理解和使用MySQL數(shù)據(jù)庫,感興趣的朋友可以了解下
    2020-09-09

最新評論