简体   繁体   English

写入SQLite DB时出错

[英]Error writing to SQLite DB

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. 这是我第一次使用SQL,我以为我做对了所有事情,但是我不断收到错误消息,说我的表不存在。 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 所以我有一个名为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' 当我触发此操作时,它会关闭应用程序并Error while creating add statement. 'no such table: Users'给我错误Error while creating add statement. 'no such table: Users' 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 编辑:这就是我在AppDelegate文件中导入数据库的方式

- (NSString *) getDBPath {

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

I'm pretty sure you get this error because sqlite could not find your sqlite file, so it creates an empty database instead. 我很确定您会收到此错误,因为sqlite找不到您的sqlite文件,因此它创建了一个空数据库。 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. 从您的代码看来,您似乎是从documents目录中读取它的-但是,如果不将它复制到该目录中,您的db文件将无法到达那里。

Try adding the actual sqlite file to your bundle. 尝试将实际的sqlite文件添加到包中。 Then, use this code to reach the path of your sqlite file: 然后,使用此代码到达您的sqlite文件的路径:

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. 但是,您应该注意,如果要保存对该数据库的更改,并且我想这样做,则不要将文件复制到documents目录,因为捆绑的文件是只读的。 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. 在这种情况下,您将在目录路径中搜索sqlite文件,如果未找到(可能是首次使用app),则将其从包中复制到该目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM