在我的视图控制器中,它显示了很多细节。所以当用户点击ADD按钮时,它会将数据插入到sqlite中。问题是如何检查之前是否插入了数据。
//this code is in viewDidLoad
NSString *sql = [NSString stringWithFormat:@"SELECT * FROM exhibitor"];
sqlite3_stmt *statement;
check= 0;
if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil) == SQLITE_OK){
while (sqlite3_step(statement)==SQLITE_ROW) {
char *exName2 = (char *) sqlite3_column_text(statement, 1);
NSString *exNameStr = [[NSString alloc] initWithUTF8String:exName2];
NSString *exhibName = [exhibitionArticle objectForKey:@"ex_name"];
if([exNameStr isEqualToString:(exhibName)] ){
check += 1;
}else{
check += 0;
}
}
NSLog(@"%d Result is", check);
} //this code checking my data is in sqlite or not
如果数据不在sqlite中,则按钮插入数据。
- (IBAction)addExhibitor:(id)sender {
NSLog(@"%d this is addExhibitor int", check);
if(check == 0){
NSDate *exhibAddDate = [NSDate date];
NSString *exhibName = [exhibitionArticle objectForKey:@"ex_name"];
NSString *exhibAbout = [exhibitionArticle objectForKey:@"ex_about"];
NSString *exhibBooth = [exhibitionArticle objectForKey:@"ex_booth"];
NSString *exhibAddress = [exhibitionArticle objectForKey:@"ex_address"];
NSString *exhibTelephone = [exhibitionArticle objectForKey:@"ex_telephone"];
NSString *exhibFax = [exhibitionArticle objectForKey:@"ex_fax"];
NSString *exhibWebSite = [exhibitionArticle objectForKey:@"ex_website"];
NSString *exhibEmail = [exhibitionArticle objectForKey:@"ex_email"];
NSString *sql1 = [NSString stringWithFormat:@"INSERT INTO exhibitor ('exhibAddDate', 'exhibName', 'exhibAbout', 'exhibBooth', 'exhibAddress', 'exhibTelephone', 'exhibFax', 'exhibWebSite', 'exhibEmail') VALUES('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@')", exhibAddDate, exhibName, exhibAbout, exhibBooth, exhibAddress, exhibTelephone, exhibFax, exhibWebSite, exhibEmail ];
char *err;
if(sqlite3_exec(db, [sql1 UTF8String], NULL, NULL, &err) != SQLITE_OK){
sqlite3_close(db);
NSAssert(0, @"Could not update table");
}else{
NSLog(@"Table Updated");
}
}
else{
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Same Data" message:@"Your data already there" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
// [alert show];
}
}
答案 0 :(得分:2)
int check = 0;
sqlite3_stmt *statement = nil;
const char * sql;
sql = "SELECT COUNT(*) FROM exhibitor WHERE exhibName = ?";
sqlite3_prepare_v2(db, sql, -1, &statement, NULL);
sqlite3_bind_text(statement, 1, [[exhibitionArticle objectForKey:@"ex_name"] UTF8String], -1, SQLITE_TRANSIENT);
while (sqlite3_step(statement) == SQLITE_ROW) {
check = sqlite3_column_int(statement, 0);
}
sqlite3_finalize(statement);
if (check == 0) {
NSLog(@"Result of count is %d and so the data is not in the database", check);
//insert the data
NSDate *exhibAddDate = [NSDate date];
NSString *exhibName = [exhibitionArticle objectForKey:@"ex_name"];
NSString *exhibAbout = [exhibitionArticle objectForKey:@"ex_about"];
NSString *exhibBooth = [exhibitionArticle objectForKey:@"ex_booth"];
NSString *exhibAddress = [exhibitionArticle objectForKey:@"ex_address"];
NSString *exhibTelephone = [exhibitionArticle objectForKey:@"ex_telephone"];
NSString *exhibFax = [exhibitionArticle objectForKey:@"ex_fax"];
NSString *exhibWebSite = [exhibitionArticle objectForKey:@"ex_website"];
NSString *exhibEmail = [exhibitionArticle objectForKey:@"ex_email"];
sql = "INSERT INTO exhibitor ('exhibAddDate', 'exhibName', 'exhibAbout', 'exhibBooth', 'exhibAddress', 'exhibTelephone', 'exhibFax', 'exhibWebSite', 'exhibEmail') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
sqlite3_prepare_v2(db, sql, -1, &statement, NULL);
sqlite3_bind_text(statement, 1, [exhibAddDate UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 2, [exhibName UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 3, [exhibAbout UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 4, [exhibBooth UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 5, [exhibAddress UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 6, [exhibTelephone UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 7, [exhibFax UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 8, [exhibWebSite UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement, 9, [exhibEmail UTF8String], -1, SQLITE_TRANSIENT);
if (sqlite3_step(statement) == SQLITE_DONE){
NSLog(@"Exhibit added");
} else {
NSLog( @"Failed from sqlite3_prepare_v2. Error is: %s", sqlite3_errmsg(db));
}
} else {
NSLog(@"Result of count is %d and so the data is already in the database", check);
}
sqlite3_finalize(statement);
sqlite3_close(db);
尝试这样的事情!
- > sql中的exhibitName< - 必须是您要搜索数据的列。
最好将这一切都放在你拥有的IBAction中。如果检查== 0,执行插入。
请看一下绑定变量,因为你很容易受到sql注入攻击。这意味着,如果我输入类似:“blabla; DELETE * FROM Exhibitor; blabla”作为标题,那么您冒着用户删除表中所有数据的风险!绑定变量确保不会发生这种情况。
周围没有XCode,所以我无法检查它是否全部有效..否则一直蠢蠢欲动直到你拥有它!
祝你好运