无法从iOS中的sqlite数据库中检索数据

时间:2013-09-02 06:31:34

标签: ios sqlite

我无法从sqlite数据库中检索数据。数据存在于其中(我通过手动打开我的数据库文件来检查它)。正在成功建立与数据库的连接。我的sqlite3_step语句也返回SQLITE_OK。但是结果集不包含任何内容。 这是代码片段:

 - (void)viewDidLoad
    {
        [super viewDidLoad];

        databaseReferences = [[DatabaseReferences alloc]init];

        const char *newPath = [databasepath UTF8String];
        int opened = sqlite3_open(newPath, &db);

        if(opened == SQLITE_OK)
        {
            NSLog(@"Database for processing login query opened successfully...");
        }
        else
        {
            NSLog(@"Database for processing login query NOT OPENED...");
        }
    }

注意:这里databaseReferences是包含sqlite3类型变量

的类的对象

实际上,在UI方面,用户输入用户名和密码,我将用户输入的字段与我的sqlite数据库中已存在的数据进行匹配。当用户按下LOGIN按钮时。发生以下情况:

-(BOOL)processLoginQueryusername:(NSString *)uname password:(NSString *)pwd
{
    const char *getUsernames = "select Username from LoginTable";
    int outcome = sqlite3_prepare_v2(db, getUsernames, -1, &statement, NULL);

    if(outcome != SQLITE_OK)
    {
        NSLog(@"could not find the usernames");
        NSLog(@"the error is %s", sqlite3_errmsg(db));
    }

    else
    {
        NSLog(@"got the usernames.. Now checking for valid username and password entry..");

        NSLog(@"the number of columns returned is %d", sqlite3_column_count(statement));

        int i = 0;
        while( sqlite3_step(statement)== SQLITE_ROW)
        {
            NSString *name = [NSString stringWithUTF8String:(const char *)sqlite3_column_text(statement, 0)];
            [usernameList addObject:name];
            NSLog(@"the Username list array contains %d names", usernameList.count);
            NSLog(@"the name is %@", [usernameList objectAtIndex:i]);
            i++;
        }

        for(int i = 0; i<usernameList.count; i++)
        {   
            if([uname isEqualToString:[usernameList objectAtIndex:i]])
            {
                NSLog(@"You are an authenticated user..");
                return YES;
            }
        }
        return NO;
    }

    return NO;
}

这是我的AppDelegate方法代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    referenceToDatabase = [[DatabaseReferences alloc]init];
    [referenceToDatabase establishDatabaseConnection];
    [referenceToDatabase release];
    //code for initializing window.. so not including that code
}

referenceToDatabase是包含sqlite3变量的类的对象。这是我叫做的方法:

-(BOOL)establishDatabaseConnection
{
    usernameList = [[NSMutableArray alloc] init];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *dbPath = [paths objectAtIndex:0];
    databaseName = @"TaskManagementDatabase.sql";

    databasepath = [dbPath stringByAppendingPathComponent:databaseName];
    NSLog(@"%@",databasepath);

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if([fileManager fileExistsAtPath:databasepath])
    {
        NSLog(@"the database already exists");
        return YES;
    }

    else
    {
        NSLog(@"database does not exist.. creating the database");
        const char *newPath = [databasepath UTF8String];
        int opened = sqlite3_open(newPath, &db);

        if(opened == SQLITE_OK)
        {
            NSLog(@"newly created database opened successfully...");
            [self createBothTables];
            [self insertInitialDataIntoBothTables];
            sqlite3_close(db);
            return YES;
        }

这是我在控制台上获得的内容:

TaskListManagementApplication [9838:c07]获取了用户名..现在检查有效的用户名和密码输入.. 2013-09-02 11:33:55.134 TaskListManagementApplication [9838:c07]返回的列数为1 2013-09-02 11:33:55.135 TaskListManagementApplication [9838:c07]用户名列表数组包含0个名字 2013-09-02 11:33:55.136 TaskListManagementApplication [9838:c07]名称为(null) 2013-09-02 11:33:55.136 TaskListManagementApplication [9838:c07]用户名列表数组包含0个名字 2013-09-02 11:33:55.136 TaskListManagementApplication [9838:c07]名称为(null) 2013-09-02 11:33:55.137 TaskListManagementApplication [9838:c07]用户名列表数组包含0个名字 2013-09-02 11:33:55.137 TaskListManagementApplication [9838:c07]名称为(null) 2013-09-02 11:33:55.137 TaskListManagementApplication [9838:c07]用户名列表数组包含0个名字 2013-09-02 11:33:55.138 TaskListManagementApplication [9838:c07]名称为(null) 2013-09-02 11:33:55.138 TaskListManagementApplication [9838:c07]用户名列表数组包含0个名字 2013-09-02 11:33:55.138 TaskListManagementApplication [9838:c07]名称为(null)

0 个答案:

没有答案