· 8 years ago · Aug 26, 2018, 10:36 AM
1Xcode: UPDATE statement SQLite won't work for me
2NSString *docsDir;
3NSArray *dirPaths;
4
5// Get the documents directory
6dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
7
8docsDir = [dirPaths objectAtIndex:0];
9
10// Build the path to the database file
11databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"contacts.db"]];
12
13NSFileManager *filemgr = [NSFileManager defaultManager];
14
15if ([filemgr fileExistsAtPath: databasePath ] == NO)
16{
17 const char *dbpath = [databasePath UTF8String];
18 if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
19 {
20 char *errMsg;
21 const char *sql_stmt = "CREATE TABLE IF NOT EXISTS CONTACTS(ID INTEGER PRIMARY KEY AUTOINCREMENT, LOGIN TEXT COLLATE NOCASE, PASSWORD TEXT, LOGGED INTEGER, SECRETQUESTION TEXT, ANSWER TEXT)";
22
23 if (sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
24 {
25 Status.text = @"Failed to create table";
26 }
27 sqlite3_close(contactDB);
28 } else {
29 Status.text = @"Failed to open/create database";
30 }
31}
32
33sqlite3_stmt *statement;
34
35const char *dbpath = [databasePath UTF8String];
36
37if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
38{
39 NSString *updateSQL = [NSString stringWithFormat: @"UPDATE CONTACTS SET LOGGED = '1' WHERE LOGIN = ("%@")", Login.text];
40 const char *update_stmt = [updateSQL UTF8String];
41 sqlite3_prepare_v2(contactDB, update_stmt, -1, &statement, NULL);
42
43 sqlite3_finalize(statement);
44}
45sqlite3_close(contactDB);