初学使用用C语言操作MYSQL,写了个小例子,帖上来献丢人一下,呵呵。
程序很简单,先连接数据库,然后向class1表中插入一条数据,最后获取并输出整个class1表的内容。
上代码:
- //test.c
- //gcc test.c -o test -lmysqlclient
- #include <stdio.h>
- #include <stdlib.h>
- #include <mysql/mysql.h>
-
- //发生错误时,输出错误信息,关闭连接,退出程序
- void error_quit(const char *str, MYSQL *connection)
- {
- fprintf(stderr, "%s : %d: %s\n",
- str, mysql_errno(connection),
- mysql_error(connection));
- if( connection != NULL )
- mysql_close(connection);
- exit(1);
- }
-
- int main(int argc, char *argv[])
- {
- MYSQL *my_con = malloc( sizeof(MYSQL) );
- MYSQL_RES *my_res;
- MYSQL_FIELD *my_field;
- MYSQL_ROW my_row;
- int rows, i;
- int res;
-
- //连接数据库
- mysql_init(my_con);
- my_con = mysql_real_connect(my_con, "localhost", "test", "aaaaaaa",
- "test1", 0, NULL, CLIENT_FOUND_ROWS);
- if( NULL == my_con )
- error_quit("Connection fail", my_con);
- printf("Connection success\n");
-
- //向数据库中插入一条记录
- res = mysql_query(my_con,
- "insert into class1(name, age, birthday) value('abc', 52, NOW());");
- if( res != 0 )
- error_quit("Insert fail", my_con);
- //返回的是表中被影响的行数
- res = mysql_affected_rows(my_con);
- printf("Inserted %d rows\n", res);
-
- //获取整个表的内容
- res = mysql_query(my_con, "select * from class1;");
- if( res != 0 )
- error_quit("Select fail", my_con);
- my_res = mysql_store_result(my_con);
- if( NULL == my_res )
- error_quit("Get result fail", my_con);
-
- //获取表的列数
- rows = mysql_num_fields(my_res);
- //获取并输出表头
- my_field = mysql_fetch_fields(my_res);
- for(i=0; i<rows; i++)
- printf("%s\t", my_field[i].name);
- printf("\n-------------------------------------\n");
-
- //输出整个表的内容
- while( 1 )
- {
- my_row = mysql_fetch_row(my_res);
- if( NULL == my_row )
- break;
- for(i=0; i<rows; i++)
- {
- if( my_row[i] == NULL )
- printf("NULL\t");
- else
- printf("%s\t", (char*)my_row[i]);
- }
- printf("\n");
- }
-
- //释放空间,关闭连接
- mysql_free_result(my_res);
- mysql_close(my_con);
- free(my_con);
-
- return 0;
- }
运行结果:
- Connection success
- Inserted 1 rows
- id name age birthday
- -------------------------------------
- 49 ddd 43 2012-11-09 09:49:41
- 50 fff 31 0000-00-00 00:00:00
- 58 eee 32 NULL
- 59 qqq 43 NULL
- 78 abc 52 2012-11-13 14:47:55
====================================
数据库中存在表table, 有name(varchar类型),age(int类型)。
1.查询语句中引用变量值
在执行INSERT语句时,如果对具体的值进行插入则很简单,如下向数据库表table中插入记录:name='颖', age=32。
mysql_query(MYSQL*, "INSERT INTO table values('曹颖',32)");
但如果插入的是变量的值,如数据是用户输入的,这些数据保存在变量中。与在java中插入变量值的方式完全不同,C语句则要使用如下方式实现:
假如用户输入的姓名和年龄值分别保存在变量name,age中
char sql_insert[200];
sprintf(sql_insert, "INSERT INTO table values('%s','%d');", name, age);
mysql_query(&mysql_conn, sql_insert);
上述语句在执行完sprintf语句后,sql_insert中保存的是INSERT查询语句字符串,sql_insert作为mysql_query()语句的参数即可实现正确的插入。
这里例举的是 INSERT 语句,当然,对其它操作语句如SELECT,UPDATE,DELETE等亦如此。
2.如果提取查询结果值
同样我们查询table表中的所有字段,实现语句如下:
MYSQL_RES *res_ptr; MYSQL_ROW row;
mysql_query(&mysql_conn, select_sql); res_ptr = mysql_store_result(&mysql_conn); while ((row = mysql_fetch_row(res_ptr))) { printf("%s %s\n", row[0], row[1]); }
注意:row[0]中是姓名 -- 是字符串; 而row[1]字段是年龄,应该是int类型,但事实上row[1]的值也是一字符串,所以在获得数据库中记录的字段时,所有的字段均转换为字符串形式提取,所以在打印row[1]时格式段用的是%s,而不是%d 。 |
|