【问题标题】:iPhone,FMDatabase : executeQuery not workingiPhone,FMDatabase:executeQuery 不起作用
【发布时间】:2013-10-25 08:05:58
【问题描述】:

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

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

在上面的代码中 成功 = [数据库执行更新:查询];不管用。我试过放置断点。这行代码后全部退出。没有任何日志被打印出来。

【问题讨论】:

    标签: iphone xcode fmdb


    【解决方案1】:

    我不确定你为什么要使用 try and 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];
    

    【讨论】:

    • 没有尝试捕获同样的问题仍然存在
    • @TechFanatic 你试过我提到的简单方法吗??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 2012-02-19
    • 2011-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多