视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
C语言实现myql中存取二进制文件_MySQL
2020-11-09 17:03:32 责编:小采
文档


C和C加加

最近搞mysql,这两天想用C把二进制文件(

CODE: [Copy to clipboard] #include
#include
#include
#include
#include
#include

#define host "localhost" //mysql server
#define username "root"
#define password "cipher"
#define database "www"

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 *conn;
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;

if (argc != 2) {
printf("Usage: %s srcfile\n", argv[0]);
exit(1);
}

filename = argv[1];
if ((get_file_size(filename, size)) == -1) {
perror("get file size" );
exit(1);
}

if ((buf = (char *)malloc(sizeof(char)*(*size+1))) == NULL) {
perror("malloc buf" );
exit(1);
}

if ((fp = fopen(filename, "rb" )) == NULL) {
perror("fopen file" );
exit(1);
}

if ((n = fread(buf, 1, *size, fp)) < 0) { //n=*size
perror("fread file" );
exit(1);
}

sql = (char *)malloc(sizeof(char)*n*2+256); //2n+1+strlen(other sql)
if (sql == NULL) {
perror("malloc sql" );
exit(1);
}

conn = mysql_init(NULL);
if (conn == NULL) {
printf("init mysql, %s\n", mysql_error(conn));
exit(1);
}

if ((mysql_real_connect(conn, host, username, password, database, 0, NULL, 0)) == NULL) {
printf("connect mysql, %s\n", mysql_error(conn));
exit(1);
}

strcpy(sql, "insert into www(id, name, file) values(5, '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, %s\n", mysql_error(conn));
exit(1);
}

if ((mysql_real_query(conn, "SELECT file FROM www where id=5", 31)) != 0) {
printf("insert failed, %s\n", mysql_error(conn));
exit(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 fwrite(row[0], 1, length[0], fp);
}
}

fclose(fp);
mysql_close(conn);
free(sql);
sql = NULL;

return 0;
}
//gcc -o mysql_binary mysql_binary.c -lmysqlclient
//usage: ./mysql_binary filenameMakefile:

CODE: [Copy to clipboard] CXX = gcc
LIBS = -lmysqlclient
PRODUCT = mysql_binary
.LIBPATTERNS: lib%.so lib%.a
vpath % .
vpath %.c src
vpath %.h include

OBJS = mysql_binary.o
$(PRODUCT): $(OBJS)
$(CXX) $(OBJS) -o $@ $(LIBS)
.c.o:

.PHONY:clean
clean:
-rm -f $(OBJS) $(PRODUCT)好了,擦汗!
晕 这么多表情符号,弄一下。
生成的foo.bk文件为源文件的拷贝。

下载本文
显示全文
专题