简体   繁体   中英

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. 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' 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"];
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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