当前位置:网站首页>量化交易之MySql篇 - mysql数据库 增删改查
量化交易之MySql篇 - mysql数据库 增删改查
2022-06-09 06:24:00 【Post-Truth】
#include <iostream>
#include <mysql.h>
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<string, string> 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;
}边栏推荐
- ping: XXX: 未知的名称或服务原因分析
- ImportError: cannot import name ‘joblib‘ from ‘sklearn. externals‘
- MySQL 5.7 安装教程(全步骤、保姆级教程)
- Unit platform related macros
- Exponential moving weighted average
- SQLServer 导入导出数据,后台有进程,前台无显示。
- Comparison between the most cost-effective processor and domestic processor i.mx6ul/a40i/t3
- Openresty newly added module
- matlab如何向mat文件写入带标题的连续数据
- LDAP application: openldap integrated into open***
猜你喜欢

Encounter nodejs

Exponential moving weighted average

Do you really understand entropy (including cross entropy)

error: subprocess-exited-with-error(fasttext)

全志V3s学习记录--ESP8089的使用

Mutual exclusion and synchronization in kernel

Conversion of data type real and word in PROFIBUS DP communication

mysql 8.0.28版本安装配置方法图文教程

二叉 树

Competition between am335x and Quanzhi a40i
随机推荐
Too many open files
基於國產全志A40I的機器人示教器解决方案
全志平台BSP裁剪(2)附件一 General setup配置说明
Sqlserver imports and exports data. There is a process in the background and no display in the foreground.
LDAP application: openldap integrated into open***
SVN账号密码查找
Coredns part 3-access Prometheus monitoring
error: subprocess-exited-with-error(fasttext)
g1 的日志解析
CodeBlocks老是重复运行上一次结果
TypeScript
Abstract classes and interfaces
Principales différences entre CMS et G1
Coredns Part 1 Introduction and installation
Ping: xxx: unknown name or service reason analysis
RNN foundation of NLP Foundation
邂逅 NodeJS
uniapp引入线上js
Yocto compiling libdrm
The national industrial level Quanzhi t3/a40i core board -com-x40i helps the intelligent power system