Ios 如何将结果从sqlite设置为数组NSMutableArray

Ios 如何将结果从sqlite设置为数组NSMutableArray,ios,sqlite,Ios,Sqlite,这是我的密码: NSString *ask = [NSString stringWithFormat:@"SELECT name FROM users"]; sqlite3_stmt *statement; const char *query_statement = [ask UTF8String]; const char *dbpath = [_databasePath UTF8String]; if (sqlite3_open(dbpath, &_DB

这是我的密码:

NSString *ask = [NSString stringWithFormat:@"SELECT name FROM users"];
    sqlite3_stmt *statement;
    const char *query_statement = [ask UTF8String];
    const char *dbpath = [_databasePath UTF8String];
    if (sqlite3_open(dbpath, &_DB) == SQLITE_OK){
        {
            if (sqlite3_prepare_v2(_DB, query_statement, -1, &statement, nil) == SQLITE_OK)
            {
                while (sqlite3_step(statement) == SQLITE_ROW)
                {
                    NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];

                }

                sqlite3_finalize(statement);
                sqlite3_close(_DB);
            }
        }
我想将上述结果设置为NSMUTABLEARRY,如下例所示:

 totalStrings =[[NSMutableArray alloc]initWithObjects:@"xxx",@"xxx",@"xxx", nil];
我该怎么办

=========================更新所有代码======================= 这是一个很容易看到的音调代码。我真的不知道我做错了什么

@interface ViewController ()
{
    NSMutableArray *totalStrings;
    NSMutableArray *filteredStrings;
    BOOL isFiltered;
}
@property(strong, nonatomic)NSMutableArray *entries;


@end


@implementation ViewController;
@synthesize entries;


- (void)viewDidLoad {



  //  totalStrings =[[NSMutableArray alloc]initWithObjects:@"ggg",@"gggggg", nil];
  //  NSLog(@"%@",totalStrings);


    NSMutableArray* result=[[NSMutableArray alloc] init];

    NSString *ask = [NSString stringWithFormat:@"SELECT * FROM users"];
    sqlite3_stmt *statement;
    const char *query_statement = [ask UTF8String];
    const char *dbpath = [_databasePath UTF8String];
    if (sqlite3_open(dbpath, &_DB) == SQLITE_OK){
        {
            if (sqlite3_prepare_v2(_DB, query_statement, -1, &statement, nil) == SQLITE_OK)
            {
                while (sqlite3_step(statement) == SQLITE_ROW)
                {
                    NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];

                    //add your namefield here.
                    [result addObject:nameField];
                     totalStrings=[[NSMutableArray alloc] initWithArray:result];



                }

                sqlite3_finalize(statement);
                sqlite3_close(_DB);
            }
        }
        NSLog(@"%@",totalStrings);

        //AND PUT THOSE RESULTS INTO THIS ARRAY ( I WILL USE THIS LATER)
        //I want it to be this format:  totalStrings=[[NSMutableArray alloc]initWithObjects:@"test1",@"test2",@"test3", nil];


    }

创建并分配新的NSMutableArray totalStrings,并在While循环中的totalStrings中添加nameField

NSMutableArray *totalStrings = [[NSMutableArray alloc] init];
在while循环中

[totalStrings addObject:nameField];

如果要获取数组中的所有名称字段,下面的代码应该可以工作

NSMutableArray* result=[[NSMutableArray alloc] init];

    NSString *ask = [NSString stringWithFormat:@"SELECT name FROM users"];
        sqlite3_stmt *statement;
        const char *query_statement = [ask UTF8String];
        const char *dbpath = [_databasePath UTF8String];
        if (sqlite3_open(dbpath, &_DB) == SQLITE_OK){
            {
                if (sqlite3_prepare_v2(_DB, query_statement, -1, &statement, nil) == SQLITE_OK)
                {
                    while (sqlite3_step(statement) == SQLITE_ROW)
                    {
                        NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];

                        //add your namefield here.
                       [result addObject:nameField];
                    }

                    sqlite3_finalize(statement);
                    sqlite3_close(_DB);
                }
            }
最终你能做到

totalStrings=[[NSMutableArray alloc] initWithArray:result];

你可以这样得到它

-(NSArray *)list_totalStrings
{
    NSMutableArray *totalStrings = [[NSMutableArray alloc] init];
    NSString *ask = [NSString stringWithFormat:@"SELECT name FROM users"]; //Make sure field and table names are proper
    sqlite3_stmt *statement;
    const char *query_statement = [ask UTF8String];
    const char *dbpath = [_databasePath UTF8String];
    if (sqlite3_open(dbpath, &_DB) == SQLITE_OK){
        if (sqlite3_prepare_v2(_DB, query_statement, -1, &statement, nil) == SQLITE_OK)
        {
            while (sqlite3_step(statement) == SQLITE_ROW)
            {
                NSString *nameField = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
                [totalStrings addObject:nameField];
            }

            sqlite3_finalize(statement);
            sqlite3_close(_DB);
            NSLog(@"%@",totalStrings);
            return totalStrings;
        }
        else
            return nil;
    }
    else
        return nil;
}

非常感谢您的回答,但我需要变量“totalstrings”,该变量包含此数组之后要使用的内容。怎么做?你可以做
totalStrings=[[NSMutableArray alloc]initWithArray:result]嗨,我做了,但我得到了结果(空):(你能确认你能用你的代码获取数据吗?
totalStrings
的数据类型是什么?这是
NSString
还是,
NSMutableArray
?嗨,我已经更新了代码。是的,我已经把数据放进了数据库。我不知道我做错了什么。嗨,我应该把第一行放在哪里?我已经把它放在@interface Vie了。)wController和我收到了错误消息。嘿,你把
totalStrings=[[NSMutableArray alloc]initWithArray:result];
放在了错误的位置。把你放在
NSLog
上面的所有循环放在外面。嗨,我已经做了,现在什么也没有得到:结果如下:2015-04-21 07:50:31.792 SQLexample[17542:299715]()你能把
NSLog(@“User-%@”,nameField);
放在
[result addObject:nameField];
上面,看看你有没有日志。嗨,我做了,结果是:2015-04-21 08:19:46.454 SQLexample[18800:313530](),但现在我发现…如果我把你的代码放到-(iAction)find:(id)sender{…}结果是出现。似乎我无法在加载应用程序后立即执行此操作。我是否做了一些错误或不正确的操作?没有任何错误,但您应该将所有内容都放在一个函数中,并使用
[self-performSelector:@selector(fetchData)withObject:nil afterDelay:.2]
要从viewDidLoad调用函数,它应该可以工作。使用未声明的标识符'list\u totalStringd',在我的代码中找到的
list\u totalStringd
就是
list\u totalStrings