无法在iOS中从sqlite数据库检索数据
我无法从我的无法在iOS中从sqlite数据库检索数据,ios,sqlite,Ios,Sqlite,我无法从我的sqlite数据库检索数据。数据存在于其中(我通过手动打开数据库文件进行了检查)。正在成功建立到数据库的连接。我的sqlite3\u step语句还返回SQLITE\u OK。但是resultset不包含任何内容。! 下面是代码片段: - (void)viewDidLoad { [super viewDidLoad]; databaseReferences = [[DatabaseReferences alloc]init];
sqlite
数据库检索数据。数据存在于其中(我通过手动打开数据库文件进行了检查)。正在成功建立到数据库的连接。我的sqlite3\u step语句还返回SQLITE\u OK。但是resultset不包含任何内容。!
下面是代码片段:
- (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端,用户输入用户名和pwd,我将用户输入的字段与sqlite数据库中已有的数据进行匹配。当用户按下登录按钮时。发生以下情况:
-(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;
}
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任务列表管理应用程序[9838:c07]返回的列数为1
2013-09-02 11:33:55.135任务列表管理应用程序[9838:c07]用户名列表数组包含0个名称
2013-09-02 11:33:55.136任务列表管理应用程序[9838:c07]名称为(空)
2013-09-02 11:33:55.136任务列表管理应用程序[9838:c07]用户名列表数组包含0个名称
2013-09-02 11:33:55.136任务列表管理应用程序[9838:c07]名称为(空)
2013-09-02 11:33:55.137任务列表管理应用程序[9838:c07]用户名列表数组包含0个名称
2013-09-02 11:33:55.137任务列表管理应用程序[9838:c07]名称为(空)
2013-09-02 11:33:55.137任务列表管理应用程序[9838:c07]用户名列表数组包含0个名称
2013-09-02 11:33:55.138任务列表管理应用程序[9838:c07]名称为(空)
2013-09-02 11:33:55.138任务列表管理应用程序[9838:c07]用户名列表数组包含0个名称
2013-09-02 11:33:55.138任务列表管理应用程序[9838:c07]名称为(空)您可能没有初始化
用户名列表
数组。是。。我已经初始化了数组显示了初始化数组的代码。并确保在调用processLoginQueryusername
之前初始化数组。。我尝试了一些东西,它奏效了。。我在processLoginQueryusername:password:….中的while循环之前初始化了数组。。。。它成功了。。!!!我猜我之前的初始化有问题。顺便说一句,在从processLoginQueryusername
返回之前,不要忘记调用sqlite3\u finalize
。
-(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;
}