ios - 从 SQLite 检索具有多条记录的数据

标签 ios cocoa sqlite

想知道是否有人可以分享如何从 SQLite 检索具有多个记录的数据,即我有一个包含 5 个字段的表,每个字段都有多个数据。

NSString *str = [[NSString alloc] initWithFormat:@"%@ %@ etc...", oneString,two...];

[entry addObject:str];

我只得到第一条记录

下面的字符串还可以工作

NSString *sql = [NSString stringWithFormat:@"SELECT * FROM abc"];

但是这个没有,我崩溃了!

NSString *sql = [NSString stringWithFormat:@"SELECT * FROM abc WHERE name= \"john\""];

最佳答案

-(NSMutableArray *)readInformationFromDatabase
{
NSMutableArray *array = [[NSMutableArray alloc] init];

// Setup the database object
sqlite3 *database;

// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
    // Setup the SQL Statement and compile it for faster access

    //SQLIte Statement 
    NSString *sqlStatement_userInfo =[NSString stringWithFormat:@"Select * from TableName"];

    sqlite3_stmt *compiledStatement;


    if(sqlite3_prepare_v2(database, [sqlStatement_userInfo UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
    {

        // Loop through the results and add them to the feeds array
        while(sqlite3_step(compiledStatement) == SQLITE_ROW)
        {
            // Init the Data Dictionary 
            NSMutableDictionary *_dataDictionary=[[NSMutableDictionary alloc] init];

            NSString *_userName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
           // NSLog(@"_userName = %@",_userName);

            NSString *_emailID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
           // NSLog(@"_emailID = %@",_emailID);

            NSString *_contactNumber = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
           // NSLog(@"_contactNumber = %@",_contactNumber);

            NSString *_address = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
           // NSLog(@"_address = %@",_address);

            NSString *_zipCode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
           // NSLog(@"_zipCode = %@",_zipCode);

            [_dataDictionary setObject:[NSString stringWithFormat:@"%@",_userName] forKey:@"UserName"];
            [_dataDictionary setObject:[NSString stringWithFormat:@"%@",_emailID] forKey:@"EmailId"];
            [_dataDictionary setObject:[NSString stringWithFormat:@"%@",_contactNumber] forKey:@"ContactNumber"];
            [_dataDictionary setObject:[NSString stringWithFormat:@"%@",_address] forKey:@"Address"];
            [_dataDictionary setObject:[NSString stringWithFormat:@"%@",_zipCode] forKey:@"ZIPCode"];

            [array addObject:_dataDictionary]; 
        }
    }
    else
    {
        NSLog(@"No Data Found");
    }

    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);
}

sqlite3_close(database);

return array;
}

关于ios - 从 SQLite 检索具有多条记录的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16322739/

相关文章:

ios - 将物理体添加到 Spritekit 节点

ios - 在 Objective-C 中的 WKWebView 上评估 JavaScript 时出现 "A JavaScript exception occurred"

macos - 如何更改 OSX 上 NSTextField 焦点的效果

objective-c - 如何确定 NSButton 是否为复选框?

ios - 使用 Dropbox 备份/恢复 Core 数据的 sqlite 文件

python - 如何将 "inject"内存中的 SQLAlchemy sqlite3 数据库放入 Flask test_client?

c# - 64 位 SQLite.dll 和任何 CPU

ios - 如何使用 iTunes Connect 中的问题列表(报亭按钮)

objective-c - 在另一个 plist 文件中引用 info.plist 中的配置变量

cocoa - NSPersistentDocument 中的 NSTextView 在失去第一响应者之前不会更新脏标志