我有需要使用数据库执行的查询序列.. 大部分时间它的工作正常..但有一段时间它无法插入查询。
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
for (int i=0;i<[queries count]; i++)
{
NSString *query = [queries objectAtIndex:i];
const char *Insert_query = [query UTF8String];
sqlite3_prepare(contactDB, Insert_query, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
//NSLog(@" \n\n\n\n %@ done query",query);
}
else {
NSLog(@" \n\n\n\n %@ not done query",query);
}
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
以上是我执行插入操作的代码......
任何人都可以帮我找到它是否失败然后因为什么原因它无法插入数据库所以我可以处理错误..
答案 0 :(得分:2)
使用此方法在sqlite上执行查询
//-----------------------------------------------------------------------------------------------------//
#pragma mark - Helper methods
//-----------------------------------------------------------------------------------------------------//
-(BOOL)dbOpenedSuccessfully
{
if(sqlite3_open([[self dbPath] UTF8String], &_database) == SQLITE_OK)
{
return YES;
}
else
{
[[[UIAlertView alloc]initWithTitle:@"Error"
message:@"Error on opening the DB"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil]show];
return NO;
}
}
//-----------------------------------------------------------------------------------------------------//
#pragma mark - Query
//-----------------------------------------------------------------------------------------------------//
- (void) executeQuery:(NSString *)strQuery
{
char *error = NULL;
if([self dbOpenedSuccessfully])
{
NSLog(@"%@",strQuery);
sqlite3_exec(_database, [strQuery UTF8String], NULL, NULL,&error);
if (error!=nil) {
NSLog(@"%s",error);
}
sqlite3_close(_database);
}
}
此外,如果插入不正常,原因可能是文件不在文档目录中,如果它在bundle中,它将获取数据,但如果db在bundle中则无法更新或插入值,将其复制到文档目录,然后尝试使用它
-(void) checkAndCreateDatabase
{
// Check if the SQL database has already been saved to the users phone, if not then copy it over
BOOL success;
// Create a FileManager object, we will use this to check the status
// of the database and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];
// Check if the database has already been created in the users filesystem
success = [fileManager fileExistsAtPath:_databasePath];
// If the database already exists then return without doing anything
if(success) return;
// If not then proceed to copy the database from the application to the users filesystem
// Get the path to the database in the application package
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:_databaseName];
// Copy the database from the package to the users filesystem
[fileManager copyItemAtPath:databasePathFromApp toPath:_databasePath error:nil];
}
答案 1 :(得分:0)
您可以尝试以下列方式打印错误:根据您可以做出决定的错误。
if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
for (int i=0;i<[queries count]; i++)
{
NSString *query = [queries objectAtIndex:i];
const char *Insert_query = [query UTF8String];
sqlite3_prepare(contactDB, Insert_query, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
//NSLog(@" \n\n\n\n %@ done query",query);
}
else {
NSLog(@"sqlite3_step error: '%s'", sqlite3_errcode(contactDB));
NSLog(@" \n\n\n\n %@ not done query",query);
}
}
sqlite3_finalize(statement);
sqlite3_close(contactDB);
}
此外,
SQLITE_DONE表示语句已完成执行 成功。不应该再次调用sqlite3_step() 虚拟机没有先调用sqlite3_reset()来重置 虚拟机回到初始状态。
您可以使用SQLITE_OK而不是SQLITE_DONE。