快捷搜索:  as  2018  FtCWSyGV  С˵  test  xxx  Ψһ  w3viyKQx

澳门新葡萄app下载:C 批量保存图片进 mysql 利用MYSQLBIND插入longblob



mysql 批量保存图片 必要用到往 longblob 里面插入图片。。。。然则直接用 insert into 会呈现无意偶尔能传有的不能插进去。。于是换招。。。。

#include

#include

#include

#include

#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);

intnum_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_BINDbind[2];

my_ulonglongaffected_rows;

intparam_count;

shortsmall_data;

intint_data;

charstr_data[STRING_SIZE];

unsigned long str_length;

my_boolis_null;

/* Prepare an INSERT query with 3 parameters */

/* (the TIMESTAMP column is not named; the server澳门新葡萄app下载 */

/*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_st澳门新葡萄app下载mt_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_澳门新葡萄app下载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澳门新葡萄app下载_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响应的字符串内容,而不能从新指向一个新的内存,否则虽然可以插入到数据库之中,然则没稀澳门新葡萄app下载有据的,等于空行。

2 对付my_bool类型,假如为1,等于代表这个参数在插入数据库的时刻为null值,假如为0,则会插入这个数据,这样做法,可以用于节制,那么数据必要插入到数据库之中,那些不必要。

3 以上的操作是mysql的c说话的操作措施,mysql也供给了响应的mysql++的类库,用于c++对付数据库的操作,名字空间为mysqlpp。

4 别的供给一个mysql的c说话操作的一个向导例子,http://zetcode.com/tutorials/mysqlcapitutorial/,讲的对照全,然则似乎没有preparestatement的例子.

您可能还会对下面的文章感兴趣: