iPhone,FMDatabase:executeQuery无法正常工作

时间:2013-10-25 08:05:59

标签: iphone xcode fmdb

以下是我正在使用的代码:

FMDatabaseQueue *queue = [[DataBaseHelper sharedManager] queue];
[queue inDatabase:^(FMDatabase *db) {

FMResultSet *results = [db executeQuery:@"select * from EVENT where ESTATUS='unread' and HIT_ATTEMPTS < 5 "];

if([results next])
{
   [results close];

   @try {
           BOOL success=FALSE;
           NSString *query=[NSString stringWithFormat:@"insert into EVENT (EDATA, ESTATUS ,EUSER) values('%@','%@','%@')",@"google.com",@"unread",@"xyz"];

           success = [db executeUpdate:query];
           NSLog(@"create edata row, %d:%@", [db lastErrorCode], [db lastErrorMessage]);
        }

   @catch (NSException *exception) {
        NSLog(@"Exception");
   }

在上面的代码中 success = [db executeUpdate:query];不管用。我试过放置断点。这行代码全部退出。没有任何日志被打印出来。

1 个答案:

答案 0 :(得分:0)

我不确定你为什么要使用try和Catch,无论如何都要遵循它肯定会有效的步骤

创建数据库

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *path = [docsPath stringByAppendingPathComponent:@"database.sqlite"];

FMDatabase *database = [FMDatabase databaseWithPath:path];

打开数据库并创建表格(可选)

[database open];
//optional
[database executeUpdate:@"create table user(name text primary key, age int)"]; 

查询数据库

FMResultSet *results = [database executeQuery:@"select * from user"];
while([results next]) {
    NSString *name = [results stringForColumn:@"name"];
    NSInteger age  = [results intForColumn:@"age"];        
    NSLog(@"User: %@ - %d",name, age);
}
[database close];