zoukankan      html  css  js  c++  java
  • 执行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.
    ");
            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;
    }
  • 相关阅读:
    基于Twisted的简单聊天室
    小学题的python实现
    初识Go(8)
    初识Go(7)
    初识Go(6)
    初识Go(5)
    初识Go(4)
    初识Go(3)
    初识Go(2)
    初识Go(1)
  • 原文地址:https://www.cnblogs.com/wangle1001986/p/3308342.html
Copyright © 2011-2022 走看看