开发者

problem with sqlite login

Friend I am really so confessed WHat should i do. Here is my code .

@synthesize Username,Password; -(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];
}

problem is that here :-Im not sure why sqlite3_prepare_v2 isn't meeting开发者_开发问答 SQLITE_OK.

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

Any help with this is greatly appreciated.


Why are you trying to prepare the statement twice? You should be using sqlite3_step() to evaluate your prepared statement.


On a side note, your code is vulnerable to SQL injection attacks. Instead of substituting the use and password string into the select statement, you should be using sqlite3_bind() to bind your parameters to the prepared statement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