与上一篇不同,本篇添加了大数据处理,对数据库的操作(增删查改)

以下注释均为测试,可忽略

绿色为文件蓝色为目录

这是整体的一个目录:

conf/server.conf:

IP:127.0.0.1PORT:8080

htdocs/index.html:(数据库操作)

                                                                                  

hello,this is database

        

you can choose a operoter...

                

INSERT

            
                name   :
                sex    :
                age    :
                tel    :
                school :
                
                    

SELECT

             

you can choose one or zero ...

           
                select_name   :
                select_sex    :
                select_age    :
                select_tel    :
                select_school :
                select_id     :
                
                    

DELETE

        

you can choose one or zero ...

             
                 delete_name  :
                 delete_sex   :
                 delete_age   :
                       delete_tel   :
                 delete_school:
                 delete_id    :
                 
                     

UPDATE

            
                 updata_name  :
                 
                                   updata_sex   :
                 
                 updata_age   :
                 
                 updata_tel   :
                 
                 updata_school:
                 
                 
                    
    

htdocs/mysql_connect/main.cpp:

#include "sql_api.h" void get_args(std::vector
 &vec,std::string &op)                    {    std::string method=getenv("METHOD");//getenv return NULL to string is error    std::string query_string;    std::string content_length;    char ch='a';    int index=0;    int ret=-1;    char buf[_SIZE_];    memset(buf,'\0',sizeof(buf));    if(method=="POST"){//POST        std::string content_length=getenv("CONTENT_LENGTH");        int len=atoi(content_length.c_str());        memset(buf,'\0',sizeof(buf));        while(len){//&& index
"<
<<""<
<<"

insert success!

"<
<<""<
<<""<
<<"HTTP/1.1 200 OK\r\n\r\n";    std::cout<<""<
<<""<
<<"

delete success!

"<
<<""<
<<""<
<<"HTTP/1.1 200 OK\r\n\r\n";    std::cout<<""<
<<""<
<<"

updata success!

"<
<<""<
<<""<
<<"HTTP/1.1 200 OK\r\n\r\n";    std::cout<<""<
<<""<
<<"

operator failed!Info Is Not Exit or Your Can Check Your Args...

"<
<<""<
<<""<
 vec;    std::string op;    get_args(vec,op);        int vec_size=vec.size();    const char *name=vec[vec_size-1].c_str();    const char *sex=vec[vec_size-2].c_str();    const char *age=vec[vec_size-3].c_str();    const char *tel=vec[vec_size-4].c_str();    const char *school=vec[vec_size-5].c_str();    const char *id=vec[vec_size-6].c_str();        mysql_api sql;                                                                  //连接到MYSQL服务器    sql.mysql_connect_sql();        bool ret=false;    if(op=="insert"){        ret=sql.mysql_insert(name,sex,age,tel,school);        if(ret==true){            ret=false;            insert_echo_html();        }else{            failed_echo_html();        }    }else if(op=="select"){        ret=sql.mysql_select(id,name,sex,age,tel,school);        if(ret==true){            ret=false;        }else{            failed_echo_html();                                                         }    }else if(op=="delete"){//      std::cout<
<

htdocs/mysql_connect/sql_api.h:

#include 
#include 
#include 
                                                               #include 
#include 
#include 
#include 
 #define _SIZE_ 2048 class mysql_api{public:    mysql_api();    void mysql_connect_sql();    bool mysql_select(std::string id="",std::string name="",std::string sex="",std::string age="",std::string tel="",std::string school="");    bool mysql_insert(std::string name,std::string sex,std::string age,std::string tel,std::string school);    bool mysql_delete(std::string id="",std::string name="",std::string sex="",std::string age="",std::string tel="",std::string school="");    bool mysql_updata(std::string oldname="",std::string newname="",std::string oldsex="",std::string newsex="",\  std::string oldage="",std::string newage="",std::string oldtel="",std::string newtel="",std::string oldschool="",std::string newschool="");    bool mysql_close_sql();    ~mysql_api();private:    bool _sql_op(std::string &sql_op);    bool _select_data();private:    MYSQL mysql;    std::string host;                                                               std::string user;    std::string passwd;    std::string db;    unsigned int port;    unsigned long client_flag;};

htdocs/mysql_connect/sql_api.cpp:

#include "sql_api.h" mysql_api::mysql_api()                                                              :host("127.0.0.1")    ,user("root")    ,passwd("密码")    ,db("9_students")    ,port(3306)    ,client_flag(0){    //获得或初始化一个MYSQL结构    mysql_init(&mysql);}void mysql_api::mysql_connect_sql(){    //连接到MYSQL服务器    mysql_real_connect(&mysql,host.c_str(),user.c_str(),passwd.c_str(),db.c_str(),port,NULL,0);}bool mysql_api::mysql_select(std::string id,std::string name,std::string sex,std::string age,std::string tel,std::string school){    //select 列名称 from 表名称 [查询条件];    std::string sql_op="select * from students where ";    if(name!=""){        sql_op.append("name=\"");        sql_op.append(name);        sql_op.append("\"");    }else if(sex!=""){        sql_op.append("sex=\"");        sql_op.append(sex);        sql_op.append("\"");    }else if(age!=""){        sql_op.append("age=");        sql_op.append(age);    }else if(tel!=""){        sql_op.append("tel=\"");        sql_op.append(tel);        sql_op.append("\"");    }else if(school!=""){        sql_op.append("school=\"");                                                     sql_op.append(school);        sql_op.append("\"");    }else if(id!=""){        sql_op.append("id=");        sql_op.append(id);    }else{                                                                              sql_op="select * from students";    }  //  std::cout<
<
"<
<<""<
<<"

id  name    sex   age   tel     school";                        std::cout<<"

"<
<<"

";        for(int i = 0; i < num_fields; i++)        {            printf("[%.*s] ", (int)lengths[i], row[i] ? row[i] : "NULL");        }        std::cout<<"

"<
<
<<""<
<<""<

htdocs/mysql_connect/Makefile:

ROOT_PATH=$(shell pwd)#shell是函数,它的参数是操作系统shell的命令,它和反引号的>功能是相同的,这就是说shell函数把执行操作系统命令后的输出作为函数返回值,这个函>数会生成一个shell程序来执行命令,所以要注意运行性能                                                                                #LINK=-I/home/chehlling/httpd/mysql_connect/mysql-clib/include\     -L/home/chehlling/httpd/mysql_connect/mysql-clib/lib -lmysqlclient -lpthread -lm -lrt -ldl sql_api:main.cpp sql_api.cpp    g++ -o $@ $^ `${ROOT_PATH}/mysql-clib/bin/mysql_config --cflags --libs ` -lstdc++  .PHONY:cleanclean:    rm -f sql_api .PHONY:outputoutput:sql_api    cp -f sql_api ../../output/htdocs/cgi_mysql/

htdocs/index.html:(大数运算操作)

              

hello chehlling

                

        
            data1: 
            data2: 
            +: 
            -: 
            *: 
            /: 
            %: 
                              
                

                
    

htdocs/cgi_bin/math_cgi.cpp:

#include "bigdata.h"#include 
#include 
                                                               #include 
#include 
#include 
#include 
#include 
#define _SIZE_ 2048 void get_args(std::vector
 &vec){    std::string method=getenv("METHOD");    std::string query_string;     std::string content_length;    char ch='a';    int index=0;    int ret=-1;    char buf[_SIZE_];    memset(buf,'\0',sizeof(buf));    if(method=="POST"){//POST        std::string content_length=getenv("CONTENT_LENGTH");        int len=atoi(content_length.c_str());        memset(buf,'\0',sizeof(buf));        while(len){//&& index
 &vec){    const char *op=vec[0].c_str();    BigData data1(vec[2].c_str());    BigData data2(vec[1].c_str());//  std::cout<
<<"     "<
<
"<
<<""<
<<"  ";    std::cout<<"

math_cgi     ";    std::cout<
<<"

"<
<<""<
<<""<
<
<
<
<<"="<
<
<<""<
<<""<
 vec;                                                   get_args(vec);//reverse store args    echo_result(vec);         return 0;}

htdocs/cgi_bin/bigadata.h:

#ifndef BIG_DATA_H#define BIG_DATA_H#include 
#include 
                                                             #include 
#include 
 #define UN_INIT 0xccccccccccccccccll#define MAX_INT64 0x7fffffffffffffffll#define MIN_INT64 0x8000000000000000ll typedef long long INT64; class BigData{public:    BigData(INT64 data = UN_INIT);    BigData(const char *pData);    BigData operator+(BigData& bigData);    BigData operator-(const BigData& bigData);    BigData operator*(const BigData& bigData);    BigData operator/(const BigData& bigData);    BigData operator%(const BigData& bigData);    //=======================================    bool operator<(const BigData& bigData);    bool operator>(const BigData& bigData);    bool operator==(const BigData& bigData);        std::ostream& operator<<(std::ostream& _cout);        friend std::ostream& operator<<(std::ostream& _cout, const BigData& bigData);    friend std::istream& operator>>(std::istream& _cin, BigData bigData);  private:    std::string Add(std::string left, std::string right);    std::string Sub(std::string left, std::string right);    std::string Mul(std::string left, std::string right);    std::string Div(std::string left, std::string right);    void INT64ToString();    bool IsINT64Owerflow()const;    bool IsLeftStrBig(char *pLeft, size_t LSize, char *pRight, size_t RSize);    char SubLoop(char *pLeft, size_t LSize, char *pRight, size_t RSize);    private:                                                                        long long m_llValue;    std::string m_strData;};  #endif

htdocs/cgi_bin/bigadata.cpp:

htdocs/cgi_bin/Makefile:

math_cgi:math_cgi.cpp bigdata.cpp    g++ -o $@ $^ -lstdc++                                                                                .PHONY:cleanclean:    rm -rf math_cgi   .PHONY:outputoutput:math_cgi    cp -f math_cgi ../../output/htdocs/cgi_bin/

httpd.c  httpd.h   main.c  这三个文件和上一篇一样

Makefile

ROOT_PATH=$(shell pwd)LDFLAGS=-lpthreadFLAGS=#-D_DEBUG_#-g CC=gccBIN=httpdSRC=$(shell ls *.c)OBJ=$(SRC:.c=.o)                                                                                CGI_BIN=htdocs/cgi_binMYSQL_CONNECT=htdocs/mysql_connect .PHONY:allall:$(BIN) cgi$(BIN):$(OBJ)    @echo "Linking [$^] to [$@]"    @$(CC) -o $@ $^  $(LDFLAGS)    @echo "Linking done..."%.o:%.c    @echo "Compling [$<] to [$@]"    @$(CC) -c $< -D_V2_ $(FLAGS)     @echo "Compling done..."      .PHONY:cgicgi:    @for name in `echo $(CGI_BIN)`;\    do\        cd $$name;\        make;\        cd -;\    done    @for name in `echo $(MYSQL_CONNECT)`;\    do\        cd $$name;\        make;\        cd -;\    done   .PHONY:cleanclean:    @rm -rf *.o $(BIN) output    @for name in `echo $(CGI_BIN)`;\    do\        cd $$name;\        make clean;\        cd -;\    done    @for name in `echo $(MYSQL_CONNECT)`;\    do\        cd $$name;\        make clean;\        cd -;\    done.PHONY:outputoutput:$(BIN) cgi    @mkdir -p output/log    @cp -rf htdocs output/    @mkdir -p output/htdocs/cgi_bin    @cp -f httpd output/    @cp -f start.sh output/    @cp -rf conf output/    @mkdir -p output/htdocs/cgi_mysql    @for name in `echo $(CGI_BIN)`;\                                                do\        cd $$name;\        make output;\        cd -;\    done    @for name in `echo $(MYSQL_CONNECT)`;\    do\        cd $$name;\        make output;\        cd -;\    done

start.sh 

#! /bin/bash ROOT_PATH=$(pwd)                                                                conf=$ROOT_PATH/conf/server.confser=$ROOT_PATH/httpd ip=$(grep -E "^IP:" $conf |awk -F: '{print $NF}')port=$(grep -E "^PORT:" $conf | awk -F: '{print $NF}')$ser $ip $port

运行结果:

插入:

查询:

删除:

更新:

发布之后output的目录下:

《完》