执行SQL语句---INSERT/UPDATE/DELETE

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.\n");
        res = mysql_query(&my_connection, "insert into children(fname, age) values('www', 3)");
        if(!res){
            printf("Inserted %lu rows\n", (unsigned long)mysql_affected_rows(&my_connection));
        }else{
            fprintf(stderr, "Insert error %d %s\n", mysql_errno(&my_connection), mysql_error(&my_connection));
            return -1;
        }
        mysql_close(&my_connection);
        printf("Connection closed.\n");
    }
    else{
        fprintf(stderr, "Connection failed.\n");
        if(mysql_errno(&my_connection)){
            fprintf(stderr, "Connection error:%d %s\n", mysql_errno(&my_connection), mysql_error(&my_connection));
            return -2;
        }
    }
    return 0;
}

 

posted @ 2013-09-08 14:46  wangle100  阅读(571)  评论(0编辑  收藏  举报