⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bb.c

📁 mysql数据库操作BLOB型数据的范例
💻 C
字号:
/*C4*/
/****************************************************************/
/*	Author:	Jethro Wright, III	TS :  3/ 4/1998  9:15	*/
/*	Date:	02/18/1998					*/
/*	mytest.c :  do some testing of the libmySQL.DLL....	*/
/*								*/
/*	History:						*/
/*		02/18/1998  jw3  also sprach zarathustra....	*/
/****************************************************************/


#include        <windows.h>
#include	<string.h>
#include    "database.h"
#include    "io.h"
#include    "fcntl.h"

/********************************************************
**
**		main  :-
**
********************************************************/

int
main( int argc, char * argv[] )
{

int i;
long fcount,l;
long lret;
char * sreturn=(char *)malloc(sizeof(char) * (100));
char svalue[100];
char smsg[100],id[3];
char filename[20];
int handle1;
int handle3;
//int handle2;
char * buf=(char *)malloc(sizeof(char)*1000);

char * sfile,* query;
//char * pp=(char *)malloc(sizeof(char) * 2);
char * ptr;


  if ( argc > 1 ) {
    strcpy( id, argv[ 1 ] ) ;
  }
  else {
	strcpy( id, "9" ) ;
  }


 handle1=open("d:\\mysql\\read.jpg", O_RDONLY | O_BINARY );



	if(handle1== -1)             //判断文件是否打开成功
             printf("File open error");//提示打开不成功
//得到文件大小
//	GetFileSize(handle1,lpdword);
//DWORD
//申请内存
sfile=(char *)malloc(150000); //文件大小再大一点
ptr=sfile;
fcount=0;

lret=read(handle1,buf,1000);
 while (lret>0)
 {
	for(i=0;i<lret;i++)
	{
		*ptr=buf[i];
		ptr++;
	}
	fcount+=lret;
	lret=read(handle1,buf,1000);
}
close(handle1);            //关闭打开的文件
ptr=NULL;

printf("%d\n",fcount);
/*
  handle2=open("d:\\mysql\\write.jpg", O_CREAT | O_WRONLY | O_BINARY );
  write(handle2,sfile,fcount);
  close(handle2);
  free(sfile);
  sfile=NULL;
  free(buf);
  buf=NULL;
  */
      
 

 if (OpenDatabase ("projectinnodb", NULL, NULL, NULL,sreturn) < 0)
 {
	printf("%s",sreturn);
  free(sreturn);
  sreturn=NULL;
	return (0);
	}



if (OpenRecordset ("set autocommit=0;",sreturn) < 0)
 {
	printf("%s",sreturn);
  free(sreturn);
  sreturn=NULL;
    
	return (0);
	}

 ptr = (char *) malloc (fcount * 2 + 1);
  l = escape_string(ptr, sfile, fcount); //调用该函数一定要在连接数据库后
  *(ptr + l + 1) = NULL;

  free(sfile);
  sfile=NULL;
  free(buf);
  buf=NULL;

//ptr就是MYSQL认识的字符串
  query = (char *) malloc (fcount * 2 + 100);

  strcpy(query,"insert into BLOBTEST SET ID=");
  strcat(query,id);
  strcat(query,",BLOBTT='");
  strcat(query,ptr);
  strcat(query,"'");


printf("\n");	
printf("insert blob\n");

 if (OpenRecordset (query,sreturn) < 0)
 {
	printf("%s",sreturn);
  free(sreturn);
  sreturn=NULL;
    free(query);
  query=NULL;
  free(ptr);
  ptr=NULL;
	return (0);
	}


printf("select blob\n");

 if (OpenRecordset ("select * from BLOBTEST",sreturn) < 0)
 {
	printf("%s",sreturn);
  free(sreturn);
  sreturn=NULL;
    free(query);
  query=NULL;
  free(ptr);
  ptr=NULL;
	return (0);
	}

printf ("%s\n", "id");

while (RecordsetEOF())
{
	if (GetField ("ID",svalue,smsg) > 0)
	{
		printf ("%s%s", svalue,"      ");
		sprintf(filename,"d:\\mysql\\%s.jpg", svalue);
		printf("%s\n",filename);
	}
	else
	{
	    printf ("%s", smsg);
  		free(sreturn);
  		sreturn=NULL;
		free(query);
		query=NULL;
		free(ptr);
		ptr=NULL;
		return 0;
	}

	if (GetFieldB ("BLOBTT",ptr,smsg,fcount) > 0)
	
	{
		handle3=open(filename, O_CREAT | O_WRONLY | O_BINARY );
		write(handle3,ptr,fcount);
		//printf("%d\n",fcount);
		close(handle3);
	}
	else
	{
		printf ("%s", smsg);
  		free(sreturn);
  		sreturn=NULL;
		free(query);
		query=NULL;
		free(ptr);
		ptr=NULL;
		return 0;
	}

printf ("\n");
MoveNext();
}



if (OpenRecordset ("commit;",sreturn) < 0)
 {
	printf("%s",sreturn);
  free(sreturn);
  sreturn=NULL;
    free(query);
  query=NULL;
  free(ptr);
  ptr=NULL;
	return (0);
	}
printf("\n");	
printf("rollback\n");



CloseRecordset ();
CloseDatabase ();
  free(sreturn);
  sreturn=NULL;
      free(query);
  query=NULL;
  free(ptr);
  ptr=NULL;
	return 0;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -