c++ mysql statement insert success but nothing is added

381
March 20, 2017, at 11:26 AM

everyone. i tried to insert some data insert mysql using mysql-c connector.

void Mysql_Statement(MYSQL * mysql)
{
    /// 测试失败
    int ret = 0;
    char errMsg[200] = {0};
    const char* statement = "insert into table_name(test) values(?);";
    MYSQL_STMT *stmt = mysql_stmt_init(mysql);
    if(!stmt)
    {
        fprintf(stderr,"mysql_stmt_init,2 failed\n");
        fprintf(stderr,"%s\n",mysql_stmt_error(stmt));
        exit(0);
    }
    if(mysql_stmt_prepare(stmt,statement,strlen(statement)))
    {
        fprintf(stderr,"mysql_stmt_prepare,2 failed\n");
        fprintf(stderr,"%s\n",mysql_stmt_error(stmt));
        exit(0);
    }
    int i = 0;
    int max_num = 3;
    const int FIELD_NUM = 1;
    while(i < max_num)
    {
        MYSQL_BIND bind[FIELD_NUM];
        memset(bind,0,FIELD_NUM * sizeof(MYSQL_BIND));
        int age = 111;
        bind[0].buffer_type = MYSQL_TYPE_LONG;
        bind[0].buffer = malloc(sizeof(int));
        *(int*)bind[0].buffer = 111;
        bind[0].length =  0;
        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);
        }
        cout << "before execute\n";
        if(mysql_stmt_execute(stmt))
        {
            fprintf(stderr,"mysql stmt execute failed\n");
            fprintf(stderr,mysql_stmt_error(stmt));
            exit(0);
        }
        cout << "after execute\n";
        i++;
    }
    mysql_commit(mysql);
//  mysql_stmt_send_long_data(stmt);
    mysql_stmt_close(stmt);
}

but in mysql , the result was like this.

mysql> select * from table_name;
+----+------+
| id | test |
+----+------+
|  1 |    0 |
|  2 |    0 |
|  3 |    0 |
|  4 |    0 |
|  5 |    0 |
|  6 |    0 |
|  7 |    0 |
|  8 |    0 |
|  9 |    0 |
| 10 |    0 |
| 11 |    0 |
| 12 |    0 |
+----+------+
12 rows in set (0.00 sec)
Rent Charter Buses Company
READ ALSO
Unable to Query Table with SQL Fiddle

Unable to Query Table with SQL Fiddle

I'm using SQL Fiddle, and I'm unable to run a simple query (SELECT * FROM CUSTOMER;) after building my schema with the code belowThe only error I get is "Oops! Something went wrong

496
How to create a trigger to decrease quantity?

How to create a trigger to decrease quantity?

I need to Create a trigger on the orderItem table to update the field, ‘quantityInStock’ from the item table after an insert occursIE: The ‘quantityInStock’ will decrement by the ordered quantity

386
SQL Inner Join request doesn&#39;t work

SQL Inner Join request doesn't work

I'm new in mysql and php and I have a problem with a queryI want to get nearby locations which I am calculating with the Harversine formula

249
How to separate check in / check out by time in MySQL into new table? [on hold]

How to separate check in / check out by time in MySQL into new table? [on hold]

How to fill this "NEWTABLE" based on my "OLDTABLE" record?

299