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
I need to stablish a sqlite connection in Objective C from XCODE 4.2.
Insert data, Select,Update, Delete.
iPad, iOS 5.1
Same as in town: http://sqlite.org/c3ref/intro.html
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
Try something similar to the following to see what the error message is:
if ( sqlite3_open( [file UTF8String], &database ) != SQLITE_OK )
{
sqlite3_close( database );
NSAssert1( 0, @"Failed to open database with message '%s'.",
sqlite3_errmsg( database ) );
}Shouldn't you be checking against SQLITE_DONE instead?
#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
I've tried with your suggestion, and I recieved this message: 'NSInternalInconsistencyException', reason: 'Failed to open database with message 'not an error'.'
as you can see on screenshots, I got the same message in my DB (SQLite Manager [firefox add-on]), but returns my data, but in my app does not work.
It does make sense?
I have the same problem as kiimbha. if (sqlite3_step(statement) == SQLITE_OK) always results to false :-(
Any help re. the issue will be much appreciated.
Thanks & regards,
Ankit Mahendru
how to make a sqlite connection in xcode 4.2