写入 SQLite DB 时出错

发布于 2024-11-06 10:52:21 字数 1491 浏览 0 评论 0原文

这是我第一次使用 SQL,我以为我做的一切都是正确的,但我不断收到我的表不存在的错误。要创建表,我做了:

CREATE TABLE "Users" ("UserID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "UserName" VARCHAR, "Password" VARCHAR)

所以我有一个名为 Users 的表

要写入表,我调用此:

- (void) addUser {

if(addStmt == nil) {
    const char *sql = "insert into Users(UserName, Password) values (?, ?)";
    if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}

sqlite3_bind_text(addStmt, 1, [userName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [password UTF8String], -1, SQLITE_TRANSIENT);

if(SQLITE_DONE != sqlite3_step(addStmt))
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
    //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
    userID = sqlite3_last_insert_rowid(database);

//Reset the add statement.
sqlite3_reset(addStmt);
}

当我触发此操作时,它会关闭应用程序并给出错误 创建添加语句时出错。 '没有这样的表:用户'

我花了几天时间才走到这一步,我不知道从这里到哪里去。我可以做什么来解决这个问题?

编辑: 这就是我在 AppDelegate 文件中导入数据库的方式

- (NSString *) getDBPath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"UserDatabase.sqlite"];
}

This is my first time using SQL, and I thought I did everything right, but I keep getting the error that my table doesn't exist. To create the table I did:

CREATE TABLE "Users" ("UserID" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "UserName" VARCHAR, "Password" VARCHAR)

So I have a table named Users

To write to the table I'm calling this:

- (void) addUser {

if(addStmt == nil) {
    const char *sql = "insert into Users(UserName, Password) values (?, ?)";
    if(sqlite3_prepare_v2(database, sql, -1, &addStmt, NULL) != SQLITE_OK)
        NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}

sqlite3_bind_text(addStmt, 1, [userName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(addStmt, 2, [password UTF8String], -1, SQLITE_TRANSIENT);

if(SQLITE_DONE != sqlite3_step(addStmt))
    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
else
    //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
    userID = sqlite3_last_insert_rowid(database);

//Reset the add statement.
sqlite3_reset(addStmt);
}

When I trigger this, it closes the app and gives me the error
Error while creating add statement. 'no such table: Users'

It took me days to get this far, and I have no clue where to take it from here. What can I do to fix this?

Edit:
This is how I'm importing my DB in my AppDelegate file

- (NSString *) getDBPath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"UserDatabase.sqlite"];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦与时光遇 2024-11-13 10:52:21

我很确定您会收到此错误,因为 sqlite 找不到您的 sqlite 文件,因此它会创建一个空数据库。在空数据库中,它显然找不到您的表,因此会出现错误。

因此,正确访问数据库文件应该是您的重点。从您的代码看来,您似乎是从文档目录中读取它的 - 但如果您不将其复制到该目录,则您的数据库文件将无法到达那里。

尝试将实际的 sqlite 文件添加到您的包中。然后,使用此代码到达 sqlite 文件的路径:

databasePath = [[NSBundle mainBundle] pathForResource:@"UserDatabase" ofType:@"sqlite"];

我相信这会解决您的问题。但是您应该注意,如果您想保存对该数据库的更改(我认为您愿意),您不会将该文件复制到文档目录,因为捆绑文件是只读的。在这种情况下,您将在目录路径中搜索 sqlite 文件,如果找不到(可能是第一次使用应用程序) - 将其从捆绑包复制到该目录。

I'm pretty sure you get this error because sqlite could not find your sqlite file, so it creates an empty database instead. And in an empty database it would obviously not find your table, hence the error.

So reaching the database file correctly should be your focus. From you code it seems your reading it from the documents directory - but there's no way your db file would get there if you didn't copy it to that directory.

Try adding the actual sqlite file to your bundle. Then, use this code to reach the path of your sqlite file:

databasePath = [[NSBundle mainBundle] pathForResource:@"UserDatabase" ofType:@"sqlite"];

And I believe this would solve your problem. However you should note that if you want to save changes to that db, and I think you want to, you would not to copy the file to the documents directory, because bundled file are read-only. In such case you would search for the sqlite file in the directory path, and if not found (probably first use of app) - copy it to that directory from the bundle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文