user3452684
user3452684

Reputation: 13

sqlite3_exec == SQLITE_OK failure

I'm new to developing in objective C and am struggling to step into an IF statement (sqlite3_exec==SQLITE_OK). I have been using tutorials and dont seem to be able to find my answer.

Is anyone able to show me where I'm going wrong?

-(IBAction)addItemButton:(id)sender {
    char *error;

    if (sqlite3_open([dbPathString UTF8String], &itemDB) == SQLITE_OK) {
        NSString *insertStat = [NSString stringWithFormat:@"INSERT INTO ITEMS(ITEM) values ('%s')", [self.itemField.text UTF8String]];

        const char *insert_stat = [insertStat UTF8String];


        if (sqlite3_exec(itemDB, insert_stat, NULL, NULL, &error)== SQLITE_OK) {
            NSLog(@"Item Added");

            Item *item = [[Item alloc]init];

            [item setItem:self.itemField.text];

            [arrayOfItem addObject:item];
        }else{
            NSLog(@"Item not added");
        }

        sqlite3_close(itemDB);
    }
}

Upvotes: 1

Views: 293

Answers (1)

Muhammad Zeeshan
Muhammad Zeeshan

Reputation: 2451

I have written a simple SQLite helper for performing general database tasks with few lines of code like fetching records from DB, Inserting, Updating and Deleting records. Source code can be found here with example

Download and Drag, drop the ZeeSQLiteHelper classes in your project and set your DB name in ZeeSQLiteHelper class.

Getting records example:

[ZeeSQLiteHelper initializeSQLiteDB];
NSString *query = @"SELECT * FROM recipes";
NSMutableArray *results = [ZeeSQLiteHelper readQueryFromDB:query];
[ZeeSQLiteHelper closeDatabase];

For insertion

[ZeeSQLiteHelper initializeSQLiteDB];
NSString *queryString = [NSString stringWithFormat:@"insert into %@ (%@,%@) VALUES ('%@','%@')",downloadsTblName, tblAttrFileName, tblAttrFileURL, downloadInfo.fileName, downloadInfo.fileURL];
[ZeeSQLiteHelper executeQuery:queryString];
[ZeeSQLiteHelper closeDatabase];

For Updation

[ZeeSQLiteHelper initializeSQLiteDB];
NSString *queryString = [NSString stringWithFormat:@"UPDATE %@ SET %@='%@' WHERE %@='%@'",downloadsTblName, tblAttrFileName, newFilePath.lastPathComponent, tblAttrFileName,oldFilePath.lastPathComponent];
[ZeeSQLiteHelper executeQuery:queryString];
[ZeeSQLiteHelper closeDatabase];

For Deletion

[ZeeSQLiteHelper initializeSQLiteDB];
NSString *queryString = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@='%@'",downloadsTblName, tblAttrFileName,downloadedFileObj.videoTitle];
[ZeeSQLiteHelper executeQuery:queryString];
[ZeeSQLiteHelper closeDatabase];

Appropriate message of success or failure will be logged on console.

Upvotes: 2

Related Questions