通用应用程序DB问题

时间:2011-07-19 15:13:31

标签: iphone xcode cocoa-touch ipad

我正在为iPhone和iPad制作一个通用应用程序,我想知道是否有一个特定的位置我必须放置我的sqlite数据库?因为他无法找到并且无法打开数据库的结果! = app无效:/

目前,我的数据库位于“其他来源”文件夹中。

//Maakt een lijst  van path strings voor de specifieke directories voor de specifieke domains.
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//Eerste object van paths wordt in de string gezet!
NSString *documentsDir = [documentPaths objectAtIndex:0];
//er wordt gezocht naar todo.sqlite in het opgegeven path!
NSString *databasePath = [documentsDir stringByAppendingPathComponent:@"todo.sqlite"];
pad = databasePath;

if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    status = TRUE; //DEBUG PURPOSES ONLY!
    const char *sql = "SELECT pk FROM todo";
    sqlite3_stmt *statement;

    if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
        while (sqlite3_step(statement) == SQLITE_ROW) {
            int primaryKey = sqlite3_column_int(statement, 0);
            Taak *taak = [[Taak alloc] initWithPrimaryKey:primaryKey database:database];
            [taken addObject:taak];
            [taak release];
        }
    }

    sqlite3_finalize(statement);
}else {
    status = FALSE; //DEBUG PURPOSES ONLY!
    sqlite3_close(database);
    NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
}

这是我的连接代码,我搜索了很多论坛和教程,每个人都这样做!

1 个答案:

答案 0 :(得分:0)

if(sqlite3_open([databasepath UTF8String], &database) == SQLITE_OK) {
        const char* sqlStatement; 
        sqlStatement="select fld_carromdescription from tbl_carrom where fld_carromdetails like?";

        sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);
        printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database));

        if( sqlite3_prepare_v2(database, sqlStatement, -1, & compiledStatement, NULL) == SQLITE_OK ) {
            sqlite3_bind_text(compiledStatement,1,[[usdflt objectForKey:@"phone"] UTF8String] , -1,SQLITE_STATIC);
            NSLog(@"Database check111");
            //  [tablearray removeAllObjects];
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                printf("in loop");

                @try {
                    NSLog(@"Database check");
                    txtview.text=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,0)];
                    toplbl.text=[usdflt valueForKey:@"phone"];
                    [txtview scrollRangeToVisible:NSMakeRange(0, 0)];
                }

                @catch (NSException * e) {

                }
                @finally {

                }

            }
        }
        sqlite3_finalize(compiledStatement);

    }

    sqlite3_close(database);
}

试试这个。

我也使用sqliteDB中的other sources only。我做了3个通用的应用程序。我没有困难获得访问权。