创建数据库
NSArray *paths= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *strPaths = [documentsDirectory stringByAppendingPathComponent:databaseName]; if (sqlite3_open([strPaths UTF8String],&database) != sqlITE_OK) { sqlite3_close(database); NSAssert(0,@"MBDataBase info:>>>>Failed to open databse"); }
执行sql语句
if(sqlite3_exec(database,[ssql_userTable UTF8String],nil,nil)!= sqlITE_OK)
解析查询结果:
const char *sql_select = [ssql_search_byID UTF8String]; sqlite3_stmt *statement; statement = nil; if (sqlite3_prepare_v2(database,sql_select,-1,&statement,NULL) != sqlITE_OK) { NSLog(@"Error: Failed to prepare statement with message:get channels."); return nil; } while (sqlite3_step(statement) == sqlITE_ROW) { char* c_result = (char*) sqlite3_column_text(statement,0); }
返回查询行数:
int nRow,nColumn; char * errmsg = NULL; char **dbResult; int unReadMessageCount = -1; if (sqlite3_get_table(database,[ssql_unRead_byID UTF8String],&dbResult,&nRow,&nColumn,&errmsg) == sqlITE_OK) { NSLog(@">>>>nRow>>>%d,>>>>nColumn>>%d",nRow,nColumn); int result_count = atoi((const char*)dbResult[nColumn]); }原文链接:https://www.f2er.com/sqlite/201209.html