C++利用SQLite實(shí)現(xiàn)命令行工具
本文介紹了一個(gè)基于 C++、SQLite 和 Boost 庫的簡單交互式數(shù)據(jù)庫操作 Shell。該 Shell 允許用戶通過命令行輸入執(zhí)行各種數(shù)據(jù)庫操作,包括添加、刪除主機(jī)信息,設(shè)置主機(jī)到特定主機(jī)組,以及顯示主機(jī)和主機(jī)組列表。通過調(diào)用 SQLite3 庫實(shí)現(xiàn)數(shù)據(jù)庫連接和操作,以及使用 Boost 庫進(jìn)行字符串解析和格式化。該交互式 Shell 提供了一些基本的命令,使用戶能夠方便地管理主機(jī)信息和組織結(jié)構(gòu)。代碼結(jié)構(gòu)清晰,易于理解,可根據(jù)需要擴(kuò)展和定制功能。
數(shù)據(jù)庫的基本使用方法請看《C/C++ 通過SQLiteSDK增刪改查》這篇文章,針對(duì)如何使用Boost解析命令行參數(shù)請看《C++ Boost 命令行解析庫》這篇文章,此處只給出實(shí)現(xiàn)代碼,如下所示;
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #include <map> #include <vector> #include <time.h> #include "sqlite3.h" #include <boost/format.hpp> #include <boost/tokenizer.hpp> #include <boost/lexical_cast.hpp> using namespace std; using namespace boost; sqlite3* open_database(std::string database_name) { int ref = -1; sqlite3 *db = 0; ref = sqlite3_open(database_name.c_str(), &db); if (ref == SQLITE_OK) return db; return false; } bool close_database(sqlite3 *db) { int ref = sqlite3_close(db); if (ref == SQLITE_OK) return true; return false; } bool exec_sql(sqlite3 *db, char *sql) { char *error_code = 0; int ref = sqlite3_exec(db, sql, 0, 0, &error_code); if (ref == SQLITE_OK) { return true; } return false; } // 初始化創(chuàng)建表結(jié)構(gòu) void Init_Database() { sqlite3* open_db = open_database("./database.db"); if (open_db != false) { std::string sql = "create table HostDB(" "uid primary key," "host_address char(128) not null," "host_username char(128) not null," "host_password char(128) not null," "host_port char(128) not null," "host_group char(128) not null default 'DefaultGroup'" ");"; char run_sql[1024] = { 0 }; strcpy(run_sql, sql.c_str()); exec_sql(open_db, run_sql); } close_database(open_db); } // 增加一條主機(jī)記錄 void AddHost_DB(sqlite3* open_db, std::string address, std::string username, std::string password, std::string port) { std::string format_string = boost::str(boost::format("insert into HostDB(host_address,host_username,host_password,host_port) values('%s','%s','%s','%s');") % address %username %password %port); char run_sql[2048] = { 0 }; strcpy(run_sql, format_string.c_str()); bool ref = exec_sql(open_db, run_sql); if (ref == true) { std::cout << "[+] 增加主機(jī): " << address << " 完成" << std::endl; } } // 刪除特定主機(jī)記錄 void DeleteHost_DB(sqlite3 *open_db, std::string address) { std::string format_string = boost::str(boost::format("delete from HostDB where host_address = '%s';") % address); char run_sql[2048] = { 0 }; strcpy(run_sql, format_string.c_str()); bool ref = exec_sql(open_db, run_sql); if (ref == true) { std::cout << "[-] 刪除主機(jī): " << address << " 完成" << std::endl; } } // 將特定主機(jī)加入到特定主機(jī)組 void SetHostGroup_DB(sqlite3* open_db, std::string address, std::string group_name) { std::string format_string = boost::str(boost::format("update HostDB set host_group='%s' where host_address = '%s';") % group_name %address); char run_sql[2048] = { 0 }; strcpy(run_sql, format_string.c_str()); bool ref = exec_sql(open_db, run_sql); if (ref == true) { std::cout << "[+] 主機(jī): " << address << " 已加入到: " << group_name << " 組" << std::endl; } } // 輸出所有主機(jī)組 void ShowHostGroup_DB(sqlite3 *open_db) { sqlite3_stmt *stmt = 0; // std::string format_string = "SELECT distinct(host_group) FROM 'HostDB';"; std::string format_string = "SELECT host_group,count(*) FROM HostDB GROUP BY host_group;"; char run_sql[1024] = { 0 }; strcpy(run_sql, format_string.c_str()); int ref = sqlite3_prepare_v2(open_db, run_sql, -1, &stmt, 0); if (ref == SQLITE_OK) { while (sqlite3_step(stmt) == SQLITE_ROW) { const unsigned char *host_group = sqlite3_column_text(stmt, 0); int host_group_count = sqlite3_column_int(stmt, 1); std::cout << host_group << " " << host_group_count << std::endl; } } sqlite3_finalize(stmt); } // 輸出所有主機(jī) void ShowHost_DB(sqlite3 *open_db) { sqlite3_stmt *stmt = 0; std::string format_string = "select * from HostDB;"; char run_sql[1024] = { 0 }; strcpy(run_sql, format_string.c_str()); int ref = sqlite3_prepare_v2(open_db, run_sql, -1, &stmt, 0); if (ref == SQLITE_OK) { while (sqlite3_step(stmt) == SQLITE_ROW) { const unsigned char *host_address = sqlite3_column_text(stmt, 1); const unsigned char *host_username = sqlite3_column_text(stmt, 2); const unsigned char *host_paddword = sqlite3_column_text(stmt, 3); const unsigned char *host_port = sqlite3_column_text(stmt, 4); const unsigned char *host_group = sqlite3_column_text(stmt, 5); std::cout << host_address << " " << host_username << " " << host_paddword << " " << host_port << " " << host_group << std::endl; } } sqlite3_finalize(stmt); } // 輸出特定主機(jī)組中的主機(jī) void ShowGroupHostList(sqlite3 *open_db, std::string group_name) { sqlite3_stmt *stmt = 0; std::string format_string = boost::str(boost::format("select * from HostDB where host_group = '%s';") % group_name); char run_sql[2048] = { 0 }; strcpy(run_sql, format_string.c_str()); int ref = sqlite3_prepare_v2(open_db, run_sql, -1, &stmt, 0); if (ref == SQLITE_OK) { std::cout << "----------------------------------------------------------" << std::endl; std::cout << "主機(jī)組: " << group_name << std::endl; std::cout << "----------------------------------------------------------" << std::endl; while (sqlite3_step(stmt) == SQLITE_ROW) { const unsigned char *host_address = sqlite3_column_text(stmt, 1); const unsigned char *host_username = sqlite3_column_text(stmt, 2); const unsigned char *host_port = sqlite3_column_text(stmt, 4); std::cout << host_address << " " << host_username << " " << host_port << std::endl; } } sqlite3_finalize(stmt); } int main(int argc, char const *argv[]) { sqlite3* open_db = open_database("./database.db"); Init_Database(); std::string command; while (1) { std::cout << "[ LyShark Shell ] # "; std::getline(std::cin, command); if (command.length() == 0) { continue; } else if (command == "help") { std::cout << "幫助菜單" << std::endl; } else { boost::char_separator<char> sep(", --"); typedef boost::tokenizer<boost::char_separator<char>> CustonTokenizer; CustonTokenizer tok(command, sep); std::vector<std::string> vecSegTag; for (CustonTokenizer::iterator beg = tok.begin(); beg != tok.end(); ++beg) { vecSegTag.push_back(*beg); } if (vecSegTag.size() == 9 && vecSegTag[0] == "AddHost") { if (vecSegTag[1] == "address" && vecSegTag[3] == "username" && vecSegTag[5] == "password" && vecSegTag[7] == "port") { std::string set_address = vecSegTag[2]; std::string set_username = vecSegTag[4]; std::string set_password = vecSegTag[6]; std::string set_port = vecSegTag[8]; AddHost_DB(open_db, set_address, set_username, set_password, set_port); } } else if (vecSegTag.size() == 3 && vecSegTag[0] == "DeleteHost") { if (vecSegTag[1] == "address") { std::string set_address = vecSegTag[2]; DeleteHost_DB(open_db, set_address); } } else if (vecSegTag.size() == 5 && vecSegTag[0] == "SetHostGroup") { if (vecSegTag[1] == "address" && vecSegTag[3] == "group") { std::string set_address = vecSegTag[2]; std::string set_group = vecSegTag[4]; SetHostGroup_DB(open_db, set_address, set_group); } } else if (vecSegTag.size() == 1 && vecSegTag[0] == "ShowHost") { std::cout << "-----------------------------------------------------------------------------" << std::endl; std::cout << "IP地址 " << "用戶名 " << "密碼 " << "端口號(hào) " << "默認(rèn)組 " << std::endl; std::cout << "-----------------------------------------------------------------------------" << std::endl; ShowHost_DB(open_db); } else if (vecSegTag.size() == 1 && vecSegTag[0] == "ShowHostGroup") { std::cout << "-----------------------------------------------------------------------------" << std::endl; std::cout << "主機(jī)組名 " << "主機(jī)數(shù)量 " << std::endl; std::cout << "-----------------------------------------------------------------------------" << std::endl; ShowHostGroup_DB(open_db); } else if (vecSegTag.size() == 3 && vecSegTag[0] == "ShowGroupHostList") { if (vecSegTag[1] == "group") { std::string set_group = vecSegTag[2]; ShowGroupHostList(open_db, set_group); } } } } close_database(open_db); return 0; }
添加主機(jī)記錄: AddHost --address 192.168.1.1 --username root --password 1233 --port 22
刪除主機(jī)記錄: DeleteHost --address 192.168.1.1
將特定主機(jī)設(shè)置到主機(jī)組: SetHostGroup --address 192.168.1.1 --group WebServer
輸出所有主機(jī)列表: ShowHost
輸出所有主機(jī)組: ShowHostGroup
輸出特定主機(jī)組中的主機(jī): ShowGroupHostList --group DefaultGroup
到此這篇關(guān)于C++利用SQLite實(shí)現(xiàn)命令行工具的文章就介紹到這了,更多相關(guān)C++ SQLite命令行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++、python和go語言實(shí)現(xiàn)的簡單客戶端服務(wù)器代碼示例
這篇文章主要介紹了C++、python和go語言實(shí)現(xiàn)的簡單客戶端服務(wù)器代碼示例,本文分別給出了3種語言的客戶端服務(wù)器通信代碼實(shí)例,需要的朋友可以參考下2015-03-03C++實(shí)現(xiàn)賓館房間管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)賓館房間管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05VC實(shí)現(xiàn)五子棋游戲的一個(gè)算法示例
這篇文章主要介紹了VC實(shí)現(xiàn)五子棋游戲的一個(gè)算法示例,對(duì)于學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)與算法的朋友有一定的借鑒價(jià)值,需要的朋友可以參考下2014-08-08