
2012-03-16 13:39158人阅读(0)收藏
网上的都是废话一大堆,好不容易修改的可以用了,测试了一下,写如几百MB的文件没问题,至于几百GB的,不得而知的,蛋疼的请自己尝试。
// mysqlwritefile.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "winsock.h"
#include "mysql.h"
#include "stdio.h"
#include "io.h"
#include "sys/stat.h"
#include <FCNTL.H>
#include "windows.h"
#include<stdlib.h>
#pragma comment(lib,"libmySQL.lib")
#define INSERT_QUERY "INSERT INTO tablename(id, file) VALUES(1, ?)"
MYSQL *conn;
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;
}
int main(int argc, char *argv[])
{
char *filename;
off_t size;
MYSQL_RES *res_set;
MYSQL_ROW row;
MYSQL_FIELD *field;
int i, flag;
char *sql;
FILE *fp;
char *buf;
int n=0;
char *end;
unsigned long *length;
filename = "c:\\a.7z";
if ((get_file_size(filename, &size)) == -1) {
perror("get file size" );

return 1;
}
if ((buf = (char *)malloc(sizeof(char)*(size+1))) == NULL) {
perror("malloc buf" );
return 1;
}
if ((fp = fopen(filename, "rb" )) == NULL) {
perror("fopen file" );
return 1;
}
if ((n = fread(buf, 1, size, fp)) < 0) { //n=*size
perror("fread file" );
return 1;
}
sql = (char *)malloc(sizeof(char)*n*2+256); //2n+1+strlen(other sql)
if (sql == NULL) {
perror("malloc sql" );
return 1;
}
conn = mysql_init(NULL);
if (conn == NULL) {
printf("init mysql, %sn", mysql_error(conn));
return 1;
}
if ((mysql_real_connect(conn, "192.168.1.101", "root", "", "test", 0, NULL, 0)) == NULL) {
printf("connect mysql, %sn", mysql_error(conn));
return 1;
}
strcpy(sql, "insert into tablename2(id, name, file) values(2, 'peter', " );
end = sql;
end += strlen(sql); //point sql tail
//convert NUL(ASCII 0)、'n'、'r'、''’、'''、'"'和Control-Z and so on
*end++ = '\'';
end += mysql_real_escape_string(conn, end, buf, n);
*end++ = '\'';
*end++ = ')';
flag = mysql_real_query(conn, sql, (unsigned int)(end-sql));
if (flag != 0) {
printf("insert failed, %sn", mysql_error(conn));
return 1;
}

if ((mysql_real_query(conn, "SELECT file FROM tablename where id=5", 31)) != 0) {
printf("insert failed, %sn", mysql_error(conn));
return 1;
}
res_set = mysql_store_result(conn);
fclose(fp);
fp = NULL;
fp = fopen("foo.bk", "wb" );
while ((row = mysql_fetch_row(res_set)) != NULL)
{
length = mysql_fetch_lengths(res_set);
for (i=0; i<mysql_num_fields(res_set);i++)
{
fwrite(row[0],1,length[0],fp);
}
}
fclose(fp);
mysql_close(conn);
free(sql);
sql = NULL;
return 0;
}
other example,but exists some mistakes.
Hi all !
I would like to insert a Blob ( a picture exactly ), here is a smallsource code to test my insert.You can find 2 sources code, the first is to insert a blob in a table ofa database ( this database exists in my MySQL server )
And the second code is to read this blob from the database.
I think there are a lot of mistakes, could you help me ? because i didnot find the solution.
thanks
#include <mysql/mysql.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/stat.h>#include <fcntl.h>#include <glib.h>
int main(int argc, char **argv){MYSQL *mysql = NULL;struct stat statbuf;int file;unsigned long size, ssize;gchar *data = NULL, *query = NULL, *end = NULL;int i;mysql = (MYSQL *) g_malloc0(sizeof(MYSQL));mysql_init(mysql);mysql = mysql_real_connect(mysql, "localhost","test", "XXXX","test_stef", 0, NULL, 0);file = open("photo.png", O_RDONLY);//file = open("test_mozilla.c", O_RDONLY);if ( !file )g_error("Impossible d'ouvrir le fichier photo.png");fstat(file, &statbuf);fprintf(stdout, "Size of photo.png : %ld\n", statbuf.st_size);size = statbuf.st_size;
data = (gchar *) g_malloc0(size);if ( !data )g_error("Impossible de charger la mémoire requise");ssize = read(file, data, size);if ( ssize == size )g_message("Fichier correctement lu"); query = (gchar *) g_malloc0(2*size);strcpy(query, "INSERT INTO test VALUES(");end = query + strlen(query);*end++ = '\'';gchar * debut, *fin;debut = end;end += mysql_real_escape_string(mysql, end, data, size);fin = end;fprintf(stdout, "Difference : %lu\n",(unsigned long) (fin - debut));*end++ = '\'';*end++ = ')';
/*for ( i = 0 ; i < 30 ; i++ )fprintf(stdout, "%c", query[i]);*/if ( mysql_real_query(mysql, query, (unsigned long) ( end - query )) ){g_error("Erreur lors de la requete d'insertion dans la DB");}g_free(data);g_free(query);mysql_close(mysql);return 0;}
But when i edit the mysql database with mysqlcc utility, i don't thecomplete file.
now, when i read the Blob from the database, i use this source code.
#include <glib.h>#include <mysql/mysql.h>#include <stdio.h>#include <string.h>#include <stdlib.h>
int main(int argc, char **argv){MYSQL *mysql = NULL;MYSQL_RES *mysql_res = NULL;MYSQL_FIELD *mysql_field = NULL;MYSQL_ROW *mysql_row = NULL;gchar query[] = "SELECT photo FROM test";mysql = (MYSQL *) g_malloc(sizeof(MYSQL));mysql_init(mysql);
if( !mysql_real_connect(mysql, "localhost", "test","XXXX","test_stef",0, NULL, 0) )g_error("Failed to connect to database : Error : %s\n",mysql_error(mysql));if (mysql_real_query(mysql, query, strlen(query)))g_error("Failed query to database : Error : %s\n",mysql_error(mysql));
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-129451-1.html
我不抢
果然还是经济学教兽