今天处理了一下配置文件的问题,毕竟不能啥都往程序里写对吧……其实我以前一直是记好了第几行是啥,然后直接读进来。不过这样显然不太优雅,所以要用点啥格式。本来是想用GKeyFile来处理的,然后它要include一个glib.h的头文件,大蜥蜴里面这个文件有点问题,路径各种不对。JSON吧,按说不错,但是手写的话格式不是那么友好,XML就更别提了。加上之前被安利过一波YAML,就决定用它了。
YAML,说来也简单,直接安装yaml-cpp-devel就可以了,封装的很好,很方便。没用什么高级的配置,看着代码自己悟吧(滑稽
嗯……话说昨天不小心暴露了我的数据库密码,应该没关系吧?
Database Info: Host: ali.himmel.tech Username: jxsong Password: nicopoiduang
#include <yaml-cpp/yaml.h> #include <mysql_connection.h> #include <mysql_driver.h> #include <cppconn/statement.h> using namespace sql; using namespace std; int main(int argc, char **argv) { YAML::Node config = YAML::LoadFile("config.yaml"); config = config["Server Info"]; string host = config["Host"].as<std::string>(); string username = config["Username"].as<std::string>(); string password = config["Password"].as<string>(); mysql::MySQL_Driver *driver; Connection *con; Statement *state; ResultSet *result; // 初始化驱动 driver = sql::mysql::get_mysql_driver_instance(); // 建立链接 con = driver->connect(host, username, password); state = con->createStatement(); state->execute("use test"); state -> execute("INSERT INTO Books (Name, ISBN, Price, Count) VALUES ('自动打Call原理' , 80211, 199.8, 2999)"); // 查询 result = state->executeQuery("select * from Books"); // 输出查询 while(result->next()) { cout << result -> getString("Name") << " : $" << result -> getDouble("Price") << endl; } state -> execute("DELETE FROM Books WHERE Name = '自动打Call原理'"); delete state; delete con; return 0; }