mysql 批量保存图片 需要用到往 longblob 里面插入图片。。。。但是直接用 insert into 会出现有时能传有的不能插进去。。于是换招。。。。
#include <my_global.h> #include <mysql.h> #include <stdio.h> #include <stdlib.h> #define STRING_SIZE 50 #define INSERT_SAMPLE "INSERT INTO testtable2 VALUES(?,?)" int main(int argc, char **argv) { MYSQL *conn; conn = mysql_init(NULL); if (conn == NULL) { printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn)); exit(1); } if (mysql_real_connect(conn, "localhost", "user", "pass", "database", 0, NULL, 0) == NULL) { printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn)); exit(1); } /*if (mysql_query(conn, "create table testtable2(name varchar(30),id int)")) { printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn)); exit(1); } MYSQL_RES* result = mysql_store_result(conn); int num_fields = mysql_num_fields(result); MYSQL_ROW *row; int i; while ((row = mysql_fetch_row(result))) { for(i = 0; i < num_fields; i++) { printf("%s ", row[i] ? row[i] : "NULL"); } printf("\n"); } mysql_free_result(result);*/ MYSQL_STMT *stmt; MYSQL_BIND bind[2]; my_ulonglong affected_rows; int param_count; short small_data; int int_data; char str_data[STRING_SIZE]; unsigned long str_length; my_bool is_null; /* Prepare an INSERT query with 3 parameters */ /* (the TIMESTAMP column is not named; the server */ /* sets it to the current date and time) */ stmt = mysql_stmt_init(conn); if (!stmt) { fprintf(stderr, " mysql_stmt_init(), out of memory\n"); exit(0); } if (mysql_stmt_prepare(stmt, INSERT_SAMPLE, strlen(INSERT_SAMPLE))) { fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } fprintf(stdout, " prepare, INSERT successful\n"); /* Get the parameter count from the statement */ param_count= mysql_stmt_param_count(stmt); fprintf(stdout, " total parameters in INSERT: %d\n", param_count); if (param_count != 2) /* validate parameter count */ { fprintf(stderr, " invalid parameter count returned by MySQL\n"); exit(0); } /* Bind the data for all 3 parameters */ memset(bind, 0, sizeof(bind)); /* INTEGER PARAM */ /* This is a number type, so there is no need to specify buffer_length */ /* STRING PARAM */ bind[0].buffer_type= MYSQL_TYPE_STRING; bind[0].buffer= (char *)str_data; bind[0].buffer_length= STRING_SIZE; bind[0].is_null= 0; bind[0].length= &str_length; /* SMALLINT PARAM */ bind[1].buffer_type= MYSQL_TYPE_LONG; bind[1].buffer= (char *)&small_data; bind[1].is_null= &is_null; bind[1].length= 0; /* Bind the buffers */ if (mysql_stmt_bind_param(stmt, bind)) { fprintf(stderr, " mysql_stmt_bind_param() failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } /* Specify the data values for the first row */ int_data= 10; /* integer */ strncpy(str_data, "MySQL", STRING_SIZE); /* string */ str_length= strlen(str_data); /* INSERT SMALLINT data as NULL */ is_null= 1; /* Execute the INSERT statement - 1*/ if (mysql_stmt_execute(stmt)) { fprintf(stderr, " mysql_stmt_execute(), 1 failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } /* Get the total number of affected rows */ affected_rows= mysql_stmt_affected_rows(stmt); fprintf(stdout, " total affected rows(insert 1): %lu\n", (unsigned long) affected_rows); if (affected_rows != 1) /* validate affected rows */ { fprintf(stderr, " invalid affected rows by MySQL\n"); exit(0); } /* Specify data values for second row, then re-execute the statement */ int_data= 1000; strncpy(str_data, "The most popular Open Source database",STRING_SIZE); str_length= strlen(str_data); small_data= 1000; /* smallint */ is_null= 0; /* reset */ /* Execute the INSERT statement - 2*/ if (mysql_stmt_execute(stmt)) { fprintf(stderr, " mysql_stmt_execute, 2 failed\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } /* Get the total rows affected */ affected_rows= mysql_stmt_affected_rows(stmt); fprintf(stdout, " total affected rows(insert 2): %lu\n", (unsigned long) affected_rows); if (affected_rows != 1) /* validate affected rows */ { fprintf(stderr, " invalid affected rows by MySQL\n"); exit(0); } /* Close the statement */ if (mysql_stmt_close(stmt)) { fprintf(stderr, " failed while closing the statement\n"); fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); exit(0); } mysql_close(conn); }
需要注意的有几点:
1 对于上面的绑定过程,首先指定数据类型,数据的指针,以及长度,其中,数据的指针所指向的内存是不能改变的,也就是说在绑定时,指定了块内存区域之后,不同的行的数据,需要为这个区域进行strcpy相应的字符串内容,而不能重新指向一个新的内存,否则虽然可以插入到数据库之中,但是没有数据的,即是空行。
2 对于my_bool类型,如果为1,即是代表这个参数在插入数据库的时候为null值,如果为0,则会插入这个数据,这样做法,可以用于控制,那么数据需要插入到数据库之中,那些不需要。
3 以上的操作是mysql的c语言的操作方法,mysql也提供了相应的mysql++的类库,用于c++对于数据库的操作,名字空间为mysqlpp。
4 另外提供一个mysql的c语言操作的一个引导例子,http://zetcode.com/tutorials/mysqlcapitutorial/,讲的比较全,但是好像没有preparestatement的例子.
大部分转自: http://blog.csdn.net/xiqi8144/archive/2009/08/07/4423527.aspx