mysql嵌入式

mysql C编程

要想使用mysql, 需要先开启服务端,然后以下代码为客户端,首先连接数据库,然后再进行各种增删改查操作。
本代码不能直接运行
需要链库 -lmysqlclient

#include "mysql.h"
#include <malloc.h>	

int ConnectDB()
{
    // global variable mysql
    if (NULL == mysql_init(&mysql)) {    //分配和初始化MYSQL对象
        printf("mysql_init(): %s\n", mysql_error(&mysql));
        return -1;
    }

    //尝试与运行在主机上的MySQL数据库引擎建立连接
    if (NULL == mysql_real_connect(&mysql,
                "localhost",    // host
                "root",         // username
                "ubuntu",       // password
                "Xianghan", // DATABASE NAME
                0,
                NULL,
                0)) {
        printf("mysql_real_connect(): %s\n", mysql_error(&mysql));
        return -1;
    }

    printf("Connected MySQL successful! \n");

    return SUCCESS;
}

{
	    int             rc;
        char            query_str[500] = {0};
    //    INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
    //    query_str = "insert into Sip_channel (%1, %2, %3 ...) values ( % , % , % , % )";
    
        //    UPDATE table_name
        //    SET column1=value1,column2=value2,...
        //    WHERE some_column=some_value;
    
    //    Sip_server
    //    strcpy(query_str, "UPDATE sip_server  "
    //                      "SET server_id=%s,"               // SipServerID
    //                      "server_domain=%s,"               // SipServerDomain
    //                      "server_ip=%s, "                  // SipServerIp
    //                      "server_port=%d,"                 // SipServerPort
    //                      "type=1"
    //           );
    
        snprintf(query_str, sizeof(query_str),"UPDATE sip_server  "
                                              "SET server_id=%s,"               // SipServerID
                                              "server_domain=%s,"               // SipServerDomain
                                              "server_ip=\"%s\", "              // SipServerIp
                                              "server_port=%d,"                 // SipServerPort
                                              "type=1",
                 SipServerID, SipServerDomain, SipServerIp, SipServerPort);
        printf("query_str = %s\n", query_str);
    
    //    execute
        rc = mysql_real_query(&mysql, query_str, strlen(query_str));
        if (0 != rc) {
            printf("mysql_real_query(): %s\n", mysql_error(&mysql));
            return -1;
        }
    
    
    //    Sip_channel
    //    strcpy(query_str, "UPDATE sip_channel "
    //                      "SET id=1, "
    //                      "sip_connection_id=%s,"           //
    //                      "sip_video_channel_id=%s,"        //
    //                      " civilcode=$d"
    //           );
    
    //    execute
    //    rc = mysql_real_query(&mysql, query_str, strlen(query_str));
    //    if (0 != rc) {
    //        printf("mysql_real_query(): %s\n", mysql_error(&mysql));
    //        return -1;
    //    }
    
    
    //    Sip_connection
    //    strcpy(query_str, "UPDATE sip_connection "
    //                      "SET local_port=%d,"              // SipLocalPort
    //                      "user_auth_id=%s,"                // SipUserRegID
    //                      "user_auth_pwd=%s, "              // SipUserPasswd
    //                      "valid_reg_period=%d,"            // valid_reg_period
    //                      "heart_cycle=%d, "                // HeartCycle
    //                      "max_heart_timeout=%d,"           // Hearttime
    //                      "user_auth_name=%d"               // SipUserRegName
    //           );
        snprintf(query_str, sizeof(query_str), "UPDATE sip_connection "		   // 写sql语句
                                             "SET local_port=%d,"              // SipLocalPort
                                             "user_auth_id=\"%s\","            // SipUserRegID
                                             "user_auth_pwd=%s, "              // SipUserPasswd
                                             "valid_reg_period=%d,"            // valid_reg_period
                                             "heart_cycle=%d, "                // HeartCycle
                                             "max_heart_timeout=%d,"           // Hearttime
                                             "user_auth_name=\"%s\"",          // SipUserRegName
              SipLocalPort , SipUserRegID, SipUserPasswd, valid_reg_period, HeartCycle, Hearttime, SipUserRegName);
        printf("query_str = %s\n", query_str);
    //    execute
        rc = mysql_real_query(&mysql, query_str, strlen(query_str));		// 执行sql语句
        if (0 != rc) {
            printf("mysql_real_query(): %s\n", mysql_error(&mysql));
            return -1;
        }
    
    //    printf("UPDATE sip_server SET server_domain=%s, server_ip=%s, server_port=%d", SipServerDomain,SipServerIp, SipServerPort);
    //    printf(query_str,"\n");
    
    //    printf(query_str);
    
    // for insert
    //    res = mysql_store_result(&mysql);
    //    if (NULL == res) {
    //         printf("mysql_restore_result(): %s\n", mysql_error(&mysql));
    //         return -1;
    //    }
    //    rows = mysql_num_rows(res);
    //    printf("The total rows is: %d\n", rows);
    //    fields = mysql_num_fields(res);
    //    printf("The total fields is: %d\n", fields);
    //    while ((row = mysql_fetch_row(res))) {
    //        for (i = 0; i < fields; i++) {
    //            printf("%s\t", row[i]);
    //        }
    //        printf("\n");
    //    }
    }


  int ReleaseDB()
    {
        mysql_close(&mysql);
        return SUCCESS;
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值