проблема с входом в sqlite - PullRequest
       2

проблема с входом в sqlite

0 голосов
/ 06 апреля 2011

Друг, я действительно так сознался, Что мне делать. Вот мой код.

@ синтезировать имя пользователя, пароль; - (void) createDatabaseIfNeeded {

BOOL success;
NSError *error;

 //FileManager - Object allows easy access to the File System.
 NSFileManager *FileManager = [NSFileManager defaultManager];

 //Get the complete users document directory path.
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

 //Get the first path in the array.
 NSString *documentsDirectory = [paths objectAtIndex:0];

    //Create the complete path to the database file.
    NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"Journeymapper.sql"];

    //Check if the file exists or not.
    success = [FileManager fileExistsAtPath:databasePath];

    //If the database is present then quit.
    if(success) return;

    //the database does not exists, so we will copy it to the users document directory]
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Journeymapper.sql"];

    //Copy the database file to the users document directory.
    success = [FileManager copyItemAtPath:dbPath toPath:databasePath error:&error];

    //If the above operation is not a success then display a message.
    //Error message can be seen in the debugger's console window.
    if(!success)
        NSAssert1(0, @"Failed to copy the database. Error: %@.", [error localizedDescription]);
}

-(void)check
{

    //Get the database and connect to it.

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



    //open the database
    if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
    {
        //const char *sql ="select Username@'%@',Password@'%@' from userinformation";
        NSString *sql = [[NSString alloc] initWithFormat:@"select * from UserInformation where UserName='%@' and Password='%@' ",Username.text,Password.text];
        sqlite3_stmt *selectSatement;


        //Preoare the select Statment
        int returnValue = sqlite3_prepare_v2(database, [sql UTF8String], -1, &selectSatement, NULL);
        NSLog(@"%i",returnValue);
        if( sqlite3_prepare_v2(database, [sql UTF8String], -1, &selectSatement, NULL) == SQLITE_OK)
        {

            NSString *user1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectSatement, 0)];
            NSLog(@"%@",user1);
        }
    }
}

-(IBAction)Login
{
    [self check];
}

проблема в том, что здесь: -Я не уверен, почему sqlite3_prepare_v2 не отвечает SQLITE_OK.

if( sqlite3_prepare_v2(database, [sql UTF8String], -1, &selectSatement, NULL) == SQLITE_OK)

Любая помощь с этим очень ценится.

1 Ответ

0 голосов
/ 06 апреля 2011

Почему вы пытаетесь подготовить заявление дважды? Вы должны использовать sqlite3_step () для оценки подготовленного заявления.


Кстати, ваш код уязвим к атакам SQL-инъекций . Вместо того, чтобы подставлять строку использования и пароля в оператор выбора, вы должны использовать sqlite3_bind () , чтобы связать ваши параметры с подготовленным оператором.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...