c - 链接 sqlite C 时出错

标签 c sqlite

我正在尝试将 sqlite 与 C 结合使用。在 Python 中执行此操作非常容易,但是当我尝试使用 C 执行相同操作时,出现错误。

我发现了这个:

libsqlite in simulator and iOS compiling

但我只是使用文本编辑器,所以我不能使用 XCode(好吧,我可以,但我没有)。当我这样做时

#include <sqlite.h> // or "sqlite.h"

我收到这样的错误:

Undefined symbols for architecture i386:
  "_sqlite3_open", referenced from:
      _main in ccb8OLfK.o
  "_sqlite3_exec", referenced from:
      _main in ccb8OLfK.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status

如果您想查看我的代码,这里是全部代码:

#include <stdio.h>
#include <stdlib.h>

#include <sqlite3.h>

#include <string.h>

int main(void) {
    /* variables */
    // create value for storing return codes
    int retval;

    // the number of queries to be handled
    int qnum;

    // size of each query
    int qsize;

    // pointer
    int ind;

    // statement for fetching tables
    sqlite3_stmt *stmt;

    // pointer to sqlite3
    sqlite3 *db;

    // db name
    char *home = getenv("HOME");
    char *dbPath = strcat(home, "/Desktop/test.db");

    /* connect and write */
    // Try to connect to database (if fails, returns null)
    retval = sqlite3_open(dbPath, &db);
    if (retval) {
        printf("\x1B[31;1mConnection failed.\x1B[0m\n");
        exit(1);
    } else {
        printf("\x1B[32;1mConnected to Database.\x1B[0m\n");
    }

    // create table
    char q[100] = "create table if not exists test (user text not null, psswd text not null);";
    retval = sqlite3_exec(db, q, 0, 0, 0);
}

到目前为止,它有几个未使用的变量,并且只创建了一个表(如果我能做到这一点),但它应该编译并运行良好。

我是 C 新手(Java --> Python --> C)。我该如何让它发挥作用?

这似乎是一个非常大的痛苦,所以我可以下载 sqlite3 的头文件(但我不知道从哪里)并将其包含在引号中吗?我不知道如何链接文件,我只是希望这个初步测试能够工作。

最佳答案

您还必须下载 sqlite3 源代码并将其与您的应用程序一起编译。标题仅提供有关功能的信息; sqlite3源代码(建议“合并”)提供了实际的实现,这正是链接器想要的。

关于c - 链接 sqlite C 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25839704/

相关文章:

c - 为什么我的查找 21 以内阶乘的 C 程序从 13 的阶乘开始产生无效结果?

java - 没有这样的表问题

database - Sqlite中B树的程度是多少?

c - While 循环中表达式的双括号

C:如何递归地逐位传输字节?

c - 从套接字 recv() 到 uint16_t 的 memcpy 上的段错误

java - Sqlite + Jodd 正在用于插入数据、选择数据,但它不适用于使用 dboom 更新数据...我如何使用 dboom 实现..?

ios - 在 iOS App 中将 SQLite 数据库从 Titanium 迁移到 Ionic

iphone - 如何在iPhone map 上显示从当​​前位置到目的地位置的方向

c - 访问嵌套结构成员的多种方式