how to make a sqlite connection in xcode 4.2

I need to stablish a sqlite connection in Objective C from XCODE 4.2.

Insert data, Select,Update, Delete.

iPad, iOS 5.1

Posted on Apr 19, 2012 9:57 AM

Reply
6 replies

Apr 19, 2012 10:52 AM in response to etresoft

Hi,

here is my code, but it seems the connection has been stablished, but when try to execute queries it does not work!.. I really do not know the reason.

my problem is this part:

if (sqlite3_step(statement) == SQLITE_OK) -----> IS FALSE ALWAYS!


PS: I'm rookie is this.. please help!


******************************************************************************** *****************************



#import "ViewController.h"





@interface ViewController ()



@end



@implementation ViewController

@synthesize name, address, phone, status;





- (void)viewDidLoad

{

[super 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: @"users.db"]];

NSFileManager *filemgr = [NSFileManager defaultManager];


if ([filemgr fileExistsAtPath: databasePath ] == YES)

{

const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)

{

status.text = @"Successfully Connected";

char *errMsg;

const char *sql_stmt = "CREATE TABLE IF NOT EXISTS userInfo (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, ADRRESS TEXT, PHONE TEXT)";



if(sqlite3_exec(contactDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)

{

status.text = @"Failed to create a table";

}



sqlite3_close(contactDB);

}

else

{

status.text = @"Failed to open/create database";

}

[filemgr release];

}

}



- (void)viewDidUnload

{

[super viewDidUnload];

self.name = nil;

self.address = nil;

self.phone = nil;

self.status = nil;

}



-(void)dealloc

{

[name release];

[address release];

[phone release];

[status release];

[super dealloc];

}





















-(void) SaveData

{

sqlite3_stmt *statement;

const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)

{

NSString *insertSQL= [NSString stringWithFormat: @"INSERT INTO userInfo(name, address, phone) VALUES (\"%@\", \"%@\", \"%@\")",

name.text, address.text,phone.text];



const char *insert_stmt = [insertSQL UTF8String];

sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);

if (sqlite3_step(statement) == SQLITE_OK)

//if (sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL) == SQLITE_OK)

{

status.text = @"Contact added";

name.text = @"";

address.text = @"";

phone.text = @"";

}

else

{

status.text = @"Failed to add contacts";

}

sqlite3_finalize(statement);

sqlite3_close(contactDB);

}

}



-(void) findContact

{

const char *dbpath = [databasePath UTF8String];

sqlite3_stmt *statement;

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)

{

status.text = @"abrio la conexion";

NSString *querySQL;

querySQL = [NSString stringWithFormat:@"SELECT address, phone FROM userInfo WHERE name=\"%@\"", name.text];

const char *query_stmt = [querySQL UTF8String];



if (sqlite3_prepare16_v2(contactDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)

{

status.text = @"ejecuto query";

if (sqlite3_step(statement) == SQLITE_ROW)

{

NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];

address.text = addressField;

NSString *phoneField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];

phone.text = phoneField;

status.text = @"Match found";

[addressField release];

[phoneField release];

}

else {

status.text = @"Match NOT found";

address.text = @"";

phone.text = @"";

}

sqlite3_finalize(statement);

}

sqlite3_close(contactDB);

}



}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceO rientation

{

return YES;

}



@end

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

how to make a sqlite connection in xcode 4.2

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.