|
| 1 | +#include <stdio.h> |
| 2 | +#include <mysql.h> |
| 3 | +#include <unistd.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <string.h> |
| 6 | + |
| 7 | +#include "c.h" |
| 8 | +#define STRING_SIZE 1024 |
| 9 | + |
| 10 | +/* |
| 11 | +
|
| 12 | +create table test1(byteCol tinyint, blobCol blob, dateCol date, decimalCol decimal, doubleCol double, floatCol float, intCol int, longCol bigint, nullCol int, shortcol smallint, medCol mediumint, stringCol varchar(300), timeCol time, timestampCol timestamp); |
| 13 | +
|
| 14 | +MYSQL_TYPE_TIMESTAMP TIMESTAMP field |
| 15 | +MYSQL_TYPE_DATE DATE field |
| 16 | +MYSQL_TYPE_TIME TIME field |
| 17 | +MYSQL_TYPE_DATETIME DATETIME field |
| 18 | +
|
| 19 | +*/ |
| 20 | + |
| 21 | +#define DROP_SAMPLE_TABLE "DROP TABLE IF EXISTS test_table" |
| 22 | +#define CREATE_SAMPLE_TABLE "CREATE TABLE test_table(col1 INT, col2 VARCHAR(40), col21 varchar(40), col22 varchar(40), col3 SMALLINT, col4 TIMESTAMP, col5 datetime, col6 date, col7 time)" |
| 23 | +#define INSERT_SAMPLE "INSERT INTO test_table(col1,col2,col3, col4, col5, col6, col7, col21, col22) VALUES(?,?,?,?,?,?,?, ?, ?)" |
| 24 | +#define INSERT_SAMPLE2 "INSERT INTO test_table(col1,col2,col3) VALUES(?,?,?)" |
| 25 | + |
| 26 | +int main (int argc, char *argv[]) { |
| 27 | + |
| 28 | + MYSQL *mysql; |
| 29 | + MYSQL_RES *result; |
| 30 | + MYSQL_ROW row; |
| 31 | + my_bool reconnect = 0; |
| 32 | + mysql = mysql_init(NULL); |
| 33 | + mysql_options(mysql, MYSQL_OPT_RECONNECT, &reconnect); |
| 34 | + |
| 35 | + CONN(0); |
| 36 | + //mysql_real_connect(mysql, "127.0.0.1", "test", "test", "test", 3306, NULL, 0); |
| 37 | + //mysql_real_connect(mysql, "10.1.170.196", "root", "root", "test", 3306, NULL, 0); |
| 38 | + |
| 39 | + MYSQL_STMT *stmt; |
| 40 | + MYSQL_BIND bind[9]; |
| 41 | + my_ulonglong affected_rows; |
| 42 | + int param_count; |
| 43 | + short small_data; |
| 44 | + int int_data; |
| 45 | + char str_data[STRING_SIZE]; |
| 46 | + unsigned long str_length; |
| 47 | + my_bool is_null; |
| 48 | + |
| 49 | + if (mysql_query(mysql, DROP_SAMPLE_TABLE)) |
| 50 | + { |
| 51 | + fprintf(stderr, " DROP TABLE failed\n"); |
| 52 | + fprintf(stderr, " %s\n", mysql_error(mysql)); |
| 53 | + exit(0); |
| 54 | + } |
| 55 | + |
| 56 | + if (mysql_query(mysql, CREATE_SAMPLE_TABLE)) |
| 57 | + { |
| 58 | + fprintf(stderr, " CREATE TABLE failed\n"); |
| 59 | + fprintf(stderr, " %s\n", mysql_error(mysql)); |
| 60 | + exit(0); |
| 61 | + } |
| 62 | + |
| 63 | + /* Prepare an INSERT query with 3 parameters */ |
| 64 | + /* (the TIMESTAMP column is not named; the server */ |
| 65 | + /* sets it to the current date and time) */ |
| 66 | + stmt = mysql_stmt_init(mysql); |
| 67 | + if (!stmt) |
| 68 | + { |
| 69 | + fprintf(stderr, " mysql_stmt_init(), out of memory\n"); |
| 70 | + exit(0); |
| 71 | + } |
| 72 | + if (mysql_stmt_prepare(stmt, INSERT_SAMPLE, strlen(INSERT_SAMPLE))) |
| 73 | + { |
| 74 | + fprintf(stderr, " mysql_stmt_prepare(), INSERT failed\n"); |
| 75 | + fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); |
| 76 | + exit(0); |
| 77 | + } |
| 78 | + fprintf(stdout, " prepare, INSERT successful\n"); |
| 79 | + |
| 80 | + /* Get the parameter count from the statement */ |
| 81 | + param_count= mysql_stmt_param_count(stmt); |
| 82 | + fprintf(stdout, " total parameters in INSERT: %d\n", param_count); |
| 83 | + |
| 84 | + /* Bind the data for all 3 parameters */ |
| 85 | + |
| 86 | + memset(bind, 0, sizeof(bind)); |
| 87 | + |
| 88 | + /* INTEGER PARAM */ |
| 89 | + /* This is a number type, so there is no need to specify buffer_length */ |
| 90 | + bind[0].buffer_type= MYSQL_TYPE_LONGLONG; |
| 91 | + bind[0].buffer= (char *)&int_data; |
| 92 | + bind[0].is_null= 0; |
| 93 | + |
| 94 | + /* STRING PARAM */ |
| 95 | + bind[1].buffer_type= MYSQL_TYPE_STRING; |
| 96 | + bind[1].buffer= (char *)str_data; |
| 97 | + //bind[1].buffer_length= STRING_SIZE; //最大长度, string not use this, 其它类型buffer_length 也没用 |
| 98 | + bind[1].is_null= 0; |
| 99 | + bind[1].length= &str_length; //实际大小, bind_ |
| 100 | + |
| 101 | + /* SMALLINT PARAM */ |
| 102 | + bind[2].buffer_type= MYSQL_TYPE_SHORT; |
| 103 | + bind[2].buffer= (char *)&small_data; |
| 104 | + bind[2].is_null= &is_null; |
| 105 | + |
| 106 | + /* timestamp */ |
| 107 | + |
| 108 | + MYSQL_TIME t; |
| 109 | + bind[3].buffer_type= MYSQL_TYPE_TIMESTAMP; |
| 110 | + bind[3].buffer= (char*)&t; |
| 111 | + bind[3].is_null= 0; |
| 112 | + |
| 113 | + bind[4].buffer_type= MYSQL_TYPE_DATETIME; |
| 114 | + bind[4].buffer= (char*)&t; |
| 115 | + bind[4].is_null= 0; |
| 116 | + |
| 117 | + bind[5].buffer_type= MYSQL_TYPE_DATE; |
| 118 | + bind[5].buffer= (char*)&t; |
| 119 | + bind[5].is_null= 0; |
| 120 | + |
| 121 | + bind[6].buffer_type= MYSQL_TYPE_TIME; |
| 122 | + bind[6].buffer= (char*)&t; |
| 123 | + bind[6].is_null= 0; |
| 124 | + |
| 125 | + bind[7].buffer_type= MYSQL_TYPE_STRING; |
| 126 | + bind[7].buffer= (char *)str_data; |
| 127 | + //bind[1].buffer_length= STRING_SIZE; //最大长度, string not use this, 其它类型buffer_length 也没用 |
| 128 | + bind[7].is_null= 0; |
| 129 | + bind[7].length= &str_length; //实际大小, bind_ |
| 130 | + |
| 131 | + bind[8].buffer_type= MYSQL_TYPE_NULL; |
| 132 | + bind[8].buffer= (char *)str_data; |
| 133 | + //bind[1].buffer_length= STRING_SIZE; //最大长度, string not use this, 其它类型buffer_length 也没用 |
| 134 | + bind[8].is_null= 0; |
| 135 | + bind[8].length= &str_length; //实际大小, bind_ |
| 136 | + |
| 137 | + /* Bind the buffers */ |
| 138 | + if (mysql_stmt_bind_param(stmt, bind)) |
| 139 | + { |
| 140 | + fprintf(stderr, " mysql_stmt_bind_param() failed\n"); |
| 141 | + fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); |
| 142 | + exit(0); |
| 143 | + } |
| 144 | + |
| 145 | + /* Specify the data values for the first row -------------------------------------------------- */ |
| 146 | + int_data= 10; /* integer */ |
| 147 | + strncpy(str_data, "MySQL", STRING_SIZE); /* string */ |
| 148 | + str_length= strlen(str_data); |
| 149 | + |
| 150 | + t.year= 2032; |
| 151 | + t.month= 02; |
| 152 | + t.day= 03; |
| 153 | + |
| 154 | + t.hour= 10; |
| 155 | + t.minute= 45; |
| 156 | + t.second= 20; |
| 157 | + |
| 158 | + /* INSERT SMALLINT data as NULL */ |
| 159 | + is_null= 1; |
| 160 | + |
| 161 | + /* Execute the INSERT statement - 1*/ |
| 162 | + if (mysql_stmt_execute(stmt)) |
| 163 | + { |
| 164 | + fprintf(stderr, " mysql_stmt_execute(), 1 failed\n"); |
| 165 | + fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); |
| 166 | + exit(0); |
| 167 | + } |
| 168 | + |
| 169 | + int_data= 333; /* integer */ |
| 170 | + mysql_stmt_execute(stmt); |
| 171 | + |
| 172 | + /* Get the total number of affected rows */ |
| 173 | + affected_rows= mysql_stmt_affected_rows(stmt); |
| 174 | + fprintf(stdout, " total affected rows(insert 1): %lu\n", |
| 175 | + (unsigned long) affected_rows); |
| 176 | + |
| 177 | + if (affected_rows != 1) /* validate affected rows */ |
| 178 | + { |
| 179 | + fprintf(stderr, " invalid affected rows by MySQL\n"); |
| 180 | + exit(0); |
| 181 | + } |
| 182 | + |
| 183 | + /* Specify data values for second row, then re-execute the statement ----------------------------------------*/ |
| 184 | + int_data= 1000; |
| 185 | + strncpy(str_data, "The most popular Open Source database", STRING_SIZE); |
| 186 | + str_length= strlen(str_data); |
| 187 | + small_data= 1000; /* smallint */ |
| 188 | + is_null= 0; /* reset */ |
| 189 | + |
| 190 | + /* Execute the INSERT statement - 2*/ |
| 191 | + if (mysql_stmt_execute(stmt)) |
| 192 | + { |
| 193 | + fprintf(stderr, " mysql_stmt_execute, 2 failed\n"); |
| 194 | + fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); |
| 195 | + exit(0); |
| 196 | + } |
| 197 | + |
| 198 | + /* Get the total rows affected */ |
| 199 | + affected_rows= mysql_stmt_affected_rows(stmt); |
| 200 | + fprintf(stdout, " total affected rows(insert 2): %lu\n", |
| 201 | + (unsigned long) affected_rows); |
| 202 | + |
| 203 | + if (affected_rows != 1) /* validate affected rows */ |
| 204 | + { |
| 205 | + fprintf(stderr, " invalid affected rows by MySQL\n"); |
| 206 | + exit(0); |
| 207 | + } |
| 208 | + |
| 209 | + /* Specify data values for third row, then re-execute the statement */ |
| 210 | + int_data= 1000; |
| 211 | + strncpy(str_data, "The most popular Open Source database11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111", STRING_SIZE); |
| 212 | + str_length= strlen(str_data); |
| 213 | + small_data= 10000; /* smallint */ |
| 214 | + is_null= 0; /* reset */ |
| 215 | + |
| 216 | + /* Execute the INSERT statement - 2*/ |
| 217 | + if (mysql_stmt_execute(stmt)) |
| 218 | + { |
| 219 | + fprintf(stderr, " mysql_stmt_execute, 2 failed\n"); |
| 220 | + fprintf(stderr, " %s\n", mysql_stmt_error(stmt)); |
| 221 | + exit(0); |
| 222 | + } |
| 223 | + |
| 224 | + /* Get the total rows affected */ |
| 225 | + affected_rows= mysql_stmt_affected_rows(stmt); |
| 226 | + fprintf(stdout, " total affected rows(insert 2): %lu\n", |
| 227 | + (unsigned long) affected_rows); |
| 228 | + |
| 229 | + if (affected_rows != 1) /* validate affected rows */ |
| 230 | + { |
| 231 | + fprintf(stderr, " invalid affected rows by MySQL\n"); |
| 232 | + exit(0); |
| 233 | + } |
| 234 | + |
| 235 | + //----------------------------- |
| 236 | + |
| 237 | + MYSQL_STMT *stmt2; |
| 238 | + MYSQL_BIND bind2[3]; |
| 239 | + |
| 240 | + stmt2 = mysql_stmt_init(mysql); |
| 241 | + |
| 242 | + //here get stmt_id |
| 243 | + mysql_stmt_prepare(stmt2, INSERT_SAMPLE2, strlen(INSERT_SAMPLE2)); |
| 244 | + |
| 245 | + memset(bind2, 0, sizeof(bind2)); |
| 246 | + |
| 247 | + /* INTEGER PARAM */ |
| 248 | + /* This is a number type, so there is no need to specify buffer_length */ |
| 249 | + bind2[0].buffer_type= MYSQL_TYPE_LONG; |
| 250 | + bind2[0].buffer= (char *)&int_data; |
| 251 | + bind2[0].is_null= 0; |
| 252 | + |
| 253 | + /* STRING PARAM */ |
| 254 | + bind2[1].buffer_type= MYSQL_TYPE_STRING; |
| 255 | + bind2[1].buffer= (char *)str_data; |
| 256 | + //bind[1].buffer_length= STRING_SIZE; //最大长度, string not use this, 其它类型buffer_length 也没用 |
| 257 | + bind2[1].is_null= 0; |
| 258 | + bind2[1].length= &str_length; //实际大小, bind_ |
| 259 | + |
| 260 | + /* SMALLINT PARAM */ |
| 261 | + bind2[2].buffer_type= MYSQL_TYPE_SHORT; |
| 262 | + bind2[2].buffer= (char *)&small_data; |
| 263 | + bind2[2].is_null= &is_null; |
| 264 | + |
| 265 | + /* Bind the buffers */ |
| 266 | + mysql_stmt_bind_param(stmt2, bind2); |
| 267 | + |
| 268 | + /* Specify the data values for the first row -------------------------------------------------- */ |
| 269 | + int_data= 999; /* integer */ |
| 270 | + strncpy(str_data, "LALA", STRING_SIZE); /* string */ |
| 271 | + str_length= strlen(str_data); |
| 272 | + |
| 273 | + t.year= 2017; |
| 274 | + t.month= 02; |
| 275 | + t.day= 03; |
| 276 | + |
| 277 | + t.hour= 10; |
| 278 | + t.minute= 45; |
| 279 | + t.second= 20; |
| 280 | + |
| 281 | + /* INSERT SMALLINT data as NULL */ |
| 282 | + is_null= 1; |
| 283 | + |
| 284 | + /* Execute the INSERT statement - 1*/ |
| 285 | + mysql_stmt_execute(stmt2); |
| 286 | + |
| 287 | + mysql_stmt_execute(stmt); |
| 288 | + |
| 289 | + /* Close the statement */ |
| 290 | + mysql_stmt_close(stmt2); |
| 291 | + |
| 292 | + mysql_stmt_close(stmt); |
| 293 | +} |
0 commit comments