Sqlite3 table primary key gets replaced - objective-c

I am having SQLite database where I have one table called tblUser,where I am having variables say Id,name,image,gender.
In my app,I am giving functionality to add,delete update the User entry (Common functionality). Say I have 2 record by default on the page with 2 users and that both are deleted. Now the 3rd record inserted will be provided Primary key as 3.
Now if I edit that particular record then it is not updated with new data rather the primary key of the same gets replaced with 1 when I call following function :
+ (void) getInitialDataToDisplay:(NSString *)dbPath
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//sqlite3_open([dbPath UTF8String], &database);
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "select UserId,UserAge,UserName,UserGender,UserImage from [tblUser]";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSInteger primaryKey = sqlite3_column_int(selectstmt,0);
UserDetail *DataObj = [[UserDetail alloc] initWithPrimaryKey:primaryKey];
DataObj.UserAge = sqlite3_column_int(selectstmt,1);
DataObj.UserName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
DataObj.UserGender = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
DataObj.UserImage = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 4)];
DataObj.isDirty = NO;
[appDelegate.DataArray addObject:DataObj];
}
}
else
{
NSAssert1(0,#"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
Note that I am calling this function when I come back to the page where listing of the user is shown after clicking save button of the page where all details need to be filled. Any hint will be much helpful. Thank you.

Assuming that UserId is an INTEGER PRIMARY KEY:
SQLite will create new IDs as one larger than the previously largest value,
but if the table is empty, it will (re)start at 1.

Related

Difficulty in accessing the db records

I'm trying to add SQLite support in my app but I had a problem, so I tried to search something on the Internet and I found a tutorial. But the problem is that the db used by the tutorial is read by the app but when I add my personal db (I've modified the code) it is not read. Any suggestions?
This is (part of) the code:
static sqlite3 *database = nil;
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select id,name from myDb";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
id = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:id, #"id", name, #"nome", nil];
[dictionary release];
}
}
else{
sqlite3_close(database);
}
Before the else statement you need to release the resources:
// "Finalize" the statement - releases the resources associated with the statement.
sqlite3_finalize(selectstmt);
And what is the purpose of the release statement?

xCode sqlite query

