本博客主要讲述Center的模块安装配置和数据初始化
1、定义安装Install函数,IP地址由makefile自动传入,也就是用户自动传入
bool Install(std::string ip);
2、编写Install函数
#define CENTER_CONF "ip
bool XCenter::Install(std::string ip)
{//1、生成配置文件,数据库的IP//ofstream默认输出ofstream of;of.open(CENTER_CONF);if (!of.is_open()){cout << "open config" << CENTER_CONF << "failed!" << endl;return false;}//写入,将ip写入到流当中of << ip;of.close();cout << "XCenter::Install! "<<ip << endl;return true;
}
3、初始化数据库Init函数
//初始化数据库bool Init();
4、编写Init函数
bool XCenter::Init()
{//只能调用一次,直接new一个my = new LXMysql();string ip = "";//读取数据库ip配置ifstream fs;fs.open(CENTER_CONF);if (!fs.is_open()){//配置项不存在cout << "Please install center" << endl;return false;}fs >> ip;fs.close();if (ip.empty()){cout << "ip is empty! Please install center" << endl;return false;}cout << "Init center" << ip << endl;if (!my->Connect(ip.c_str(), "fdd", "990107Wjl@", "test_fdd")){cerr << "db Connect failed!" << endl;return false;}cout << "db Connect success!" << endl;return my->Query("set names utf8");
}
5、将Init放进Install函数里面,Install函数的最终定义为
bool XCenter::Install(std::string ip)
{//1、生成配置文件,数据库的IP//ofstream默认输出ofstream of;of.open(CENTER_CONF);if (!of.is_open()){cout << "open config" << CENTER_CONF << "failed!" << endl;return false;}//写入,将ip写入到流当中of << ip;of.close();//2、初始化表格数据//2.1、先连接上数据库if (!Init()){cerr << "Init failed!" << endl;return false;}cout << "XCenter::Install! "<<ip << endl;return true;
}
完整代码:
XCenter.h
#ifndef XCENTER_H
#define XCENTER_H
#include<string>
#include"LXMysql.h"
class XCenter
{
public:static XCenter* Get(){//不考虑线程安全static XCenter a;return &a;}bool Install(std::string ip);//初始化数据库bool Init();~XCenter();
private:XCenter();LX::LXMysql* my = 0;
};#endif // !XCENTER_H
XCenter.cpp
#include "XCenter.h"
#include<iostream>
#include<fstream>
#define CENTER_CONF "ip"
using namespace std;
using namespace LX;bool XCenter::Install(std::string ip)
{//1、生成配置文件,数据库的IP//ofstream默认输出ofstream of;of.open(CENTER_CONF);if (!of.is_open()){cout << "open config" << CENTER_CONF << "failed!" << endl;return false;}//写入,将ip写入到流当中of << ip;of.close();//2、初始化表格数据//2.1、先连接上数据库if (!Init()){cerr << "Init failed!" << endl;return false;}cout << "XCenter::Install! "<<ip << endl;return true;
}bool XCenter::Init()
{//只能调用一次,直接new一个my = new LXMysql();string ip = "";//读取数据库ip配置ifstream fs;fs.open(CENTER_CONF);if (!fs.is_open()){//配置项不存在cout << "Please install center" << endl;return false;}fs >> ip;fs.close();if (ip.empty()){cout << "ip is empty! Please install center" << endl;return false;}cout << "Init center" << ip << endl;if (!my->Connect(ip.c_str(), "fdd", "990107Wjl@", "test_fdd")){cerr << "db Connect failed!" << endl;return false;}cout << "db Connect success!" << endl;return my->Query("set names utf8");
}XCenter::XCenter()
{}XCenter::~XCenter()
{}
Center.cpp
#include <iostream>
#include<string>
#include"XCenter.h"
using namespace std;
//做一个系统的说明,使用说明
void Usage()
{cout << "=====================Center Usage" << endl;cout << "./center install 127.0.0.1" << endl;
}
int main(int argc,char*argv[])
{//argc参数的数量string cmd;//安装系统if (argc > 1){cmd = argv[1];}if (cmd == "install"){cout << "Begin install center!" << endl;//argv[1]第一个参数install,第二个argv[2]是ip//用户可能只是输入了install而没有输入ip,做一个判断if (argc < 3){Usage();return 0;//在makefile里面-1可能认为出错,还是return 0}XCenter::Get()->Install(argv[2]);}std::cout << "Hello World!\n";return 0;
}
makefile
center:Center.cpp XCenter.cpp XCenter.h#-lLXMysql动态链接库,对应的是libLXMysql.sog++ $^ -o $@ -I ../../LXMysql/LXMysql -lLXMysql./$@ install (你的IP)clean:rm -rf *.orm -rf center