sqlite3 in iphone application - ios5

I build in sqlite3 3 tables. When I'm doing the following SELECT command:
select us.saleSpecificProduct,us.fAtMall,u.name,u.gender,st.storeName
from UsersSale us,Users u,Stores st
where u.userID=us.userID and st.storeID=us.saleStoreID order by us.saleID
It work ok in the shell, but if i put this statement in my iphone application I'm getting an error.
/*
* read items from the database and store in itemw
*
*/
-(void)readItems {
if (!database) return; // earlier problems
// build select statement
if (!selStmt)
{
const char *sql = "select us.saleSpecificProduct,us.fAtMall,u.name,u.gender,st.storeName from UsersSale us,Users u,Stores st where u.userID=us.userID and st.storeID=us.saleStoreID order by us.saleID";
if (sqlite3_prepare_v2(database, sql, -1, &selStmt, NULL) != SQLITE_OK)
{
selStmt = nil;
}
When I execute the application I get error in "sqlite3_prepare_v2"
}
What is wrong?

Maybe try putting your parameters inside single quotes
So something like:
WHERE u.userID = 'us.userID' AND st.storeID = 'us.saleStoreID' ORDER BY us.saleID
Also you should make use of bindings to get safe statements:
e.g.
NSString *strGet = #" . . . WHERE u.userID = ? AND st.storeID = ? ORDER BY us.saleID"
if(sqlite_prepare_v2(...) == SQLITE_OK)
{
sqlite3_bind_int(stmtGet, 1, us.userID);
sqlite3_bind_int(stmtGet, 2, us.saleStoreID);
while(sqlite3_step(stmtGet) == SQLITE_ROW)
{
DatabaseDataObject *myDataObj = [[DatabaseDataObject alloc] init];
myDataObj.objID = sqlite3_column_int(0);
myDataObj.postcode = sqlite3_column_int(1);
// add to parameter array of objects
[myArr addObject:myDataObj];
[myDataObj release];
}
}

Related

select query in sqlite(iPhone)

I have problem while selecting data from database.
I created the "foldertable" in database with following query
CREATE TABLE foldertable(fid integer primary key autoincrement,foldername text);
and here is my code to get foldername according to fid(folderid)
-(NSString *)getFolderFromDatabase:(int)folderId
{
NSString * retriveFolderName;
UIApplication * application=[UIApplication sharedApplication];
ScrapMemoAppDelegate * appDelegate=application.delegate;
NSString * destinationPath=[appDelegate getDestinationPath];
sqlite3 * database;
int retriveWhere=folderId;
if(sqlite3_open([destinationPath UTF8String], &database)==SQLITE_OK)
{
const char * query="select foldername from foldertable where fid = ?;";
sqlite3_stmt * statement;
if(sqlite3_prepare_v2(database, query, -1, &statement, NULL)==SQLITE_OK)
{
if (sqlite3_step(statement)==SQLITE_DONE)
{
sqlite3_bind_int(statement, 1, retriveWhere);
retriveFolderName=[NSString stringWithCString:sqlite3_column_text(statement,0) encoding:NSASCIIStringEncoding];
}
else
{
NSLog(#"Error %s",sqlite3_errmsg(database));
}
sqlite3_reset(statement);
}
sqlite3_close(database);
}
return retriveFolderName;
}
But I get null foldername while I fired same query on terminal it gives proper name.Please
provide me solution.
From looking at the code, the primary issue is that you try to execute (step) the query and then you try to bind the value for fid. You need to do the bind first.

sqlite3 query works on cmd line but returns no data in iOS 6 SImulator

I have this sqlite3 table used in an iOS 6 app for iPad:
CREATE TABLE notes(id INTEGER PRIMARY KEY, note TEXT, noteDate TEXT, wasUploaded INTEGER);
from the sqlite3 command line this query works:
sqlite> Select `id`,`note`,`noteDate`,`wasUploaded` FROM `notes` WHERE `wasUploaded`=0;
1|Well|2012-10-04 22:46:23|0
On iOS iPad 6.0 Simulator each of these queries returns the exact same data as above:
const char *sqlStatement = "Select `id`,`note`,`noteDate`,`wasUploaded` FROM `notes` WHERE `id`=1";
const char *sqlStatement = "Select `id`,`note`,`noteDate`,`wasUploaded` FROM `notes` WHERE `note`='Well'";
const char *sqlStatement = "Select `id`,`note`,`noteDate`,`wasUploaded` FROM `notes` WHERE `noteDate`='2012-10-04 22:46:23'";
But this query which worked fine on the command line now returns no data:
const char *sqlStatement = "Select `id`,`note`,`noteDate`,`wasUploaded` FROM `notes` WHERE `wasUploaded`=0";
Has me baffled. Why is that last query not working? Do I need to make that column an index or something? The other two non-indexed columns work but not this.
No errors. The last query that returns no data gives a normal return code of 101 (sqlite3_step() has finished executing) and a query without the where clause returns the same data as for the other three queries.
Edit: here is the complete code
- (NSString *)getNotesToBeUploaded {
sqlite3 *stuDb;
NSString *thisNote;
NSMutableString *notes = [[NSMutableString alloc]init];
if (self.filePath == #"empty") {
[self setDatabaseFilePath];
}
if (sqlite3_open([self.filePath UTF8String], &stuDb) == SQLITE_OK)
{
// this is the query line that get changed to show stackoverflow the different results:
const char *sqlStatement = "Select `id`,`note`,`noteDate` FROM notes WHERE `wasUploaded`=0";
sqlite3_stmt *compiledStatement;
int nResult = sqlite3_prepare_v2(stuDb, sqlStatement, -1, &compiledStatement, NULL);
if ( nResult == SQLITE_OK)
{
int nret; // diagnostic used to watch return vaues when single stepping
while ((nret = sqlite3_step(compiledStatement)) == SQLITE_ROW)
{
int id = sqlite3_column_int(compiledStatement, 0);
const unsigned char *note = sqlite3_column_text(compiledStatement, 1);
const unsigned char *noteDate = sqlite3_column_text(compiledStatement, 2);
int wu = sqlite3_column_int(compiledStatement, 4);
if (strlen((const char *)note) > 0 && strlen((const char *)noteDate) > 0)
{
thisNote = [NSString stringWithFormat:#"%d,%s,%s,%d\n",id, noteDate, note, wu];
[notes appendString:thisNote];
}
}
} else {
sqlite3_finalize(compiledStatement);// prevent small memory leaks
sqlite3_close(stuDb);
thisNote =
[NSString stringWithFormat:#"prepare failed with status:%d in %s at line %d path was %#,0,0\n",nResult,__FILE__,__LINE__,self.filePath];
[notes appendString:thisNote];
[notes appendString:#"\n"];
return (NSString *)notes;
}
sqlite3_finalize(compiledStatement);
sqlite3_close(stuDb);
}
Are you checking the return codes from your sqlite3 calls? And, if you're not getting SQLITE_OK or SQLITE_ROW, as appropriate, you should check the sqlite3_errmsg results to diagnose what's going on. You really should share your code if you want us to help you.
But the most common problems in the first-time iOS SQLite apps are
Failing to include the database in your app's bundle. Check your Target settings and make sure you've included the database in the Build Phases. You can also confirm this by looking at your app's bundle in the simulator in the ~/Library/Application Support/iPhone Simulator folder. If you want to do that, you may want to unhide your ~/Library folder if you haven't by typing in chflags no hidden ~/Library in the Terminal command line interface.
If you're planning on updating your database from the app, failing to first copy the database from the bundle to the Documents folder before you try to start using it.
Using sqlite3_open and interpreting a successful return code as evidence that the database was opened successfully ... but if it didn't find the database, the sqlite3_open function annoyingly will create a new blank database ... I always suggest that people use sqlite3_open_v2 instead, in which you can omit the parameter to create a blank database if it's not found if that's not what you want.
Certainly, there can be a ton of code-related issues (order that the functions are called, failing to check return codes, etc.), too. It's impossible to comment further without seeing the code.
And I feel obliged to share my final SQLite programming advice that it's worth checking out FMDB Objective-C SQLite wrapper library, which greatly simplifies SQLite programming in iOS.
Update:
Having looked at your code, it looks fine. I just ran it (only tweaked to just NSLog rather than appending notes):
- (void)test2
{
sqlite3 *stuDb;
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *databaseName = [documentsDirectory stringByAppendingPathComponent:#"test.db"];
if (sqlite3_open([databaseName UTF8String], &stuDb) == SQLITE_OK)
{
// this is the query line that get changed to show stackoverflow the different results:
const char *sqlStatement = "Select `id`,`note`,`noteDate` FROM notes WHERE `wasUploaded`=0";
sqlite3_stmt *compiledStatement;
int nResult = sqlite3_prepare_v2(stuDb, sqlStatement, -1, &compiledStatement, NULL);
if ( nResult == SQLITE_OK)
{
int nret; // diagnostic used to watch return vaues when single stepping
while ((nret = sqlite3_step(compiledStatement)) == SQLITE_ROW)
{
int id = sqlite3_column_int(compiledStatement, 0);
const unsigned char *note = sqlite3_column_text(compiledStatement, 1);
const unsigned char *noteDate = sqlite3_column_text(compiledStatement, 2);
int wu = sqlite3_column_int(compiledStatement, 4);
if (strlen((const char *)note) > 0 && strlen((const char *)noteDate) > 0)
{
// thisNote = [NSString stringWithFormat:#"%d,%s,%s,%d\n",id, noteDate, note, wu];
// [notes appendString:thisNote];
NSLog(#"%d,%s,%s,%d\n",id, noteDate, note, wu);
}
}
} else {
//sqlite3_finalize(compiledStatement);// prevent small memory leaks, not needed if the prepare failed
sqlite3_close(stuDb);
NSLog(#"prepare failed with error %s", sqlite3_errmsg(stuDb));
return;
}
sqlite3_finalize(compiledStatement);
sqlite3_close(stuDb);
}
}
And I got results:
2012-10-05 15:44:06.075 test8[1574:c07] 1,2012-10-05 19:43:37,ABC,0
2012-10-05 15:44:06.076 test8[1574:c07] 2,2012-10-05 19:43:46,XYZ,0
So the problem must be in the database itself. Judging from your last comment, it sounds like rebuilding the database did it for you. That's great.

Objective-C Issue with deleting data from SQLITE database - result code 5

I am experiencing issues with deleting a row / data record from an SQLite database located on the iPhone. I followed advice given in the following post, but the result code from SQLite is 5. From the SQLite reference manual, 5 means:
SQLITE_BUSY 5 /* The database file is locked */
Has anyone else come across this problem or have any ideas why this would be the case?
Here is an excerpt of my code (dbrc is set to 5):
NSString *retrievedContactID = [archivedContact objectForKey:#"contact_id"];
sqlite3 *database = [FollowUPAppDelegate checkAndCreateDatabase];
NSString *deleteSQL = [NSString stringWithFormat:#"DELETE FROM Contacts WHERE contact_id = %#", retrievedContactID];
const char *delete_stmt = [deleteSQL UTF8String];
sqlite3_stmt *compiledStatement;
int dbrc; //database return code
dbrc = sqlite3_prepare_v2(database, delete_stmt, -1, &compiledStatement, NULL);
dbrc = sqlite3_step(compiledStatement);
sqlite3_finalize(compiledStatement);
compiledStatement = NULL;
if (dbrc != 101) { //anything except 101 (SQLITE_DONE for delete, *not* SQLITE_OK)
NSLog(#"Error deleting contact from database: result code %i", dbrc);
return;
}
else {
NSLog(#"deleted the customer from datasets");
}
sqlite3_close(database);
This means. your database is being used by some other process. you have to stop the other process and proceed.
I can give you an example. when you are accessing a database from your app and if you try to change the content using some external sqlite manager you would get this busy error. The solution to this problem is,stop your app and try.
You problem is similar. Your database is locked by some other process.

data is not being inserted from this insert query

I am using this code to insert the data in my database
but it is not working..
My data is not being inserted in the table ..
what can be the problem??
in function.h
+(BOOL)insertStudentinfoData:(NSString *)first_name last_name:(NSString *)last_name phone_num:(NSString *)phone_num;
in function.m
+(BOOL)insertStudentinfoData:(NSString *)first_name last_name:(NSString *)last_name phone_num:(NSString *)phone_num
{
NSString *sql = [NSString stringWithFormat:#"insert into add_data
values(NULL,'%#','%#','%#')",first_name,last_name,phone_num];
return [DBOperation executeSQL:sql];
}
And I am giving the data from this
[Function insertStudentinfoData:#"hello" last_name:#"w3qrq" phone_num:#"efew"];
but my data is not being inserted in the table
////In DBOperation.h
+(BOOL) executeSQL:(NSString *)sqlTmp {
if(conn == SQLITE_OK) {
NSLog(#"\n\n%#",sqlTmp);
const char *sqlStmt = [sqlTmp cStringUsingEncoding:NSUTF8StringEncoding];
sqlite3_stmt *cmp_sqlStmt1;
int returnValue = sqlite3_prepare_v2(database, sqlStmt, -1, &cmp_sqlStmt1, NULL);
returnValue == SQLITE_OK ? NSLog(#"\n Inserted \n") :NSLog(#"\n Not Inserted \n");
sqlite3_step(cmp_sqlStmt1);
sqlite3_finalize(cmp_sqlStmt1);
if (returnValue == SQLITE_OK) {
return TRUE;
}
}
return FALSE;
}
I guess you are passing the NULL value for PRIMARY KEY, first reset the simulator
if you are not specifying the column name and inserting the values then you should pass the value for each column in particular order of column created otherwise its a good idea to specify column
NSString *sql = [NSString stringWithFormat:#"INSERT INTO add_data
(first_name,last_name,phone_num) VALUE('%#','%#','%#')",first_name,last_name,phone_num];
or
NSString *sql =[NSString stringWithFormat:#"INSERT INTO add_data
(first_name,last_name,phone_num,email,address,city,zip) VALUES
('%#','%#','%#','%#','%#','%#','%#');",first_name,last_name,phone_num,email,addr‌​ess,city,zip];
are you sure that the database is initialized correctly? I always had trouble with the DB not actually there. Which DB Framework are you using?
[EDIT] do you see this log statement?
NSLog(#"\n\n%#",sqlTmp);
Did you write the DBOperations class yourself? I think there is some issue there with the static variable you're using. I'd suggest to either use an existing db framework like fmdb or something, or else modify your class so that it uses the Singleton Pattern.
Oh, and one thing: Are you calling all methods from the same thread? SQlite DBs are not thread safe!
[edit2] You can use the link provided here: SQLite3 error - iOS to check what the value in returnValue actually states - and then figure out the underlying error.
It's probably best for now to modify your not inserted statement like this:
returnValue == SQLITE_OK ? NSLog(#"\n Inserted \n") :NSLog(#"\n Not Inserted with code: %i\n",returnValue);
What if you provide the names of the columns to be inserted:
insert into T (foo, bar, baz)
values( ....)
Try it this way to see full error code and message:
The sql comes as NSString *, do not convert it into a cstring.
int rc;
sqlite3_stmt *sel;
if ((rc = sqlite3_prepare_v2(db_handle, [sql UTF8String], -1, &sel, NULL)) != SQLITE_OK) {
NSLog(#"cannot prepare '%#': %d (%s)", sql, rc, sqlite3_errmsg(db_handle));
return;
}
if (sqlite3_step(sel) != SQLITE_DONE) {
NSLog(#"error insert/update: '%s'", sqlite3_errmsg(db_handle));
...

Select Query Is Not Being Prepared

Here i am implementing a select query to fetch data from table but no any row is been returned by my query.
below is my code that i am using:
if ((sqlite3_open([[self dataFilePath] UTF8String], &database))==SQLITE_OK)
{
NSLog(#"DataBase OpeneD..!!");
imageId=[[NSMutableArray alloc] init];
const char *slectImageIdQuery="SELECT * from ts_Gallary;";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, slectImageIdQuery, -1, &statement, nil)==SQLITE_OK)
{
NSLog(#"Query CREATED..!!");
while (sqlite3_step(statement)==SQLITE_ROW)
{
int iId=sqlite3_column_int(statement, 0);
NSString *imgTtl=[NSString stringWithFormat:#"%d",iId];
NSLog(#"image id in while=%d",iId);
[imageId addObject:[NSString stringWithFormat:#"%#",imgTtl]];
}
}
else
{
NSLog(#"query could not be prepared");
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
My console display:
2011-04-06 17:44:33.381 Tuscany[4680:207] DataBase OpeneD..!!
2011-04-06 17:44:33.382 Tuscany[4680:207] query could not be prepared
Note: Here dataFilePath is method which return path of my database and database is sqlite3 object
No Error or exception is been thrown by my application.
What problem can there be and how can I detect the problem as well as solve that?
Thanks.
No Error or exception is been thrown
by my application.
Well, yeah, there is. Your call to sqlite3_prepare_v2() fails; it doesn't return SQLITE_OK.
What happens when you run this statement at your database's sqlite prompt?
SELECT * from ts_Gallary;
Try replacing this line
NSLog(#"query could not be prepared");
with something that will give you more information about the error. Include a call to sqlite3_errmsg().
Remove the semicolon(;) from const char *slectImageIdQuery="SELECT * from ts_Gallary;"
like const char *slectImageIdQuery="SELECT * from ts_Gallary"
and its work properly.