gpt4 book ai didi

iphone - Sqlite 不执行查询 Some time

转载 作者:行者123 更新时间:2023-11-28 22:29:28 25 4
gpt4 key购买 nike

我有需要对数据库执行的查询序列..大多数时候它工作正常..但有时它无法插入查询。

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);
}

以上是我为执行插入操作而实现的代码...

任何人都可以帮助我找到它是否失败然后由于什么原因它无法插入数据库以便我可以处理错误..

最佳答案

使用该方法在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);


}

}

此外,如果插入不能正常工作,原因可能是文件不在文档目录中,如果它在包中,它将获取数据,但如果数据库在包中,则无法更新或插入值,将其复制到文档中目录,然后尝试使用它

-(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];

}

For more info see this

关于iphone - Sqlite 不执行查询 Some time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17876910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com