尝试分页查询(第一次实验失败,注释部分修改,待解决)
This commit is contained in:
parent
9ddea88f8c
commit
f9fe11d9dc
|
@ -8,19 +8,23 @@
|
|||
|
||||
// sqlite3的回调函数
|
||||
// sqlite 每查到一条记录,就调用一次这个回调
|
||||
int sql_search_callback(void* data, int argc, char** argv, char** azColName) {
|
||||
cJSON* result_array = (cJSON*)data;
|
||||
cJSON* item = cJSON_CreateObject();
|
||||
int sql_search_callback(void *data, int argc, char **argv, char **azColName)
|
||||
{
|
||||
cJSON *result_array = (cJSON *)data;
|
||||
cJSON *item = cJSON_CreateObject();
|
||||
|
||||
// 遍历每一列,并将数据添加到 JSON 对象中
|
||||
for (int i = 0; i < argc; i++) {
|
||||
for (int i = 0; i < argc; i++)
|
||||
{
|
||||
cJSON_AddStringToObject(item, azColName[i], argv[i] ? argv[i] : "");
|
||||
}
|
||||
|
||||
int isDuplicate = 0;
|
||||
cJSON* existingItem = result_array->child;
|
||||
while (existingItem != NULL) {
|
||||
if (cJSON_Compare(existingItem, item, 1)) {
|
||||
cJSON *existingItem = result_array->child;
|
||||
while (existingItem != NULL)
|
||||
{
|
||||
if (cJSON_Compare(existingItem, item, 1))
|
||||
{
|
||||
isDuplicate = 1;
|
||||
break;
|
||||
}
|
||||
|
@ -28,34 +32,46 @@ int sql_search_callback(void* data, int argc, char** argv, char** azColName) {
|
|||
}
|
||||
|
||||
// 如果不是重复的结果,则添加到 JSON 数组中
|
||||
if (!isDuplicate) {
|
||||
if (!isDuplicate)
|
||||
{
|
||||
cJSON_AddItemToArray(result_array, item);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int main(int argc, char const* argv[]) {
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
// 连接到 SQLite 数据库
|
||||
sqlite3* db; // 声明 sqlite3 类型变量,即声明一个数据库对象
|
||||
char* zErrMsg = 0; // 用于存储错误信息
|
||||
sqlite3 *db; // 声明 sqlite3 类型变量,即声明一个数据库对象
|
||||
char *zErrMsg = 0; // 用于存储错误信息
|
||||
int rc; // 用于存储函数返回值
|
||||
|
||||
rc = sqlite3_open(
|
||||
"../sql_base/green_house.db",
|
||||
&db); // 打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
|
||||
if (rc) {
|
||||
if (rc)
|
||||
{
|
||||
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_close(db);
|
||||
return 1;
|
||||
}
|
||||
fprintf(stderr, "ok Opened database successfully\n");
|
||||
|
||||
// 解析分页参数
|
||||
int page = 1; // 默认第一页
|
||||
int limit = 10; // 默认每页10条
|
||||
if (getenv("QUERY_STRING"))
|
||||
{
|
||||
sscanf(getenv("QUERY_STRING"), "page=%d&limit=%d", &page, &limit);
|
||||
}
|
||||
|
||||
// 构建查询语句
|
||||
char query_sql[128] = "";
|
||||
sprintf(
|
||||
query_sql,
|
||||
"select temperature, humidity , th_date_time from temp_hum_info ;"); // 构建查询
|
||||
"select temperature, humidity , th_date_time from temp_hum_info;"); // 构建查询
|
||||
// "select temperature, humidity , th_date_time from temp_hum_info order by thid desc limit %d offset %d;", limit, (page - 1) * limit); // 构建查询
|
||||
|
||||
cJSON* result_array = cJSON_CreateArray();
|
||||
cJSON *result_array = cJSON_CreateArray();
|
||||
|
||||
// printf("query_sql is %s\n", query_sql);
|
||||
|
||||
|
@ -64,7 +80,8 @@ int main(int argc, char const* argv[]) {
|
|||
&zErrMsg); // 执行查询语句
|
||||
// rc = sqlite3_exec(db, query_sql, sql_search_callback, 0, &zErrMsg); //
|
||||
// 执行查询语句
|
||||
if (rc != SQLITE_OK) {
|
||||
if (rc != SQLITE_OK)
|
||||
{
|
||||
fprintf(stderr, "SQL error: %s\n", zErrMsg);
|
||||
sqlite3_free(zErrMsg);
|
||||
// cJSON_Delete(json_buf);
|
||||
|
@ -72,7 +89,7 @@ int main(int argc, char const* argv[]) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
char* json_str = cJSON_Print(result_array);
|
||||
char *json_str = cJSON_Print(result_array);
|
||||
|
||||
printf("Content-Type: application/json;charset=utf-8\r\n");
|
||||
printf("\r\n");
|
||||
|
|
|
@ -66,11 +66,17 @@
|
|||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<!-- 下一页,上一页按钮
|
||||
<div class="page">
|
||||
<button onclick="prevPage()" id="prevPage">上一页</button>
|
||||
<button onclick="nextPage()" id="nextPage">下一页</button>
|
||||
</div> -->
|
||||
|
||||
<script>
|
||||
function loadBlackData() {
|
||||
var blackData;
|
||||
fetch("../cgi-bin/green_house_info_show.cgi")
|
||||
// fetch("../cgi-bin/green_house_info_show.cgi?page="+currentPage+"&limit=10")
|
||||
.then(resp => resp.json())
|
||||
.then(data => {
|
||||
blackData = data;
|
||||
|
@ -94,6 +100,17 @@
|
|||
window.onload = function () {
|
||||
loadBlackData();
|
||||
}
|
||||
|
||||
// // HTML 页面中的按钮点击事件处理程序
|
||||
// function nextPage() {
|
||||
// currentPage++; // 增加当前页码
|
||||
// loadBlackData(); // 重新加载数据
|
||||
// }
|
||||
|
||||
// function prevPage() {
|
||||
// currentPage--; // 减少当前页码
|
||||
// loadBlackData(); // 重新加载数据
|
||||
// }
|
||||
</script>
|
||||
</body>
|
||||
|
||||
|
|
Loading…
Reference in New Issue