基于ID

时间:2016-04-12 22:50:36

标签: objective-c sqlite sql-update

尝试在不使用包装器的情况下学习Sqlite并管理大部分内容,但我确实陷入了UPDATE查询

我正在尝试根据作为主键的_ID号更新一列中的sting并且是唯一的。

我尝试了各种各样的谷歌代码。这个说它已经有效,但当我检查列没有更新时

这是代码

            NSDateFormatter *formatter;
            NSString        *dateString;
            formatter = [[NSDateFormatter alloc] init];
            [formatter setDateFormat:@"dd-MM-yyyy HH:mm"];
            dateString = [formatter stringFromDate:[NSDate date]];
            NSString *IDCODE = code.stringValue;
           NSInteger b = [IDCODE integerValue];
            sqlite3 *contactDB; //Declare a pointer to sqlite database structure
            const char *dbpath = [dbPath UTF8String]; // Convert NSString to UTF-8
            if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
            {
                //Database opened successfully
                NSString *databaseName = @"vistorlog.db";
                // Get the path to the documents directory and append the databaseName
                NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *documentsDir = [documentPaths objectAtIndex:0];
                NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
                const char *dbPath=[databasePath UTF8String];
                if (sqlite3_open(dbPath, &myDB)==SQLITE_OK) {
                    NSLog(@"database Opened");
                    const char* updateQuery="update LOG set TIMEOUT='22/03/19' where _ID=1";
                    sqlite3_stmt *stmt;
                    if (sqlite3_prepare_v2(myDB, updateQuery, -1, &stmt, NULL)==SQLITE_OK) {
                        NSLog(@"Query Executed");
                    }else{
                         NSLog(@"Query NOT Executed");
                                               }
                }
                sqlite3_close(myDB);
                }
                else {
                //Failed to open database
            }

打开DB vistorlog.db ok。有一个名为LOG的表,并且有一个名为TIMEOUT的列,我想要更新字符串,并且有一个名为_ID的列,这是我基于查询但它不会更新

最终我想让更新语句使用字符串变量dateString作为要更新的字符串,使用b作为_ID的整数变量

我出错的任何想法?

任何帮助表示赞赏

标记

修改

工作代码更新查询

sqlite3 *contactDB; //Declare a pointer to sqlite database structure
            const char *dbpath = [dbPath UTF8String]; // Convert NSString to UTF-8
            if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
            {
                //Database opened successfully
                NSString *databaseName = @"vistorlog.db";
                // Get the path to the documents directory and append the databaseName
                NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                NSString *documentsDir = [documentPaths objectAtIndex:0];
                NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
                const char *dbPath=[databasePath UTF8String];
                if (sqlite3_open(dbPath, &myDB)==SQLITE_OK) {
                    NSLog(@"database Opened");

// define dateString and IDCODE as strings before this
                    const char *updateQuery = "Update log set TIMEOUT=? where _ID=?";
                    sqlite3_stmt *stmt;
                    // Prepare Stment
                    if (sqlite3_prepare_v2(myDB, updateQuery, -1, &stmt, NULL) == SQLITE_OK) {
                        sqlite3_bind_text(stmt, 1, [dateString UTF8String], -1, SQLITE_TRANSIENT);
                        sqlite3_bind_text(stmt, 2, [IDCODE UTF8String], -1, SQLITE_TRANSIENT);                            if(sqlite3_step(stmt) == SQLITE_DONE) {
                            NSLog(@"Query Executed");
                        } else {
                            NSLog(@"Query NOT Executed: %s", sqlite3_errmsg(myDB));
                        }
                        sqlite3_finalize(stmt);

                    }else{
                        NSLog(@"Statement NOT Prepared: %s", sqlite3_errmsg(myDB));
                    }
                }
                sqlite3_close(myDB);
                }
                else {
                //Failed to open database
            }

不要忘记导入sqlite3.h

标记

0 个答案:

没有答案