回调函数v1
This commit is contained in:
parent
9d07810e70
commit
2da456316c
|
@ -7,7 +7,8 @@
|
|||
// sqlite 每查到一条记录,就调用一次这个回调
|
||||
int sql_search_callback(void *NotUsed, int argc, char **argv, char **azColName);
|
||||
|
||||
int main(int argc, char const* argv[]) {
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
// 连接到 SQLite 数据库
|
||||
sqlite3 *db; // 声明 sqlite3 类型变量,即声明一个数据库对象
|
||||
char *zErrMsg = 0; // 用于存储错误信息
|
||||
|
@ -16,7 +17,8 @@ int main(int argc, char const* argv[]) {
|
|||
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;
|
||||
|
@ -29,7 +31,8 @@ int main(int argc, char const* argv[]) {
|
|||
|
||||
// 解析 JSON 数据
|
||||
cJSON *json_buf = cJSON_Parse(request_data); // 解析 JSON 数据
|
||||
if (json_buf == NULL) {
|
||||
if (json_buf == NULL)
|
||||
{
|
||||
fprintf(stderr, "Error parsing JSON data\n");
|
||||
sqlite3_close(db);
|
||||
return 1;
|
||||
|
@ -40,7 +43,8 @@ int main(int argc, char const* argv[]) {
|
|||
cJSON *password = cJSON_GetObjectItemCaseSensitive(json_buf, "password");
|
||||
|
||||
if (user == NULL || password == NULL || !cJSON_IsString(user) ||
|
||||
!cJSON_IsString(password)) { // 是否是非法输入
|
||||
!cJSON_IsString(password))
|
||||
{ // 是否是非法输入
|
||||
fprintf(stderr,
|
||||
"Error getting username and/or password from JSON data\n");
|
||||
cJSON_Delete(json_buf);
|
||||
|
@ -58,14 +62,16 @@ int main(int argc, char const* argv[]) {
|
|||
// 构建查询语句
|
||||
char query_sql[128] = "";
|
||||
sprintf(query_sql,
|
||||
"select * from login_users where username='%s' and password='%s'",
|
||||
"select * from login_users where username='%s' and password='%s';",
|
||||
username_from_frontend, password_from_frontend); // 构建查询语句
|
||||
|
||||
printf("query_sql is %s\n", query_sql);
|
||||
|
||||
// 执行查询语句
|
||||
// rc = sqlite3_exec(db, query_sql, 0, 0, &zErrMsg); // 执行查询语句
|
||||
rc = sqlite3_exec(db, query_sql, sql_search_callback, 0,
|
||||
&zErrMsg); // 执行查询语句
|
||||
if (rc != SQLITE_OK) {
|
||||
rc = sqlite3_exec(db, query_sql, sql_search_callback, 0, &zErrMsg); // 执行查询语句
|
||||
if (rc != SQLITE_OK)
|
||||
{
|
||||
fprintf(stderr, "SQL error: %s\n", zErrMsg);
|
||||
sqlite3_free(zErrMsg);
|
||||
cJSON_Delete(json_buf);
|
||||
|
@ -81,7 +87,7 @@ int main(int argc, char const* argv[]) {
|
|||
|
||||
// 打印 JSON 数据
|
||||
char *result_json_str = cJSON_Print(result_json);
|
||||
printf("%s\n", result_json_str);
|
||||
// printf("%s\n", result_json_str);
|
||||
|
||||
// 释放资源
|
||||
cJSON_Delete(json_buf);
|
||||
|
@ -94,4 +100,13 @@ int main(int argc, char const* argv[]) {
|
|||
int sql_search_callback(void *NotUsed,
|
||||
int argc,
|
||||
char **argv,
|
||||
char** azColName) {}
|
||||
char **azColName)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < argc; i++)
|
||||
{
|
||||
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue