How to Compare two rows in Sqlite3 table? - authentication

Here i m trying to compare details of rows values in consisting table.here my table contents [here my table
][1]
and also here my code
`-
(IBAction)LogIn:(id)sender {
sqlite3 *dbConnection;
const char *dbpath = [[sharedobj getSandboxPath] UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &dbConnection) == SQLITE_OK)
{
NSString *querySQL = #"SELECT USERNAME, PASSWORD FROM NEWVALUES";
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(dbConnection,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *emailId = [[NSString alloc]
initWithUTF8String:
(const char *) sqlite3_column_text(statement,0)];
NSString *password = [[NSString alloc]
initWithUTF8String:(const char *)
sqlite3_column_text(statement, 1)];
if ([emailId isEqualToString:self.userNameField.text] || [password isEqualToString:self.passwordField.text]) {
NSLog(#"Login Credentials Correct");
CHECKViewController *checkobj=[self.storyboard instantiateViewControllerWithIdentifier:#"CHECK"];
[self.navigationController pushViewController:checkobj animated:YES];
}
else{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Login Credentials are Incorrect"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:#"OK ", nil];
[alert show];
}
}
NSLog(#"Login Details %# %#",self.userNameField.text ,self.passwordField.text );
}
sqlite3_finalize(statement);
}
else{
NSLog(#"SQLITE NOT OK");
NSLog(#"Error %s ", sqlite3_errmsg(dbConnection));
}
sqlite3_close(dbConnection);
}
imgur.com/QW6KX.png
when enter first row values it working but when i enter second row values it not comparing .what i m doing wrong here?
Please give suggestions to fix this issue.
thanks in advance.

Related

Database is locked sqlite ios

I write a application have use sqlite save data. I add data to table success. But i can't delete content in table. It notify DATABASE IS LOCKED.
My code:
Code Delete in sqlite
//Xoá tài khoản
-(BOOL) delete_Account
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
const char *delete_Statement = "DELETE FROM info";
if(sqlite3_prepare_v2(database, delete_Statement,-1, &statement, NULL)==SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(#"DELETE Account : %#", #"Ban da xoa thanh cong!");
return YES;
}
else{
NSLog(#"DELETE Account: %# %s",#"Xoa that bai",sqlite3_errmsg(database));
}
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
return NO;
}
Full code
#import "DBManager.h"
static DBManager *sharedInstance = nil;
static sqlite3 *database = nil;
static sqlite3_stmt *statement = nil;
#implementation DBManager
+(DBManager*)getSharedInstance{
if (!sharedInstance) {
sharedInstance = [[super allocWithZone:NULL]init];
[sharedInstance createDB];
}
return sharedInstance;
}
-(BOOL)createDB{
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = dirPaths[0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString:
[docsDir stringByAppendingPathComponent: #"Sync.db"]];
BOOL isSuccess = YES;
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
char *errMsg;
const char *sql_statement = "CREATE TABLE IF NOT EXISTS info (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, PASS TEXT, TOKEN TEXT, COMPANY TEXT, CUSTOMER TEXT)";
const char*sql_create_contact_db = "CREATE TABLE IF NOT EXISTS contact (ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRST TEXT, LAST TEXT, PHONE TEXT, EMAIL TEXT)";
if (sqlite3_exec(database, sql_statement, NULL, NULL, &errMsg)
!= SQLITE_OK)
{
isSuccess = NO;
NSLog(#"Failed to create table INFO");
}
if (sqlite3_exec(database, sql_create_contact_db, NULL, NULL, &errMsg)
!= SQLITE_OK)
{
isSuccess = NO;
NSLog(#"Failed to create table CONTACT");
}
sqlite3_finalize(statement);
sqlite3_close(database);
return isSuccess;
}
else {
isSuccess = NO;
NSLog(#"Failed to open/create database");
}
}
return isSuccess;
}
-(BOOL) add_Data_Info:(NSString *)username :(NSString *)password :(NSString *)token
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:#"INSERT INTO info (name, pass, token, company, customer) VALUES (\"%#\",\"%#\",\"%#\",\"%#\",\"%#\")", username, password, token, #"ON", #"OFF"];
const char *insert_Statement = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_Statement,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
return YES;
}
else {
return NO;
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
return NO;
}
//Thêm data vào database
-(BOOL) add_Data_Sync:(NSString *)first :(NSString *)last :(NSString *)phone : (NSString*)email
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat:#"INSERT INTO contact (first,last,phone,email) VALUES (\"%#\",\"%#\",\"%#\",\"%#\")", first, last, phone, email];
const char *insert_Statement = [insertSQL UTF8String];
sqlite3_prepare_v2(database, insert_Statement,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
return YES;
}
else {
return NO;
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
return NO;
}
//Cập nhật token
-(BOOL) update_Data_Info:(NSString *)username : (NSString *) token
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *updateSQL = [NSString stringWithFormat:#"UPDATE info Set token = \"%#\"where name = \"\"%#\")", token, username];
const char *update_Statement = [updateSQL UTF8String];
sqlite3_prepare_v2(database, update_Statement,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
return YES;
}
else {
return NO;
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
return NO;
}
//Cập nhật token
-(BOOL) update_Sw_Info:(NSString *)username : (NSString *) status
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *updateSQL = [NSString stringWithFormat:#"UPDATE info Set company = \"%#\" where name = \"%#\" ", status, username];
const char *update_Statement = [updateSQL UTF8String];
sqlite3_prepare_v2(database, update_Statement,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
return YES;
}
else {
return NO;
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
return NO;
}
//Xoá tài khoản
-(BOOL) delete_Account
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
const char *delete_Statement = "DELETE FROM info";
if(sqlite3_prepare_v2(database, delete_Statement,-1, &statement, NULL)==SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(#"DELETE Account : %#", #"Ban da xoa thanh cong!");
return YES;
}
else{
NSLog(#"DELETE Account: %# %s",#"Xoa that bai",sqlite3_errmsg(database));
}
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
return NO;
}
-(BOOL)delete_All_Contact
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *deleteSQL = [NSString stringWithFormat:#"DELETE FROM contact"];
const char *delete_Statement = [deleteSQL UTF8String];
sqlite3_prepare_v2(database, delete_Statement,-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_ROW)
{
return YES;
}
else {
return NO;
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
return NO;
}
- (NSArray*) getInfo
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
#"SELECT name, token, pass, company, customer FROM info"];
const char *query_stmt = [querySQL UTF8String];
NSMutableArray *resultArray = [[NSMutableArray alloc]init];
if (sqlite3_prepare_v2(database,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *name = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 0)];
[resultArray addObject:name];
NSString *token = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 1)];
[resultArray addObject:token];
NSString *pass = [[NSString alloc]initWithUTF8String:
(const char *) sqlite3_column_text(statement, 2)];
[resultArray addObject:pass];
NSString *company = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 3)];
[resultArray addObject:company];
NSString *customer = [[NSString alloc]initWithUTF8String:
(const char *) sqlite3_column_text(statement, 4)];
[resultArray addObject:customer];
return resultArray;
}
else{
NSLog(#"Not found");
return nil;
}
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
return nil;
}
//Kiểm tra dữ liệu trong database
-(int)checkContact : (NSString *) txtPhone
{
int check = 0;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat:
#"SELECT COUNT(*) FROM contact WHERE phone = \"%#\"",txtPhone];
const char *query_stmt = [querySQL UTF8String];
NSMutableArray *resultArray = [[NSMutableArray alloc]init];
if (sqlite3_prepare_v2(database,
query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{
check = 1;
}
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
return check;
}
#end

Sqlite3 step == SQLITE_ROW not working for me

As i am new to Xcode so no idea to fix this issue. Please help me out to figure the actual issue.
Database connection is build up, the query is execute, but I always get the message as : Result not found, after execution the query. :(
- (void)viewDidLoad
{
NSString *MyDirectory;
NSArray *DirectoryPaths;
// Get the documents directory
DirectoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
MyDirectory = [DirectoryPaths objectAtIndex:0];
// Build the path to the database file
MyDBPath = [[NSString alloc]initWithString: [MyDirectory stringByAppendingPathComponent:#"mydb.db"]];
//Status.text = MyDBPath;
const char *Database = [MyDBPath UTF8String];
if (sqlite3_open(Database, &MyConnection) == SQLITE_OK)
{
Status.text = #"Build Connection Successfully...!";
}
else
{
Status.text = #"Failed to open database...!";
}
[super viewDidLoad];
}
- (void) Find
{
const char *Database = [MyDBPath UTF8String];
sqlite3_stmt *SQLStatement;
NSString *Query;
if (sqlite3_open(Database, &MyConnection) == SQLITE_OK)
{
Query = [NSString stringWithFormat:#"SELECT EmpName FROM EmpLogin WHERE EmpID=\"%#\"",MyText.text];
if (sqlite3_prepare_v2(MyConnection, [Query UTF8String], 0, &SQLStatement, nil) == SQLITE_OK)
{
while (sqlite3_step(SQLStatement) == SQLITE_ROW)
{
NSString *Name = [[NSString alloc]
initWithUTF8String:(const char *) sqlite3_column_text(SQLStatement, 0)];
TextResult.text = Name;
}
if (TextResult.text.length > 0)
{
Status.text = #"Result found";
}
else
{
NSLog(#"%s", sqlite3_errmsg(MyConnection));
Status.text = #"Result not found";
TextResult.text = #"";
}
sqlite3_finalize(SQLStatement);
}
else
{
//NSString *decode = [[NSString alloc]initWithCString:Query_Stmt encoding:NSUTF8StringEncoding];
//Status.text = decode;
Status.text = #"Query execution Failed...!";
}
sqlite3_close(MyConnection);
}
}
Sqlite3_step(SQLStatement) == SQLITE_ROW __ nor working
Yes i resolved the issue…!!!!
actually i just change the path of the directory to direct to my DB path.
here is my final and working code… anyway thanks #HotLicks,,, #CL. :) :)
This works like a charm … :)
- (void) Find
{
MyDBPath = #"/Users/zaibi/Documents/IOSProjects/mydb.db";
MyDatabase = [MyDBPath UTF8String];
sqlite3_stmt *SQLStatement;
NSString *Query;
//NSString *decode = [[NSString alloc]initWithCString:Database encoding:NSUTF8StringEncoding];
//NSLog(#"%#",decode);
if (sqlite3_open(MyDatabase, &MyConnection) == SQLITE_OK)
{
Query = [NSString stringWithFormat:#"SELECT EmpName FROM EmpLogin WHERE EmpID=\"%#\"",MyText.text];
if (sqlite3_prepare_v2(MyConnection, [Query UTF8String], -1, &SQLStatement, nil) == SQLITE_OK)
{
if (sqlite3_step(SQLStatement) == SQLITE_ROW)
{
NSString *Name = [[NSString alloc]
initWithUTF8String:(const char *) sqlite3_column_text(SQLStatement, 0)];
TextResult.text = Name;
Status.text = #"Result found";
}
else
{
NSLog(#"%s", sqlite3_errmsg(MyConnection));
Status.text = #"Result not found";
TextResult.text = #"";
}
sqlite3_finalize(SQLStatement);
}
else
{
NSLog(#"%s", sqlite3_errmsg(MyConnection));
Status.text = #"Query execution Failed...!";
}
sqlite3_close(MyConnection);
}
}
while this way of accessing the DB is just confusing… :/
NSString *MyDirectory;
NSArray *DirectoryPaths;
// Get the documents directory
DirectoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
MyDirectory = [DirectoryPaths objectAtIndex:0];
// Build the path to the database file
MyDBPath = [[NSString alloc]initWithString: [MyDirectory stringByAppendingPathComponent:#"mydb.db"]];
//Status.text = MyDBPath;
Sometimes it helps to read the documentation:
If the nByte argument is less than zero, then zSql is read up to the
first zero terminator. If nByte is non-negative, then it is the
maximum number of bytes read from zSql.

Cannot achieve a successful sqlite query to display in my text view

I'm trying to display a string of text in a text view. According to NSLog the database is found but, I cannot retrieve the data from the table. Could someone point out what is wrong with my query? (I may need this explained in very simple terms. I've been sing objective-c for only a few days.)
- (void)viewDidLoad {
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:#"trivia_game.db"]];
NSLog(#"Full DB path: %#", databasePath);
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
NSLog(#"[SQLITE] DB not found");
} else {
NSLog(#"[SQLITE] trivia_game.db found");
}
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &questionsForGame) == SQLITE_OK)
{
NSLog(#"[SQLITE] db opened");
NSString *querySQL = [NSString stringWithFormat: #"SELECT question, answer0, answer1, answer2, answer3 FROM questions"];
NSLog(#"[SQLITE] string is: %#", querySQL);
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(questionsForGame, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
NSLog(#"[SQLITE] written!");
if (sqlite3_step(statement) == SQLITE_ROW)
{
NSString *textField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
questHolder.text = textField;
NSLog(#"[SQLITE] string is: %#",textField);
[textField release];
} else {
questHolder.text = #"query not found";
NSLog(#"[SQLITE] Unable to open database!");
}
sqlite3_finalize(statement);
sqlite3_close(questionsForGame);
} else {
NSLog(#"[SQLITE] Screwed up query!");
questHolder.text = #"query not found";
}
}
[filemgr release];
[super viewDidLoad];
}
log:
[Session started at 2012-05-28 12:27:19 -0300.]
2012-05-28 12:27:20.547 ans[9374:207] Full DB path: /Users/admin/Library/Application Support/iPhone Simulator/4.3/Applications/0B4C50FB-5A3F-4371-83D6-1A2AF95B9F66/Documents/trivia_game.db
2012-05-28 12:27:20.549 ans[9374:207] [SQLITE] trivia_game.db found
2012-05-28 12:27:20.550 ans[9374:207] [SQLITE] db opened
2012-05-28 12:27:20.551 ans[9374:207] [SQLITE] string is: SELECT question, answer0, answer1, answer2, answer3 FROM questions
2012-05-28 12:27:20.553 ans[9374:207] [SQLITE] Screwed up query!
I am not familiar with the SQLite library you're using but I would recommend to switch to Core Data. cf. http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/coredata/cdprogrammingguide.html

NSImage to blob and blob to NSImage (SQLite, NSData)

First of all, I know that I should not use an SQLite database to store image, but I only store favicon of website which are really small.
My problem is that I try to insert those favicon into the database (seems to work), I convert the favicon to NSData with the -tiffrepresentation method of NSimage and then insert it to my database into a blob column:
NSImage *favico = [webview mainFrameIcon];
[appDelegate insertBookmark:[titleField stringValue] url:[urlfield stringValue] data:[favico TIFFRepresentation]]
The SQLite method look like this:
-(void)insertBookmark:(NSString *)title url:(NSString *)url data:(NSData *)data
{
NSData *imagedata = [[NSData alloc]initWithData:data];
sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSString *query = [NSString stringWithFormat:#"INSERT INTO Bookmarks (title, url, image) VALUES ('%#', '%#', '%#')",title, url, data];
const char *querychar = [query UTF8String];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, querychar, -1, &statement, NULL) == SQLITE_OK)
{
int row = 3;
sqlite3_bind_blob(statement, row, [imagedata bytes], [imagedata length], NULL);
sqlite3_step(statement);
sqlite3_finalize(statement);
}
else
{
NSLog(#"Error");
}
[databasePath retain];
[databaseName retain];
}
sqlite3_close(database);
[imagedata release];
}
When I look into the database I have value in the image column between <data> (normal ? )
Now when I extract the blob from the database and try to put it in my object I got null in the NSimage:
-(void) readDatabase {
// Setup the database object
sqlite3 *database;
bookmarks = [[NSMutableArray alloc] init];
// Open the database from the users filessytem
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = "SELECT * FROM Bookmarks ORDER BY title ASC";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSString *aTitle = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aUrl = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSData *data = [[NSData alloc] initWithBytes:sqlite3_column_blob(compiledStatement, 3) length:sqlite3_column_bytes(compiledStatement, 3)];
NSImage *image = [[NSImage alloc]initWithData:data];
bookmarkObject *bookmark = [[bookmarkObject alloc] initWithName:aTitle url:aUrl favico:image];
[bookmarks addObject:bookmark];
[bookmark release];
[data release];
[image release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
sqlite3_close(database);
[databasePath retain];
[databaseName retain];
}
}
Thanks in advance
The < and > around your actual data come from NSData. By using %# in your format string and providing data, NSString sends description to data. [NSData description] wraps the content between < and >.
Another issue with your code is, that you seem to mix parameter-less & parameter prepared statements:
Either use stringWithFormat: to create a complete query statement
or use a query like INSERT INTO Bookmarks (title ...) VALUES (? ...) combined with sqlite3_bind_blob
The parameter syntax SQLite supports can be found here:
http://www.sqlite.org/c3ref/bind_blob.html

Problem fetching more than the first row from SQLite DB

I am creating an application which first checks whether the user is valid or not by checking data from an SQLite database. I have created a table named loginchk in the database with three rows and two columns in it, uname and pass.
When I run the application and enter the username and password values in my text fields, it checks if the username and password are in the database. If they are not, it will present an alert view that says "Please enter correct username".
In my code there is a problem: it reads only the first row, i.e., when I enter the username and password values that are in the first row it succeeds, and all the other values that I enter do not work.
What could be the problem?
Thanks.
#import "loginAppDelegate.h"
#import "global.h"
#import <sqlite3.h>
#import "logincontroller.h"
#implementation loginAppDelegate
#synthesize window;
#synthesize loginView;
//databaseName=#"login.sqlite";
-(void) chekAndCreateDatabase
{
BOOL success;
//sqlite3 *databaseName=#"login.sqlite";
NSFileManager *fileManager=[NSFileManager defaultManager];
NSArray *documentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir =[documentPaths objectAtIndex:0];
NSString *databasePath=[documentsDir stringByAppendingPathComponent"login.sqlite"]; success=[fileManager fileExistsAtPathatabasePath];
if(success)return;
NSString *databasePathFromApp=[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent"login.sqlite"];
[fileManager copyItemAtPathatabasePathFromApp toPathatabasePath error:nil]; [fileManager release];
}
-(void) Data
{
Gpass=#"";
Guname=#"";
sqlite3_stmt *detailStmt=nil;
//sqlite3 *databaseName;
NSArray *documentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir =[documentPaths objectAtIndex:0];
NSString *databasePath=[documentsDir stringByAppendingPathComponent{angry_smile}"login.sqlite"];
[self chekAndCreateDatabase];
sqlite3 *database;
if (sqlite3_open([databasePath UTF8String],&database)==SQLITE_OK)
{
if (detailStmt==nil)
{
const char *sql= "select *from Loginchk"; //where uname='%?'and password='%?'"; //NSString *sql = [[NSString alloc] initWithFormat{angry_smile}"SELECT * FROM Loginchk WHERE uname ='%#' and password ='%#' ",Uname.text,Password.text];
if (sqlite3_prepare_v2(database,sql,-1,&detailStmt,NULL)==SQLITE_OK)
{
sqlite3_bind_text(detailStmt,1,[Gunameq UTF8String],-1,SQLITE_TRANSIENT);
sqlite3_bind_text(detailStmt,2,[Gpassq UTF8String],-1,SQLITE_TRANSIENT);
if (SQLITE_DONE!= sqlite3_step(detailStmt))
{
Guname=[NSString stringWithUTF8Stringchar*)sqlite3_column_text(detailStmt,0)];
Gpass =[NSString stringWithUTF8Stringchar*)sqlite3_column_text(detailStmt,1)];
NSLog(#"'%#'",Guname);
NSLog(#"'%#'",Gpass); }
} sqlite3_finalize(detailStmt);
}
} sqlite3_close(database);
}
This my login controller; here I am calling my app delegate's function data:
-(IBAction)buttonPressed:(id)sender
{
Gpassq=Password.text;
Gunameq=Uname.text;
NSLog(#"%#%#",Gunameq,Gpassq);
loginAppDelegate *appDelegate =(loginAppDelegate *)[[UIApplication sharedApplication]delegate];
[appDelegate Data];
if ([Uname.text isEqualToString:Guname]&&[Password.text isEqualToString:Gpass])
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"UIAlertView" message:#"Success" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:#"UIAlertView" message:#"PleaseEnterCorrectUserName and password" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
change
if (SQLITE_DONE!= sqlite3_step(detailStmt))
with
while (SQLITE_DONE!= sqlite3_step(detailStmt))
Update 1
use this code -
-(void) Data
{
Gpass=#"";
Guname=#"";
sqlite3_stmt *detailStmt=nil;
//sqlite3 *databaseName;
NSArray *documentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir =[documentPaths objectAtIndex:0];
NSString *databasePath=[documentsDir stringByAppendingPathComponent:#"login.sqlite"];
[self chekAndCreateDatabase];
sqlite3 *database;
if (sqlite3_open([databasePath UTF8String],&database)==SQLITE_OK)
{
const char *sql= "select *from Loginchk"; //where uname='%?'and password='%?'"; //NSString *sql = [[NSString alloc] initWithFormat{angry_smile}"SELECT * FROM Loginchk WHERE uname ='%#' and password ='%#' ",Uname.text,Password.text];
sqlite3_stmt *detailStmt;
if (sqlite3_prepare_v2(database,sql,-1,&detailStmt,NULL)==SQLITE_OK)
{
while(sqlite3_step(detailStmt) == SQLITE_ROW)
{
Guname=[NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt,0)];
Gpass =[NSString stringWithUTF8String:(char *)sqlite3_column_text(detailStmt,1)];
NSLog(#"'%#'",Guname);
NSLog(#"'%#'",Gpass);
}
}
sqlite3_finalize(detailStmt);
sqlite3_close(database);
}
}
Try this
\-(void) Data
{
Gpass=#"";
Guname=#"";
sqlite3_stmt *detailStmt=nil;
//sqlite3 *databaseName;
NSArray *documentPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir =[documentPaths objectAtIndex:0];
NSString *databasePath=[documentsDir stringByAppendingPathComponent{angry_smile}"login.sqlite"];
[self chekAndCreateDatabase];
sqlite3 *database;
if (sqlite3_open([databasePath UTF8String],&database)==SQLITE_OK)
{
if (detailStmt==nil)
{
const char *sql= "select *from Loginchk"; //where uname='%?'and password='%?'"; //NSString *sql = [[NSString alloc] initWithFormat{angry_smile}"SELECT * FROM Loginchk WHERE uname ='%#' and password ='%#' ",Uname.text,Password.text];
if (sqlite3_prepare_v2(database,sql,-1,&detailStmt,NULL)==SQLITE_OK)
{
sqlite3_bind_text(detailStmt,1,[Gunameq UTF8String],-1,SQLITE_TRANSIENT);
sqlite3_bind_text(detailStmt,2,[Gpassq UTF8String],-1,SQLITE_TRANSIENT);
// Loop through the results
while(sqlite3_step(detailStmt) == SQLITE_ROW) {
{
Guname=[NSString stringWithUTF8Stringchar*)sqlite3_column_text(detailStmt,0)];
Gpass =[NSString stringWithUTF8Stringchar*)sqlite3_column_text(detailStmt,1)];
NSLog(#"'%#'",Guname);
NSLog(#"'%#'",Gpass); }
} sqlite3_finalize(detailStmt);
}
} sqlite3_close(database);
}