经过二天的奋斗,终于用VC把图片插入到了MYsql中,这可费了我很大的劲呀,Mysql上的那个C++的例子,转义符不能转,用C的例子,文件都不出来,最后我把它两混在一起,这个问题就解决了,为了大家以后使用这个的时候,不再苦苦去寻找资料,我把这个函数贴出来,大家参考.
可以把你自己的文件路径,和ID号改了,这是为了实验才这样做的呀
MYSQL *conn;
char query[1024*200], *p;
if ( (conn = mysql_init((MYSQL*) 0)) &&
mysql_real_connect( conn, "localhost", "root",
"taotaomysql", "test", 3306,
NULL, 0 ) )
{
sprintf (query, "INSERT INTO photo VALUES (%d,'", 31);
p = query + strlen (query); ifstream In ("E:\\TN_cd_photo10.jpg",ios::in | ios::binary);
struct stat for_len;
if ((In.rdbuf())->is_open())
{
if (stat ("c:\\171033.jpg",&for_len) == -1) return ;
unsigned int blen = for_len.st_size;
if (!blen) return; //检查文件大小
char *read_buffer = new char[blen]; //声明空间,存储图片
In.read(read_buffer,blen);
string fill(read_buffer,blen);
p += mysql_escape_string (p, read_buffer, blen); //转义符
//写入
(void) strcpy (p, "')");
mysql_query (conn, query);
delete[] read_buffer;
MessageBox("插入完毕");
}
else
MessageBox("Your binary file could not be open",NULL,MB_OK);}
可以把你自己的文件路径,和ID号改了,这是为了实验才这样做的呀
MYSQL *conn;
char query[1024*200], *p;
if ( (conn = mysql_init((MYSQL*) 0)) &&
mysql_real_connect( conn, "localhost", "root",
"taotaomysql", "test", 3306,
NULL, 0 ) )
{
sprintf (query, "INSERT INTO photo VALUES (%d,'", 31);
p = query + strlen (query); ifstream In ("E:\\TN_cd_photo10.jpg",ios::in | ios::binary);
struct stat for_len;
if ((In.rdbuf())->is_open())
{
if (stat ("c:\\171033.jpg",&for_len) == -1) return ;
unsigned int blen = for_len.st_size;
if (!blen) return; //检查文件大小
char *read_buffer = new char[blen]; //声明空间,存储图片
In.read(read_buffer,blen);
string fill(read_buffer,blen);
p += mysql_escape_string (p, read_buffer, blen); //转义符
//写入
(void) strcpy (p, "')");
mysql_query (conn, query);
delete[] read_buffer;
MessageBox("插入完毕");
}
else
MessageBox("Your binary file could not be open",NULL,MB_OK);}
ifstream In ("E:\\TN_cd_photo10.jpg",ios::in | ios::binary);
In.rdbuf())->is_open();
为什么打不开呢,open返回的为0
char *read_buffer = new char[2G!];
岂不就糟了...