如果不存在,则将联系人插入数据库,否则将联系人更新到数据库。
static sqlite3_stmt *statement = nil;
-(void)syncPhoneBookWithVcardsTable
{
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
NSString *dbPath = [userDefault objectForKey:@"DATABASEPATH"];
NSLog(@"databasePath :%@", dbPath);
const char *dbpath = [dbPath UTF8String];
NSLog(@"dbpath :%s", dbpath);
for (Contacts *objContact in self.arrContacts ) {
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:@"select id from vcards where firstname =\"%@\" and lastname=\"%@\" and telcell=\"%@\" and emailWork=\"%@\" and org=\"%@\"",objContact.firstname,objContact.surname,objContact.telcell,objContact.emailWork,objContact.org];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(database,query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *ids = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
NSLog(@"Id :%@",ids);
sqlite3_finalize(statement);
const char *update_stmt = "update vcards set salute=?,firstname=?,midname=?,lastname=?,org=?,jobtitle=?,dept=?,telwork=?,telhome=?,telcell=?,telworkfax=?,telpager=?,msn=?,skype=?,websiteWork=?,emailWork=?,bday=?,streetW=?,cityW=?,stateW=?,postalcodeW=?,countryW=?,streetH=?,cityH=?,stateH=?,postalcodeH=?,countryH=?,notes=? where id=?";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/dd/YYYY"];
NSString *dateString= [NSString stringWithFormat:@"%@",objContact.bday];
NSLog(@"Error %s while preparing statement", sqlite3_errmsg(database));
if(sqlite3_prepare_v2(database, update_stmt,-1, &statement, NULL) == SQLITE_OK)
{
sqlite3_bind_text(statement,1,[objContact.salute UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,2,[objContact.firstname UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,3,[objContact.midname UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,4,[objContact.surname UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,5,[objContact.org UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,6,[objContact.jobtitle UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,7,[objContact.dept UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,8,[objContact.telwork UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,9,[objContact.telhome UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,10,[objContact.telcell UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,11,[objContact.telworkfax UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,12,[objContact.telpager UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,13,[objContact.msn UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,14,[objContact.skype UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,15,[objContact.urlWork UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,16,[objContact.emailWork UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,17,[dateString UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,18,[objContact.street UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,19,[objContact.city UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,20,[objContact.statew UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,21,[objContact.postalcode UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,22,[objContact.country UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,23,[objContact.streetH UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,24,[objContact.cityH UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,25,[objContact.stateH UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,26,[objContact.postalcodeH UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,27,[objContact.countryH UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,28,[objContact.note UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(statement,29,[ids UTF8String], -1, SQLITE_TRANSIENT);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Record updated");
}
else {
NSLog(@"Record updation failed");
NSLog(@"Error %s while preparing statement", sqlite3_errmsg(database));
}
sqlite3_finalize(statement);
//sqlite3_close(database);
//sqlite3_reset(statement);
//statement = nil
}
else
{
NSAssert1(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
}
else
{
NSString *insertSQL = [NSString stringWithFormat:@"insert into vcards (vcardid,salute,firstname,midname,lastname,org,jobtitle,dept,telwork,telhome,telcell,telworkfax,telpager,msn,skype,websiteWork,emailWork,bday,streetW,cityW,stateW, postalcodeW,countryW,streetH,cityH,stateH,postalcodeH,countryH,notes) values(\"%d\",\"%@\", \"%@\", \"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\",\"%@\")",[objContact.VcardID integerValue],objContact.salute, objContact.firstname, objContact.midname,objContact.surname,objContact.org,objContact.jobtitle,objContact.dept,objContact.telwork,objContact.telhome,objContact.telcell,objContact.telworkfax,objContact.telpager,objContact.msn,objContact.skype,objContact.urlWork,objContact.emailWork,objContact.bday,objContact.street,objContact.city,objContact.statew,objContact.postalcode,objContact.country,objContact.streetH,objContact.cityH,objContact.stateH,objContact.postalcodeH,objContact.countryH,objContact.note];
const char *insert_stmt = [insertSQL UTF8String];
NSLog(@"insert_stmt :%s", insert_stmt);
sqlite3_prepare_v2(database, insert_stmt,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Record inserted");
}
else
{
NSLog(@"Record inserted failed");
}
sqlite3_finalize(statement);
}
}
sqlite3_close(database);
}//end of if
}
}
有时候我的应用程序崩溃了
if(sqlite3_prepare_v2(database,query_stmt,-1,& statement,NULL)== SQLITE_OK)此行.BAD ACCESS,有时它没有更新,显示错误,如数据库在准备语句之前被锁定。请看任何一个进去。提前谢谢。