0

我是 iPhone 开发的新手。我正在制作一个应用程序,使用 sqlite3 作为数据库。我用 sqlite3 manager 3.9.5 创建了我的表,并在 appdelegete.m 文件中编写了代码复制数据库,并成功复制了数据库。

我在数据库中插入数据并使用主键成功插入,但是当我从数据库访问数据时,它发送 null 并且如果我打开我的数据库并打开我的表,它们不会显示我在表中输入的数据。这是我插入数据并从数据库获取数据的代码。

- (IBAction)addrecord:(id)sender {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"ebirthdaydatabase.sqlite"];
        if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {


            const char *sqlStatement = "insert into Name(name) VALUES(?)";
            sqlite3_stmt *compiledStatement;                
            if (sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK){
                NSLog(@"name.text :%@",name.text);

                sqlite3_bind_text(compiledStatement, 1, [name.text UTF8String], -1, SQLITE_TRANSIENT);
//                ShowContactsViewController *contact = [[ShowContactsViewController alloc]init];
//                contact.username = name.text;
//                [contact addarray:contact];

            }

            if(sqlite3_step(compiledStatement) != SQLITE_DONE ) {
                NSLog( @"Error: %s", sqlite3_errmsg(database) );
            } else {
                NSLog( @"Insert into row id = %lld", sqlite3_last_insert_rowid(database));
            }
            sqlite3_finalize(compiledStatement);

        sqlite3_close(database);
        }

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

    NSLog(@"%@",path);
    if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {

        const char *sql = "select name from Name";
        sqlite3_stmt *selectstmt;
        if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {

            while(sqlite3_step(selectstmt) == SQLITE_ROW) {

                //                NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
                //                Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:primaryKey];
                ShowContactsViewController *contact=[[ShowContactsViewController alloc]init];

                char *localityChars =( char *)sqlite3_column_name(selectstmt, 0);

                if (localityChars ==NULL)
                    contact.username = nil;
                else
                    contact.username = [NSString stringWithUTF8String: localityChars];

                //contact.username = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
                NSLog(@"%@",username);

                [showcontact addObject:contact];
            }
        }
    }
4

2 回答 2

0

sqlite3_column_name()函数将返回列的名称,而不是内容。你想要sqlite3_column_text()

于 2012-06-29T08:06:15.277 回答
0

如果您无法解决问题,请使用一些经理。例如https://github.com/misato/SQLiteManager4iOS

于 2012-06-29T07:49:46.570 回答