执行SQL语句的增、删、改、查的主要API函数为:
- int mysql_query(MYSQL *connection, const char *query);
函数接收参数连接句柄和字符串形式的有效SQL语句(没有结束的分号,这与MySQL工具不同)。如果成功,它返回0。
如果包含二进制数据的查询,要使用mysql_real_query.
检查受查询影响的行数:
- my_ulonglong mysql_affected_rows(MYSQL *connection);
my_ulonglong是无符号长整形,为%lu格式
这个函数返回受之前执行update,insert或delete查询影响的行数。
例子
数据库中有一个student表
- CREATE TABLE student (
- student_no varchar(12) NOT NULL PRIMARY KEY,
- student_name varchar(12) NOT NULL
- );
增、删、改代码:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "mysql.h"
- #include "errmsg.h"
- #include "mysqld_error.h"
-
-
- MYSQL conn;
-
- void connection(const char* host, const char* user, const char* password, const char* database) {
- mysql_init(&conn);
-
- if (mysql_real_connect(&conn, host, user, password, database, 0, NULL, 0)) {
- printf("Connection success!\n");
- } else {
- fprintf(stderr, "Connection failed!\n");
- if (mysql_errno(&conn)) {
- fprintf(stderr, "Connection error %d: %s\n", mysql_errno(&conn), mysql_error(&conn));
- }
- exit(EXIT_FAILURE);
- }
- }
-
- void insert() {
- int res = mysql_query(&conn, "INSERT INTO student(student_no,student_name) VALUES('123465', 'Ann')");
- if (!res) {
- printf("Inserted %lu rows\n", (unsigned long)mysql_affected_rows(&conn));
- } else {
- fprintf(stderr, "Insert error %d: %s\n", mysql_errno(&conn), mysql_error(&conn));
- }
- }
-
- void update() {
- int res = mysql_query(&conn, "UPDATE student SET student_name='Anna' WHERE student_no='123465'");
- if (!res) {
- printf("Update %lu rows\n", (unsigned long)mysql_affected_rows(&conn));
- } else {
- fprintf(stderr, "Update error %d: %s\n", mysql_errno(&conn), mysql_error(&conn));
- }
- }
-
- void delete() {
- int res = mysql_query(&conn, "DELETE from student WHERE student_no='123465'");
- if (!res) {
- printf("Delete %lu rows\n", (unsigned long)mysql_affected_rows(&conn));
- } else {
- fprintf(stderr, "Delete error %d: %s\n", mysql_errno(&conn), mysql_error(&conn));
- }
- }
-
-
- int main (int argc, char *argv[]) {
-
- connection("localhost", "root", "shuang", "shuangde");
- delete();
- mysql_close(&conn);
- exit(EXIT_SUCCESS);
- }
返回数据的语句:select
SQL最常见的用法是提取数据而不是插入或更新数据。数据是用select语句提取的
C应用程序提取数据一般需要4个步骤:
1、执行查询
2、提取数据
3、处理数据
4、必要的清理工作
就像之前的insert和update一样,使用mysql_query来发送SQL语句,然后使用mysql_store_result或mysql_use_result来提取数据,具体使用哪个语句取决于你想如何提取数据。接着,将使用一系列mysql_fetch_row来处理数据。最后,使用mysql_free_result释放查询占用的内存资源。
一次提取所有数据:mysql_store_result
-
-
-
- MYSQL_RES *mysql_store_result(MYSQL *connection);
-
-
- my_ulonglong mysql_num_rows(MYSQL_RES *result);
-
-
-
- MYSQL_ROW mysql_fetch_row(MYSQL_RES *resutl);
-
-
-
-
- void mysql_data_seek(MYSQL_RES *result, my_ulonglong offset);
-
-
-
- MYSQL_ROW_OFFSET mysql_row_tell(MYSQL_RES *result);
-
-
-
- MYSQL_ROW_OFFSET mysql_row_seek(MYSQL_RES *result, MYSQL_ROW_OFFSET offset);
-
-
-
- void mysql_free_result(MYSQL_RES *result);
在使用mysql_query()进行一个查询后,一般要用这两个函数之一来把结果存到一个MYSQL_RES *变量中。
两者的主要区别是,mysql_use_result()的结果必须“一次性用完”,也就是说用它得到一个result后,必须反复用mysql_fetch_row()读取其结果直至该函数返回null为止,否则如果你再次进行mysql查询,会得到“Commands out of sync; you can't run this command now”的错误。
而mysql_store_result()得到result是存下来的,你无需把全部行结果读完,就可以进行另外的查询。比如你进行一个查询,得到一系列记录,再根据这些结果,用一个循环再进行数据库查询,就只能用mysql_store_result()。
示例代码:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "mysql.h"
- #include "errmsg.h"
- #include "mysqld_error.h"
-
-
- MYSQL conn;
- MYSQL_RES *res_ptr;
- MYSQL_ROW sqlrow;
-
- void connection(const char* host, const char* user, const char* password, const char* database) {
- mysql_init(&conn);
-
- if (mysql_real_connect(&conn, host, user, password, database, 0, NULL, 0)) {
- printf("Connection success!\n");
- } else {
- fprintf(stderr, "Connection failed!\n");
- if (mysql_errno(&conn)) {
- fprintf(stderr, "Connection error %d: %s\n", mysql_errno(&conn), mysql_error(&conn));
- }
- exit(EXIT_FAILURE);
- }
- }
-
- int main (int argc, char *argv[]) {
-
- connection("localhost", "root", "shuang", "shuangde");
- int res = mysql_query(&conn, "SELECT * from student");
- if (res) {
- fprintf(stderr, "SELECT error: %s\n", mysql_error(&conn));
- } else {
- res_ptr = mysql_store_result(&conn);
- if (res_ptr) {
- printf("Retrieved %lu rows\n", (unsigned long)mysql_num_rows(res_ptr));
- while ((sqlrow = mysql_fetch_row(res_ptr))) {
- printf("Fetched data...\n") ;
- }
- if (mysql_errno(&conn)) {
- fprintf(stderr, "Retrive error: %s\n", mysql_error(&conn));
- }
- mysql_free_result(res_ptr);
- }
- }
- mysql_close(&conn);
- exit(EXIT_SUCCESS);
- }
一次提取一行数据:mysql_use_result
使用方法和mysql_store_result完全一样,把上面代码的mysql_store_result改为mysql_use_result即可。
mysql_use_result具备资源管理方面的实质性好处,更好地平衡了网络负载,以及减少了可能非常大的数据带来的存储开销,但是不能与mysql_data_seek、mysql_row_seek、mysql_row_tell、mysql_num_rows一起使用。如果数据比较少,用mysql_store_result更好。
处理返回的数据
-
-
-
-
- unsigned int mysql_field_count(MYSQL *connection);
-
-
- MYSQL_FIELD *mysql_fetch_field(MYSQL *result);
-
-
- MYSQL_FIELD_OFFSET mysql_field_seek(MYSQL *result, MYSQL_FIELD_OFFSET offset);
-
-
-
- char *name;
- char *table;
- char *def;
- enum enum_field_types type;
- unsigned int length;
- unsigned int max_length;
- unsigned int flags;
-
-
-
-
-
- unsigned int decimals;
-
-
-
-
-
-
-
-
-
代码示例:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "mysql.h"
- #include "errmsg.h"
- #include "mysqld_error.h"
-
-
- MYSQL conn;
- MYSQL_RES *res_ptr;
- MYSQL_ROW sqlrow;
-
- void connection(const char* host, const char* user, const char* password, const char* database) {
- mysql_init(&conn);
-
- if (mysql_real_connect(&conn, host, user, password, database, 0, NULL, 0)) {
- printf("Connection success!\n");
- } else {
- fprintf(stderr, "Connection failed!\n");
- if (mysql_errno(&conn)) {
- fprintf(stderr, "Connection error %d: %s\n", mysql_errno(&conn), mysql_error(&conn));
- }
- exit(EXIT_FAILURE);
- }
- }
-
- void display_row() {
- unsigned int field_count = mysql_field_count(&conn);
- int i = 0;
- while (i < field_count) {
- if (sqlrow[i]) printf("%s ", sqlrow[i]);
- else printf("NULL");
- i++;
- }
- printf("\n");
- }
-
- void display_header() {
- MYSQL_FIELD *field_ptr;
- printf("Column details:\n");
- while ((field_ptr = mysql_fetch_field(res_ptr)) != NULL) {
- printf("\t Name: %s\n", field_ptr->name);
- printf("\t Table: %s\n", field_ptr->table);
- printf("\t Type: ");
- if (IS_NUM(field_ptr->type)) {
- printf("Numeric field\n");
- } else {
- switch(field_ptr->type) {
- case FIELD_TYPE_VAR_STRING:
- printf("VARCHAR\n");
- break;
- case FIELD_TYPE_LONG:
- printf("LONG");
- break;
- default:
- printf("Type is %d, check in msyql_com.h\n", field_ptr->type);
- }
- }
- printf("\t Max width %ld\n", field_ptr->length);
- if (field_ptr->flags & AUTO_INCREMENT_FLAG)
- printf("\t Auto increments\n");
- printf("\n");
- }
- }
-
- int main (int argc, char *argv[]) {
-
- connection("localhost", "root", "shuang", "shuangde");
- int res = mysql_query(&conn, "SELECT * from student");
- if (res) {
- fprintf(stderr, "SELECT error: %s\n", mysql_error(&conn));
- } else {
- res_ptr = mysql_use_result(&conn);
- if (res_ptr) {
- int first = 1;
- while ((sqlrow = mysql_fetch_row(res_ptr))) {
- if (first) {
- display_header();
- first = 0;
- }
- display_row();
- }
- if (mysql_errno(&conn)) {
- fprintf(stderr, "Retrive error: %s\n", mysql_error(&conn));
- }
- mysql_free_result(res_ptr);
- }
- }
- mysql_close(&conn);
- exit(EXIT_SUCCESS);
- }