目录
一、项目资源
Ubuntu系统:12.04
实验箱:DYV-1 型嵌入式车载多媒体实验箱
BOA服务器搭建:Ubuntu下boa服务器的配置与搭建 - Jason Xiang - 博客园 (cnblogs.com)
SQLite数据库搭建:sudo apt-get install sqlite3 或者 转载:Ubuntu下sqlite3的安装及使用 - Aldridge1 - 博客园 (cnblogs.com)
交叉编译工具链:arm-none-linux-gnueabi-gcc 【ARM-Linux开发】arm-none-Linux-gnueabi-gcc下载安装 - ZhangPYi - 博客园 (cnblogs.com)
环境搭建以上两篇足够;
二、项目目的要求
1、嵌入式web服务器Boa+嵌入式数据库SQLite
2、浏览器访问嵌入式web服务器;
3、嵌入式WEB服务器访问SQLite数据库
4、数据库可查询、删除、插入、修改
三、项目源码(直接上源码!!)
1、config.html(写的及其简单,可自行美化)
<!DOCTYPE html>
<html lang="zh-CN" id="dt">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>表格操作</title>
<link href="bootstrap-3.3.5-dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body style="text-align:center;">
<h1 style='color:rgb(246, 119, 119);font-size:50px;'>6666666</h1>
<h1 style='color:rgb(246, 119, 119);font-size:50px;'>嵌入式实训</h1>
<hr />
<h2>Students Add</h2>
<form action="cgi-bin/tianjia.cgi" method="get">
<label for="xuhao">序号</label>
<input type="text" name="xuhao" id="xuhao" />
<label for="xingming">姓名</label>
<input type="text" id="xingming" name="xingming" />
<label for="xuehao">学号</label>
<input type="text" name="xuehao" id="xuehao" />
<input type="submit" value="添加">
</form>
<hr />
<h2>Students Delete</h2>
<form action="cgi-bin/shanchu.cgi" method="get">
<label for="xuehao">学号</label>
<input type="text" name="xuhao" id="xuhao" />
<input type="submit" value="删除">
</form>
<hr />
<h2>Information Changes</h2>
<form action="cgi-bin/xiugai.cgi" method="get">
<label for="xuhao">序号</label>
<input type="text" name="xuhao" />
<label for="xingming">姓名</label>
<input type="text" name="xingming" name="xingming" />
<label for="xuehao">学号</label>
<input type="text" name="xuehao" id="xuhao"/>
<input type="submit" value="修改">
</form>
<hr />
<h2>Search Information</h2>
<form action="cgi-bin/chakan.cgi" method="get">
<label for="xuhao">序号</label>
<input type="text" name="xuhao" />
<input type="submit" value="查看序号">
</body>
</html>
2、查看学生信息页面
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "cgic.h"
#include "sqlite3.h"
#define SQL_SIZE 256
int cgiMain(int argc, char **argv)
{
char xuhao[20];
cgiHeaderContentType("text/html\n\n");
//获取表单中的name的值,存入name缓冲区
fprintf(cgiOut, "<HTML><HEAD>\n");
fprintf(cgiOut, "<TITLE>My CGI</TITLE></HEAD>\n");
fprintf(cgiOut, "<BODY style='text-align:center;'>");
//获取数据
cgiFormString("xuhao", xuhao, 241);
//打印数据
//-----------
sqlite3 *db;
int result;
result = sqlite3_open("text.db", &db);
if(result != SQLITE_OK)
{
printf("Fail to sqlite3_open text.db: %s.\n", sqlite3_errmsg(db));
exit(-1);
}
int nrow = 0, ncolumn = 0;
char **azResult = NULL;
char *zErrMsg = 0;
int rc = 0;
char *errmsg;
char sql[SQL_SIZE];
bzero(sql, SQL_SIZE);
sprintf(sql,"select * from people where xuhao='%s';",xuhao);
rc = sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
if (rc != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_close(db);
exit(-1);
}
if(*azResult != NULL)
{
printf("<h3>Query content:</h3>");
printf("<table style='margin:auto;' border='1' width='500px' height='300px' cellspacing='0'");
printf("<thead>");
printf("<tr>");
printf("<th>");
printf( "%s", azResult[0] );
printf("</th>");
printf("<th>");
printf( "%s", azResult[1] );
printf("</th>");
printf("<th>");
printf( "%s", azResult[2] );
printf("</th>");
printf("</tr>");
printf("</thead>");
printf("<tbody>");
printf("<th>");
printf( "%s", azResult[3] );
printf("</th>");
printf("<th>");
printf( "%s", azResult[4] );
printf("</th>");
printf("<th>");
printf( "%s", azResult[5] );
printf("</th>");
printf("</tbody>");
printf("</table>");
//int i;
//for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
// printf( "<h3>azResult[%d] = %s \n</h3>", i , azResult[i] );
}
printf("<a href='../config.html'>");
printf("<button>fanhui</button>");
printf("</a>");
fprintf(cgiOut, "</BODY>\n");
fprintf(cgiOut, "</HTML>\n");
return 0;
}
3、学生信息删除
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "cgic.h"
#include "sqlite3.h"
#define SQL_SIZE 256
int cgiMain(int argc, char **argv)
{
char xuhao[20];
char xingming[20];
char xuehao[20];
cgiHeaderContentType("text/html");
//获取表单中的name的值
fprintf(cgiOut, "<HTML><HEAD>\n");
fprintf(cgiOut, "<TITLE>My CGI</TITLE></HEAD>\n");
fprintf(cgiOut, "<BODY style='text-align:center;'>");
//获取数据
cgiFormString("xuhao", xuhao, 241);
//打印数据
fprintf(cgiOut, "<h1>Delete object=%s</h1>",xuhao);
//-----------
sqlite3 *db;
int result;
result = sqlite3_open("text.db", &db);
if(result != SQLITE_OK)
{
printf("Fail to sqlite3_open text.db: %s.\n", sqlite3_errmsg(db));
exit(-1);
}
char *errmsg;
char sql[SQL_SIZE];
bzero(sql, SQL_SIZE);
sprintf(sql,"DELETE FROM people WHERE xuhao = %s;",xuhao);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != 0)
{
printf("Fail to exec sql: %s.\n\n", errmsg);
printf("\n%s\n",sql);
exit(-1);
}else{
printf("<a>___________________</a>");
printf("<h3>successfully delete!!!</h3>");
}
result = sqlite3_close(db);
if(result != 0)
{
fprintf(stderr, "Fail to sqlite3_open person.db : %s.\n", sqlite3_errmsg(db));
exit(-1);
}
printf("<a href='../config.html'>");
printf("<button>fanhui</button>");
printf("</a>");
fprintf(cgiOut, "</BODY>\n");
fprintf(cgiOut, "</HTML>\n");
return 0;
}
4、学生信息修改
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "cgic.h"
#include "sqlite3.h"
#define SQL_SIZE 256
int cgiMain(int argc, char **argv)
{
char xuhao[20];
char xingming[20];
char xuehao[20];
cgiHeaderContentType("text/html");
//获取表单中的name的值,存入name缓冲区
fprintf(cgiOut, "<HTML><HEAD>\n");
fprintf(cgiOut, "<TITLE>My CGI</TITLE></HEAD>\n");
fprintf(cgiOut, "<BODY style='text-align:center;'>");
//获取数据
cgiFormString("xuhao", xuhao, 241);
cgiFormString("xingming", xingming, 241);
cgiFormString("xuehao", xuehao, 241);
//打印数据
fprintf(cgiOut, "<H1>Modify object:</H1>");
fprintf(cgiOut, "<a>id=%s</a> ",xuhao);
fprintf(cgiOut, "<a>username=%s</a> ",xingming);
fprintf(cgiOut, "<a>student number=%s</a> ",xuehao);
//-----------
sqlite3 *db;
int result;
result = sqlite3_open("text.db", &db);
if(result != SQLITE_OK)
{
printf("Fail to sqlite3_open text.db: %s.\n", sqlite3_errmsg(db));
exit(-1);
}
char *errmsg;
char sql[SQL_SIZE];
bzero(sql, SQL_SIZE);
sprintf(sql,"UPDATE people SET xingming='%s',xuehao='%s' WHERE xuhao = '%s';",xingming,xuehao,xuhao);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != 0)
{
printf("Fail to exec sql: %s.\n\n", errmsg);
printf("\n%s\n",sql);
exit(-1);
}else{
printf("<h1>modify successfully!!!</h1>");
}
result = sqlite3_close(db);
if(result != 0)
{
fprintf(stderr, "Fail to sqlite3_open person.db : %s.\n", sqlite3_errmsg(db));
exit(-1);
}
printf("<a href='../config.html'>");
printf("<button>fanhui</button>");
printf("</a>");
fprintf(cgiOut, "</BODY>\n");
fprintf(cgiOut, "</HTML>\n");
return 0;
}
5、添加学生信息
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "cgic.h"
#include "sqlite3.h"
#define SQL_SIZE 256
int cgiMain(int argc, char **argv)
{
char xuhao[20];
char xingming[20];
char xuehao[20];
cgiHeaderContentType("text/html");
//获取表单中的name的值,存入name缓冲区
fprintf(cgiOut, "<HTML><HEAD>\n");
fprintf(cgiOut, "<TITLE>My CGI</TITLE></HEAD>\n");
fprintf(cgiOut, "<BODY style='text-align:center;'>");
//获取数据
cgiFormString("xuhao", xuhao, 241);
cgiFormString("xingming", xingming, 241);
cgiFormString("xuehao", xuehao, 241);
//打印数据
fprintf(cgiOut, "<H1>Add object:</H1>");
fprintf(cgiOut, "<a>id=%s</a> ",xuhao);
fprintf(cgiOut, "<a>username=%s</a> ",xingming);
fprintf(cgiOut, "<a>student number=%s</a> ",xuehao);
//-----------
sqlite3 *db;
int result;
result = sqlite3_open("text.db", &db);
if(result != SQLITE_OK)
{
printf("Fail to sqlite3_open text.db: %s.\n", sqlite3_errmsg(db));
exit(-1);
}
char *errmsg;
char sql[SQL_SIZE];
bzero(sql, SQL_SIZE);
sprintf(sql,"insert into people values('%s','%s','%s');",xuhao,xingming,xuehao);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != 0)
{
printf("Fail to exec sql: %s.\n\n", errmsg);
printf("\n%s\n",sql);
exit(-1);
}else{
printf("<h1>successfully added!!!!</h1>");
}
result = sqlite3_close(db);
if(result != 0)
{
fprintf(stderr, "Fail to sqlite3_open person.db : %s.\n", sqlite3_errmsg(db));
exit(-1);
}
printf("<a href='../config.html'>");
printf("<button>fanhui</button>");
printf("</a>");
fprintf(cgiOut, "</BODY>\n");
fprintf(cgiOut, "</HTML>\n");
return 0;
}
交叉编译语句:arm-none-linux-gnueabi-gcc -o chakan.cgi chakan.c cgic.c -lsqlite3
编译需要用到cgic.c(自己网上找找)以及sqlite线程
运行结果:
四、总结:
项目本身难度不大,主要弄清楚boa服务器如何搭建、CGI是个啥、SQLIte数据库的使用及API。
我遇到的困难就是交叉编译工具链sqlite动态库自己下载的和开发板上的不一样,程序放上去运行不了,没办法只能把开发板的所有库移植进自己的工具链中。