I had integrate FMDB library in my code, i try to load multiple query in single function but i have received some issue
我在代码中集成了FMDB库,我尝试在单个函数中加载多个查询,但是我收到了一些问题
* Error calling sqlite3_step (21: out of memory) rs
* FMDB not open
* FMDB is in use
Is it possible to run multiple query in single function?, please let me know your comments.
是否可以在一个函数中运行多个查询?,请告诉我你的意见。
Thanks in Advance
谢谢提前
I tried this FYI:
我试着这仅供参考:
-(NSMutableArray *)getMyInfo
{
NSMutableArray *contactList = [NSMutableArray array];
if(![instance.database open])
{
DLog(@"Could not open DB");
return nil;
}
********** QUERY 1 **************
FMResultSet *resultSet=[instance.database executeQuery:@"SELECT * FROM mycontacts WHERE cont_hidden = 0 and cont_recent_chat_time IS NOT NULL ORDER BY strftime(cont_recent_chat_time) DESC"];
if(resultSet)
{
while([resultSet next])
{
@autoreleasepool
{
int type = [resultSet intForColumn:@"contact_type"];
Contactbean *contactObj = [[Contactbean alloc]init];
[contactObj setPhoneNo:[resultSet stringForColumn:@"cont_phonenumber"]];
********** QUERY 2 **************
* * * * * * * * * *查询2 * * * * * * * * * * * * * *
FMResultSet *getCountResult=[instance.database executeQuery:@"SELECT count(kchatid) FROM mychat WHERE kisread = 0 and kchatnumber = ?",[resultSet stringForColumn:@"cont_phonenumber"]];
if ([getCountResult next]) {
if([getCountResult intForColumnIndex:0] > 0)
{
[contactObj setCount:[getCountResult intForColumnIndex:0]];
}
}
[getCountResult close];
********** QUERY 3 **************
* * * * * * * * * *查询3 * * * * * * * * * * * * * *
FMResultSet *getRecentResult=[instance.database executeQuery:@"SELECT * from mychat where kchatnumber =? AND kchattime =?",[resultSet stringForColumn:@"cont_phonenumber"],[resultSet stringForColumn:@"status"]];
if([getRecentResult next])
{
[contactObj setStatus:[getRecentResult intForColumn:@"status"]];
}
[getRecentResult close];
// Adding contact to the list
[contactList addObject:contactObj];
}
}
}
[resultSet close];
[instance.database close];
return contactList;
}
1 个解决方案
#1
2
you are not opening and closing properly the database object, should be something like this:
您没有正确地打开和关闭数据库对象,应该是这样的:
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [docPaths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"theDataBase.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];//Open the database
//Execute the update you want
[database executeUpdate:@"INSERT INTO some_table (something) values (?)", someValue];
//Execute the consults you want
FMResultSet *results = [database executeQuery:@"SELECT * FROM some_table"];
if ([results next])
{
//Do something with the data it brings you
}
[database close];//Close the database
Dont forget to read the official documentation: FMDB v2.5
不要忘记阅读官方文档:FMDB v2.5
#1
2
you are not opening and closing properly the database object, should be something like this:
您没有正确地打开和关闭数据库对象,应该是这样的:
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [docPaths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"theDataBase.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
[database open];//Open the database
//Execute the update you want
[database executeUpdate:@"INSERT INTO some_table (something) values (?)", someValue];
//Execute the consults you want
FMResultSet *results = [database executeQuery:@"SELECT * FROM some_table"];
if ([results next])
{
//Do something with the data it brings you
}
[database close];//Close the database
Dont forget to read the official documentation: FMDB v2.5
不要忘记阅读官方文档:FMDB v2.5