我无法从我的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 数据库中已经存在的数据进行匹配。当用户按下登录按钮时。发生以下情况:
-(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)