1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include <stdio.h> #include <winsock.h> #include <mysql.h>
int main(int argc, char *argv[]) { MYSQL *conn = mysql_init(NULL); if (!mysql_real_connect(conn, "HOST", "USER", "PASSWD", "DB", 3306, 0, 0)) { printf("Connect Error: %s", mysql_error(conn)); exit(-1); }
if (mysql_query(conn, "SET NAMES gbk")) { printf("Set Names Failed: %s", mysql_error(conn)); mysql_close(conn); exit(-1); }
if (mysql_query(conn, "INSERT INTO account ( DATE, CR, DR, SUM, NOTE) VALUES ( CURRENT_DATE(), '1004', '3001', '5', '示例')")) { printf("Insert Failed: %s", mysql_error(conn)); mysql_close(conn); exit(-1); } printf("写入行数:%ld", mysql_affected_rows(conn));
if (mysql_query(conn, "SELECT * FROM account")) { printf("Select Failed: %s", mysql_error(conn)); mysql_close(conn); exit(-1); } MYSQL_RES *result = mysql_store_result(conn); MYSQL_ROW row;
while ( row = mysql_fetch_row( result ) ) { printf("\n%s\t%s\t%s\t%s\t%s\t%s", row[0], row[1], row[2], row[3], row[4], row[5], row[6]); } mysql_free_result(result); mysql_close(conn); return 0; }
|