我不熟悉在目标C中进行开发,并且正在努力进入IF语句(sqlite3_exec == SQLITE_OK)。我一直在使用教程,似乎无法找到我的答案。
有人能告诉我哪里出错吗?
-(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);
}
}
答案 0 :(得分:2)
我编写了一个简单的SQLite帮助程序,用于执行一般数据库任务,只需几行代码,例如从数据库中获取记录,插入,更新和删除记录。 Source code can be found here with example
下载并拖动,在项目中删除ZeeSQLiteHelper类,并在ZeeSQLiteHelper类中设置数据库名称。
获取记录示例:
[ZeeSQLiteHelper initializeSQLiteDB];
NSString *query = @"SELECT * FROM recipes";
NSMutableArray *results = [ZeeSQLiteHelper readQueryFromDB:query];
[ZeeSQLiteHelper closeDatabase];
插入
[ZeeSQLiteHelper initializeSQLiteDB];
NSString *queryString = [NSString stringWithFormat:@"insert into %@ (%@,%@) VALUES ('%@','%@')",downloadsTblName, tblAttrFileName, tblAttrFileURL, downloadInfo.fileName, downloadInfo.fileURL];
[ZeeSQLiteHelper executeQuery:queryString];
[ZeeSQLiteHelper closeDatabase];
有关更新
[ZeeSQLiteHelper initializeSQLiteDB];
NSString *queryString = [NSString stringWithFormat:@"UPDATE %@ SET %@='%@' WHERE %@='%@'",downloadsTblName, tblAttrFileName, newFilePath.lastPathComponent, tblAttrFileName,oldFilePath.lastPathComponent];
[ZeeSQLiteHelper executeQuery:queryString];
[ZeeSQLiteHelper closeDatabase];
删除
[ZeeSQLiteHelper initializeSQLiteDB];
NSString *queryString = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@='%@'",downloadsTblName, tblAttrFileName,downloadedFileObj.videoTitle];
[ZeeSQLiteHelper executeQuery:queryString];
[ZeeSQLiteHelper closeDatabase];
将在控制台上记录适当的成功或失败消息。