This must be a stupid problem but i cant get my query to work. Its as if my database is empty (I double checked, its not). Its a simple SELECT * FROM table query. This is how i try it:
+(MyDatabase *)database{
if (database == nil) {
database = [[MyDatabase alloc] init];
}
return database;
}
- (id)init
{
self = [super init];
if (self) {
NSString *sqliteDb = [[NSBundle mainBundle] pathForResource:#"personsDB" ofType:#"sqlite3"];
if (sqlite3_open([sqliteDb UTF8String], &database) != SQLITE_OK) {
NSLog(#"Fail to open Database.");
}
}
return self;
}
and
-(NSArray *)getAllRows{
NSMutableArray *retArray = [[NSMutableArray alloc] init];
NSString *query = #"SELECT * FROM persons";
sqlite3_stmt *statement;
NSLog(#"checking SQLITE_OK?");
if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
NSLog(#"SQLITE_OK");
while (sqlite3_step(statement) == SQLITE_ROW) {
int personID = sqlite3_column_int(statement, 0);
char *personChars = (char *) sqlite3_column_text(statement, 1);
int gender = sqlite3_column_int(statement, 2);
int related = sqlite3_column_int(statement, 3);
NSString *person = [[NSString alloc] initWithUTF8String:personChars];
MyDBInfo *info = [[MyDBInfo alloc] initWithpersonID:personID person:person gender:gender related:related ];
[retArray addObject:info];
}
sqlite3_finalize(statement);
}
return retArray;
}
I think this is all the interesting stuff.
In my Log I get checking SQLITE_OK?, but no SQLITE_OK. I'm not getting Fail to open Database. so I'm assuming that its all good there.
My Database is full and there is a table called persons. I'm very new to sqlite in iOS apps.
Thank you.
Are you sure the db is included in your bundle? The default behavior of sqlite3_open is SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, so it will be created on your device/simulator if it wasn't there already, so you won't see Fail to open Database. Use sqlite3_open_v2 if you don't want SQLITE_OPEN_CREATE. Anyway, I sometimes have added files to my xcode project and they're not automatically included in my bundle. Select the target in the top of the project navigator, click on the target, select the target in the main panel, select "Build Phases", and see if your db is included in the "Copy Bundle Resources".
Probably unrelated, but I always copy the database from the bundle to documents folder (if it's not there already).
If I don't receive the expected SQLITE_OK, I always look at my return code or log my error message and error codes (and this would probably report that the table in question was not found, which would have let you identify the problem).
Thus,
NSLog(#"%s db err '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(contactDB), sqlite3_errcode(contactDB));

Cannot select from sqlite3 database

I have some problem with reading from sqlite3 database.
-(void) readMessengesFromDatabase {
sqlite3 *database;
messenges = [[NSMutableArray alloc] init];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSLog(#"Connection OK");
const char *sqlStatement = "select * from MessagesData";
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) NSLog(#"connect to table OK"); else NSLog(#"connect to table FALSE");
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { //не проходит условие
NSLog(#"Connection to table OK");
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSLog(#"Read rows OK");
NSString *dbMessageID = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
NSString *dbMessageText = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *dbMessageDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
NSString *dbMediaOrNot = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
Message *messege = [[Message alloc] initWithName:dbMessageText messageID:dbMessageID messageDate:dbMessageDate mediaOrNot:dbMediaOrNot];
[messenges addObject:messege];
[messege release];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
}
My first NSLog shows me what connection to database is ok. But next step "select * from MessagesData" and if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) NSLog(#"connect to table OK"); else NSLog(#"connect to table FALSE"); shows me "connect to table FALSE". Tried select from my database's table whith Terminal and got error "unable to open database file" . Where is my mistake? I don't see any problems in my code...
If you print the error code returned by sqlite3_prepare_v2 it will be a lot easier to diagnose the problem. The numeric values can be found on this page.
int errorCode = sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL);
if(errorCode != SQLITE_OK) {
NSLog(#"Connect to table failed: %d", errorCode);
}
However, if you cannot even select from the database in the sqlite3 command line tool, I suggest you check that the file exists, is readable and in the correct format.
Try to reproduce the error in your simulator (failing that, copy the database file to your computer e.g. using Organizer). Try to run the query using sqlite3 (I know you did try that, but make sure you are checking the following).
If you are getting the message Error: file is encrypted or is not a database, it means that the database file is corrupt. If you get Error: no such table:, it means your database does not exist, is empty, or simply hasn't got the table. If you get (as in your question): Error: unable to open database (you get this during the opening of sqlite, not when performing the query), it means that sqlite cannot read the file (for example permissions).
well i m guessing ur Database is not being able to connect.
Try My Answer From Here...
I mentioned few things here and i believe u ll be just Fine
Let me know it worked
write a code for check that table exist or not. and before u execute the query select u should check that create table if not exists ....
NSLog(#"Connection OK");
sqlite3_exec(database,"CREATE TABLE IF NOT EXISTS MessagesData (ID INTEGER PRIMARY KEY AUTOINCREMENT ,"
"FirstName TEXT,LastName TEXT"));
const char *sqlStatement = "select * from MessagesData";
If it work please like it

SQLite doesn't compile query - Objective C

I have problems with SQLite function sqlite3_prepare_v2, it always returns 1 error code. I'm using SQLite wrapper SQLitemanagerforIOS4. Previously the same error happened without using the wrapper, I switched to it because, despite the statement was encoded in UTF8, the error still happened. I checked the database path with the debugger, and it's correct, so I'm lost...By the way, database is correctly opened and closed.
Here it is the pice of code :
- (NSArray *)getRowsForQuery:(NSString *)sql {
NSMutableArray *resultsArray = [[NSMutableArray alloc] initWithCapacity:1];
if (db == nil) {
[self openDatabase];
}
sqlite3_stmt *statement;
const char *query = [sql UTF8String];
int prepareStatus = sqlite3_prepare_v2(db, query, -1, &statement, NULL);
while (sqlite3_step(statement) == SQLITE_ROW) {
Many thanks for your help.
Here they are the parameters passed to the wrapper object:
- (void)viewDidLoad
{
[super viewDidLoad];
dbManager = [[SQLiteManager alloc] initWithDatabaseNamed:#"XLO.sqlite"];
SArray *provinciaArray = [dbManager getRowsForQuery:[NSString stringWithFormat:#"SELECT provincia FROM provincias;"]];
Thanks!
Peter, here it is:
- (NSError *) openDatabase {
NSError *error = nil;
NSString *databasePath = [self getDatabasePath];
const char *dbpath = [databasePath UTF8String];
#ifdef DEBUG
NSLog(#"SQL result: <%s>", dbpath );
#endif
int result = sqlite3_open(dbpath, &db);
if (result != SQLITE_OK) {
const char *errorMsg = sqlite3_errmsg(db);
NSString *errorStr = [NSString stringWithFormat:#"The database could not be opened: %#",[NSString stringWithCString:errorMsg encoding:NSUTF8StringEncoding]];
error = [self createDBErrorWithDescription:errorStr andCode:kDBFailAtOpen];
}
return error;
}
Many thanks to all!
Because it's saying the database table does not exist, you may not be opening the database # the path that you think you are. Remember that sqlite is passive - it will create a database on first write.
Run the app in the simulator and print out the path. Then go to that path in terminal and use the sqlite cmdline to confirm the db is there and it has the tables.

"SQL error or missing database" error while preparing statement

I am trying to insert values in database but while preparing statement it is giving me error "SQL error or missing database"...can anyone know what this error is and how one can resolve this?????my path for database is
2010-07-22 14:34:59.933 DatabaseApp[1521:207] path of database /Users/nuzhat/Library/Application Support/iPhone Simulator/User/Applications/C50A0188-2A9A-487F-951C-6E7FFCE3CFBB/Documents/UserName.db3
here is my code:-
// Open the database connection and retrieve minimal information for all objects.
- (void)initializeDatabase {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"TextWandiPhone.db3"];
//for second database
NSString *path1 = [documentsDirectory stringByAppendingPathComponent:#"UserName.db3"];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK)
{
// Get the primary key for all books.
const char *sql = "SELECT CountryName FROM Country";
sqlite3_stmt *statement = nil;
// Preparing a statement compiles the SQL query into a byte-code program in the SQLite library.
// The third parameter is either the length of the SQL string or -1 to read up to the first null terminator.
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK)
{
// int success=sqlite3_step(statement);
// We "step" through the results - once for each row.
while(sqlite3_step(statement) == SQLITE_ROW)
{
// if(success == SQLITE_ROW) {
//NSLog(#"value of success %d",success);
// The second parameter indicates the column index into the result set.
char *str = (char *)sqlite3_column_text(statement, 0);
NSString *country = (str) ? [NSString stringWithUTF8String:str] : #"";
//NSLog(#"value :%#",country);
//NSLog(#"after running query");
//NSLog(#"after initializing array");
[arrCountry addObject:country];
//NSLog(#"after adding values");
//[return arrCountry];
}//while
//NSLog(#"values of array %#",arrCountry);
}//aft prepare
sqlite3_finalize(statement);
}//aft open
//for second database
//else if (sqlite3_open([path1 UTF8String], &database1) == SQLITE_OK)
if (sqlite3_open([path1 UTF8String], &database1) == SQLITE_OK)
{
// Get the primary key for all books.
const char *sql1 = "SELECT UserID FROM User";
sqlite3_stmt *statement1=nil;
// Preparing a statement compiles the SQL query into a byte-code program in the SQLite library.
// The third parameter is either the length of the SQL string or -1 to read up to the first null terminator.
int value1=sqlite3_prepare_v2(database1, sql1, -1, &statement1, NULL);
NSLog(#"value of preparing stmt %d",value1);
if ( value1== SQLITE_OK)
{
// int success=sqlite3_step(statement);
// We "step" through the results - once for each row.
while(sqlite3_step(statement1) == SQLITE_ROW)
{
// if(success == SQLITE_ROW) {
//NSLog(#"value of success %d",success);
// The second parameter indicates the column index into the result set.
int userid = sqlite3_column_int(statement1, 0);
//NSString *country = (str) ? [NSString stringWithUTF8String:str] : #"";
//NSLog(#"value :%#",country);
//NSLog(#"after running query");
//NSLog(#"after initializing array");
NSString *strUserId = [[NSNumber numberWithInt:userid] stringValue];
[arrUser addObject:strUserId];
//[return arrCountry];
}
NSLog(#"values of array %#",arrUser);
}//aft prepare
// "Finalize" the statement - releases the resources associated with the statement.
//sqlite3_finalize(statement1);
}//aft opening 2nd database
else {
// Even though the open failed, call close to properly clean up resources.
sqlite3_close(database);
sqlite3_close(database1);
NSAssert1(0, #"Failed to open database with message '%s'.", sqlite3_errmsg(database));
NSAssert1(0, #"Failed to open database with message '%s'.", sqlite3_errmsg(database1));
// Additional error handling, as appropriate...
}
}
thanks in advance....
Not enough information here, but I think it could be a couple of things. Does the user of the database you are using have the permissions that allow them to insert data to the database? You might want to append your post so it includes the SQL statement you are using.