0
我使用下面的代碼將代碼插入數據庫。這些數據被「:::」分割並且被分割的這些值被添加到數據庫中。當我檢查了數據庫,數據不是SQLite中添加我無法將值插入到sqlite db
#pragma mark - Web View Delegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *strResponse = [[request URL] absoluteString];
if ([strResponse hasPrefix:@"ios:"]) {
NSString *newString = [strResponse substringFromIndex:[@"ios:" length]];
newString = [newString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
UIAlertView *uiAlert = [[UIAlertView alloc] initWithTitle:@"Message" message:newString delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[uiAlert show];
NSArray *arrItems = [newString componentsSeparatedByString:@":::"];
for (NSString *strItems in arrItems) {
[strItems stringByRemovingPercentEncoding];
NSMutableDictionary *mDic = [[NSMutableDictionary alloc] init];
[mDic setObject:strItems forKey:@"item_name"];
[mDic setObject:@"0" forKey:@"is_deleted"];
[[DataBase connection] insertTableData:mDic AndTableName:@"school_feed"];
}
return NO;
}
return YES;
}
插入表格數據的方法
- (void)insertTableData:(NSMutableDictionary *)mDictDetails AndTableName:(NSString*)table_name {
NSArray *arrAllKey = [mDictDetails allKeys];
NSString *key = @"";
NSString *values = @"";
for (NSInteger i = 0; i <arrAllKey.count ; i++) {
NSString *column = arrAllKey[i];
if ([key isEqualToString:@""]) {
key = column;
values = @"?";
} else {
key = [key stringByAppendingString:[NSString stringWithFormat:@",%@", column]];
values = [values stringByAppendingString:@", ?"];
}
}
NSString *query = [NSString stringWithFormat: @"INSERT OR REPLACE INTO %@ (%@) values(%@)", table_name, key, values];
const char *sql = [query UTF8String];
sqlite3_stmt *insertStatement; // statement created
if (sqlite3_prepare_v2(database, sql, -1, &insertStatement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}// if
for (int i = 0; i <arrAllKey.count; i++) {
NSString *column = arrAllKey[i];
NSString *strKeys = [NSString stringWithFormat:@"%@", [mDictDetails objectForKey:column]];
sqlite3_bind_text(insertStatement, i + 1, [strKeys UTF8String], -1, SQLITE_TRANSIENT);
}
sqlite3_step(insertStatement);// executing query
sqlite3_finalize(insertStatement); // finalizing statement
}
添加代碼[數據庫連接] insertTableData:MDIC AndTableName:@ 「school_feed」];' – iphonic
@iphonic:問題更新 –
它看起來像你這樣做不對於'key'和'values'有正確的字符串,確保不應該有任何逗號(「,」),或者以逗號(「,」)結尾。例如,你的鍵和值應該看起來像這個'name,age'和'?,?'它不應該是',name,age,'或'',?,?,'這可能是你的情況。順便說一句,爲什麼你不使用一個偉大的包裝的sqlite請參見[this](https://github.com/ccgus/fmdb) – iphonic