0

I have a login page where user has to enter Username & password to access the application, for first time user can enter any username & password of user's wish.

First Scenario:

First time i need to insert the login details to the DB

Second Scenario:

And the second time when user is logging in i need to select the user details from the db instead of inserting once again.

The above 2 operations I am doing in login button operation.

Please help me to how to achieve the process

Thanks.

2 Answers 2

2

For this flow of your app you need to create a table with 4 fields.

1.id 2.username 3.password 4.flag(default value is 0)

then add some dump values to username and password field except flag(for flag=0). After that you need to copy that sqliteDB into the app.

for the first time you need to select username password and flag from ur db. then check the value of flag ,if the flag =0 ,then update the values of username,password and flag=1.

In the second time it again check the flag field and goes to the else part ,where you need to compare with the entered username and password in the textfield. if exist ,he is a valid user else he is not a valid user

Sign up to request clarification or add additional context in comments.

Comments

0

First create custom class UserDetails with NSString userName and userPassword.

Then you can try something like this to read data

- (UserDetails *)getUserDetails:(NSString *)userLogin password:(NSString *)userPassword
{
    UserDetails *retVal;

    sqlite3 *database;

        NSString *dbPath = @"your_db_path";

    if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK){

        const char *sql_statement = [[NSString stringWithFormat:@"select * from Users where UserLogin = '%@' and UserPassword = '%@'", userLogin, userPassword] UTF8String];

        sqlite3_stmt *compiled_statement;       

        if(sqlite3_prepare_v2(database, sql_statement, -1, &compiled_statement, NULL) == SQLITE_OK){

            while(sqlite3_step(compiled_statement) == SQLITE_ROW){
                [retVal setUsername:[NSString stringWithUTF8String:(const char *)sqlite3_column_text(compiled_statement, 0)]];

                [retVal setPassword:[NSString stringWithUTF8String:(const char *)sqlite3_column_text(compiled_statement, 1)]];

            }

            sqlite3_finalize(compiled_statement);
            sqlite3_close(database);

        }

    }

    return retVal;
}

And for inserting data you can use almost the same code but without returning values, so method will return void or some control value if you need it and only if you have errors in execution, while sql statement will be something like

Insert into Users(UserLogin, UserPassword) values ('%@', '%@')

2 Comments

but i want the 2 operations to be done in one method or one action? first time insert option should execute, from next time only select operation should work.
then you can add that if the select returns 0 records you perform sql query with inserting same data as new records

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.