#include <iostream>
#include <fstream> //读写文件 c++标准库
#include <string> //字符串类 c++标准库
#include <sstream> //字符串流 c++标准库
#include <assert.h>
#include "sqlite3.h" //sqlite3头文件
#include <cgicc/CgiDefs.h> //cgicc库头文件
#include <cgicc/Cgicc.h>
#include <cgicc/HTTPHTMLHeader.h>
#include <cgicc/HTMLClasses.h>
using namespace cgicc;
using namespace std;
#define DEBUG false
/********************* 测试模块 ********************/
void testMode()
{
Cgicc formData;
//获取文本框内容
const vector<cgicc::FormEntry>& form_entries = formData.getElements();
cout << "form_entries.size()=" << form_entries.size() << endl;
for (vector<cgicc::FormEntry>::size_type i=0; i<form_entries.size(); ++i)
{
const cgicc::FormEntry& form_entry = form_entries[i];
cout << "getName=" << form_entry.getName() << "; getValue=" << form_entry.getValue() << endl;
}
}
/*****************WG67-Key-In*****************/
static int callback(void *data, int argc, char **argv, char **azColName)
{
#if (DEBUG == true)
cout << "callback: argc = " << argc
<< "; azColName[0] = " << azColName[0]
<< "; argv[0] = " << argv[0]
<< endl;
#endif
int j = 0;
for(j = 0; j < argc; j++){
cout << argv[j];
if(j < argc - 1){
cout << ",";
}
if(j == argc - 1)
cout << ";";
}
cout << endl;
return 0;
}
bool SelectMsgFromDB()
{
bool rtn = true;
sqlite3 *dataDB;
string callback_data = "Callback function called";
char *zErrMsg = 0;
string dataBaseFilePath = "/opt/diantai.db";
int select_warn_res = 0;
string sql_warn_select;
/*
* Open
* Check File Exist
* If not Exist it will creat new database
*/
int openDBStatus = sqlite3_open(dataBaseFilePath.c_str(), &dataDB);
if(openDBStatus)
{
#if (DEBUG == true)
cout << "Can't open database: " << sqlite3_errmsg(dataDB) << endl;
#endif
goto exit_error;
rtn = false;
}
/* Create SQL statement */
sql_warn_select = "Select ppt_id,client_sip_host,client_req_type FROM radio_session;";
/* Execute SQL statement */
cout << "fid: 000.000" << ";" << endl;
select_warn_res = sqlite3_exec(dataDB, sql_warn_select.c_str(), callback, (void*)callback_data.c_str(), &zErrMsg);
if(select_warn_res != SQLITE_OK)
{
#if (DEBUG == true)
cout << "SQL(select warn msg) error: " << zErrMsg << endl;
#endif
sqlite3_free(zErrMsg);
rtn = false;
}
exit_error:
sqlite3_close(dataDB);
return rtn;
}
/********************* main函数 *********************/
int main(int argc, char *argv[])
{
cout << "Content-type:text/html;charset=utf-8\r\n\r\n";
#if (DEBUG == true)
if(argc > 1){
for(int i = 0; i < argc; i++)
cout << "main: argv[" << i << "]=" << argv[i] << endl;
}
testMode();
#endif
SelectMsgFromDB();
return 0;
}
web通过 submit 提交form形式执行cgi程序。
sqlite3操作,参考:SQLite – C/C++ | 菜鸟教程
cgicc的安装和使用,参考:Cgicc搭建交叉编译环境(移植到arm)-CSDN博客