栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

量化交易之MySql篇 - mysql数据库 增删改查

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

量化交易之MySql篇 - mysql数据库 增删改查

#include 
#include 

int main(int argc, char *argv[]) {

    MYSQL mysql;
    mysql_init(&mysql);

    const char *host = "127.0.0.1";
    const char *user = "root";
    const char *passwd = "密码";
    const char *db = "mysql";  // tqzschema

    // 连接登录数据库
    bool connect_ret = mysql_real_connect(&mysql, host, user, passwd, db, 3306, 0, 0);
    if (!connect_ret) { // 登录失败
        std::cout << "connect failed: " << mysql_error(&mysql) << std::endl;
    } else {
        std::cout << "connect success: " << host << std::endl;
    }
    
    
    // 创建表
    string sql = "create table `t_image` (
        `id` int AUTO_INCREMENT, 
        `name` varchar(1024), 
        `path` varchar(2048), 
        `size` int, 
        PRIMARY KEY(`id`)
    )";
    int ret = mysql_query(&mysql, sql.c_str());
    if (ret != 0) { // 出错
        std::cout << "mysql_query faied! " << mysql_error(&mysql) << std::endl;
    }
    
    
    // 插入数据
    for (int i = 0; i < 1000; i++) {
        stringstream insert_sql;
        insert_sql << "insert `t_image` (`name`,`path`,`size`) values('test"
        insert_sql << i << ".jpg','d:/img/test.jpg',10240)"
        
        int ret = mysql_query(&mysql, sql.c_str());
        if (ret == 0) { // 成功
            int count = mysql_affected_rows(&mysql);
            std::cout << "mysql_affected_rows: " << count << ", current id: " << mysql_insert_id(&mysql) << std::endl;
        } else {
            std::cout << "mysql_query faied! " << mysql_error(&mysql) << std::endl;
        }
    }
    
    
    // 修改数据
    string update_sql = "update t_image set `name`='test3.png',size=2000 where id=1";
    int update_ret = mysql_query(&mysql, sql.c_str());
    
    if (update_ret == 0) {
        int count = mysql_affected_rows(&mysql);
        std::cout << "update mysql_affected_rows: " << count << std::endl;
    } else {
        std::cout << "update failed!" << mysql_error(&mysql) << std::endl;
    }
    
    
    map keyValues;
    
    keyValues.insert(make_pair("name", "image_update001.jpg"));
    keyValues.insert(make_pair("size", 5000));
    string where = "where id=1";
    
    string temp = "";
    for (auto iterator = keyValues.begin(); iterator != keyValues.end(); iterator++) {
        temp += "`";
        temp += iterator->first;
        temp += "`='"
        temp += iterator->second;
        temp += "',"
    }
    temp += "id=id";
    sql = "update t_image set";
    sql += temp;
    sql += where;
    
    
    
    // 删除数据
    // string delete_sql = "truncate t_image"; // 清空数据, 并恢复自增id从1开始
    string delete_sql = "delete from t_image"; // delete 不会实际删除数据库的空间
    int delete_ret = mysql_query(&mysql, delete_sql.c_str());
    
    if (delete_ret == 0) {
        int count = mysql_affected_rows(&mysql);
        std::cout << "delete mysql_affected_rows: " << count << endl;
    } else {
        std::cout << "delete failed! " << mysql_error(&mysql) << std::endl;
    }
    delete_sql = "OPTIMIZE TABLE t_image"; // 优化删除, 会清理数据库空间
    delete_ret = mysql_query(&mysql, delete_sql.c_str());
    
    
    mysql_close(&mysql);
    mysql_library_end();

    return EXIT_SUCCESS;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/928598.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号