1、执行SQL语句函数:
int mysql_query(MYSQL* mysql, const char * query);
query:所有的sql语句
2、例子: 向children表插入一条语句,查看sql语句改动的行数量。
/* * MysqlQuery.c * * Created on: Sep 8, 2013 * Author: root */ #include <stdlib.h> #include <stdio.h> #include <mysql/mysql.h> int main(){ MYSQL my_connection; int res; mysql_init(&my_connection); //if(mysql_real_connect(&my_connection, "localhost", "root", "ROOT-123456", "icmp",0, NULL, 0)){ if(mysql_real_connect(&my_connection, "localhost", "root", "ROOT123456", "icmp",0, NULL, 0)){ printf("Connection Succeed. "); res = mysql_query(&my_connection, "insert into children(fname, age) values('www', 3)"); if(!res){ printf("Inserted %lu rows ", (unsigned long)mysql_affected_rows(&my_connection)); }else{ fprintf(stderr, "Insert error %d %s ", mysql_errno(&my_connection), mysql_error(&my_connection)); return -1; } mysql_close(&my_connection); printf("Connection closed. "); } else{ fprintf(stderr, "Connection failed. "); if(mysql_errno(&my_connection)){ fprintf(stderr, "Connection error:%d %s ", mysql_errno(&my_connection), mysql_error(&my_connection)); return -2; } } return 0; }