EXC_BAD_ACCESS (code=1,)c语言sqlite3

问题描述 投票:0回答:1

我正在尝试使用 sqlite3 和 c 创建一个 cgi 可执行文件。但我收到错误 EXC_BAD_ACCESS 并且我不知道问题是什么。我的代码:

main.c

#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include "sqlite.h"

int callback(void *, int, char **, char **);

int main() {

    printf("Content-Type: text/html\n\n");

    sqlite_container *ptr = get_sql();

    char *sql_query = "DROP TABLE IF EXISTS Cars;"
                "CREATE TABLE Cars(Id INT, Name TEXT, Price INT);"
                "INSERT INTO Cars VALUES(1, 'Audi', 52642);"
                "INSERT INTO Cars VALUES(2, 'Mercedes', 57127);"
                "INSERT INTO Cars VALUES(3, 'Skoda', 9000);"
                "INSERT INTO Cars VALUES(4, 'Volvo', 29000);"
                "INSERT INTO Cars VALUES(5, 'Bentley', 350000);"
                "INSERT INTO Cars VALUES(6, 'Citroen', 21000);"
                "INSERT INTO Cars VALUES(7, 'Hummer', 41400);"
                "INSERT INTO Cars VALUES(8, 'Volkswagen', 21600);";

    int rc = sqlite3_exec(ptr->db, sql_query, 0, 0, 0);

    if (rc != SQLITE_OK) {

        fprintf(stderr, "Failed to fetch data: %s\n", sqlite3_errmsg(ptr->db));
        sqlite3_close(ptr->db);

        return 1;
    }

    ptr = "SELECT * FROM Cars";

    rc = sqlite3_exec(ptr->db, sql_query, callback, 0, 0);

    if (rc != SQLITE_OK ) {

        fprintf(stderr, "Failed to select data\n");
        fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(ptr->db));

        sqlite3_close(ptr->db);

        return 1;
    }

    sqlite3_close(ptr->db);

    printf("%s\n", sqlite3_libversion());

    return 0;
}

int callback(void *NotUsed, int argc, char **argv,
             char **azColName) {

    NotUsed = 0;

    for (int i = 0; i < argc; i++) {

        printf("<pre>%s = %s</pre>\n", azColName[i], argv[i] ? argv[i] : "NULL");
    }

    printf("\n");

    return 0;
}

sqlite.h

#pragma once
#include <sqlite3.h>

typedef struct {
    struct sqlite3 *db;
    int status;
} sqlite_container;

sqlite_container *get_sql();

sqlite.c

#include "sqlite.h"
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
#include "config.h"

sqlite_container* get_sql() {
    sqlite_container* container = malloc(sizeof(struct sqlite_container *));
    container->db = NULL;
    container->status = 0;

    int rc = sqlite3_open(DATABASEFILE, &container->db);
    container->status = rc;

    if (rc != SQLITE_OK) {
        fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(container->db));
        sqlite3_close(container->db);
    }
    return container;
}

配置.h

#pragma once

#define DATABASEFILE "test.sql"

调试器在 main.c 第 37 行停止

rc = sqlite3_exec(ptr->db, sql_query, callback, 0, 0);

错误例如

EXC_BAD_ACCESS (code=1, address=0x2a205443454c45c4)
但我在变量概述中找不到地址。我使用 clion 作为编辑器。

c sqlite
1个回答
0
投票

我发现问题了。在 main.c 中我有以下内容:

    ptr = "SELECT * FROM Cars";

    rc = sqlite3_exec(ptr->db, sql_query, callback, 0, 0);

我将 ptr 从结构重写为字符串。当我将 ptr 更改为另一个变量时,程序再次运行。

注意:我已经尝试了评论中的建议,然后发现了错误。感谢您的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.