warning: invalid receiver type +sqlite3 - iphone

I am programming an Xcode iPhone app and utilizing sqlite. In an effort to delete all rows from a table, I receive the warning above when I build my code. Does anyone have any suggestions on how to fix this?
Thanks
- (void) deleteData {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory
stringByAppendingPathComponent:#"myDatabase.sqlite"];
if (sqlite3_open([writableDBPath UTF8String], &database) == SQLITE_OK) {
[database executeNonQuery:#"DELETE FROM test;"];
}
[database release];
}

Assuming
sqlite3 *database;
somewhere all up ins, it should be noted that sqlite3_open() doesn't create an Objective-C object; it creates an sqlite3 database handle, which is, if memory serves, a struct packed in a pointer. It can, in other words, not receive Objective-C messages. * does not an object make.

Related

Storing sqlite databsae in the iphone. Unable to get it back.

Not sure what the mistake i am doing. Please suggest.
In my application i am creating a database, and when i relaunch the application next time, the database is not seen. I found what the problem is, but not the solution .
NSArray *dirPaths;
NSString *docsDirectory;
NSString *databasePath;
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDirectory = [dirPaths objectAtIndex:0];
databasePath = [[NSString alloc] initWithString: [docsDirectory stringByAppendingPathComponent: #"MYDATABASE.db"]];
NSLog (#" Database Path : %# ",databasePath);
const char *dbpath = [databasePath UTF8String];
Unfortunately, i get the following path printed.
Database Path : /var/mobile/Applications/27C4C465-0D65-44AA-BBB3-693G4D1CDD4D/Documents/MYDATABASE.db
When i reinvoke the application i get the different path ie: 27C4C465-0D65-44AA-BBB3-693G4D1CDD4 is replaced with some other value, that's why it could not able to locate the database properly.
How can i resolve this issue? Kindly suggest.

Opening SQL with Objective-C

I have an sql file in my supporting files. How can I open this file so that I can start using SQL commands to get the information that I want? I've tried a couple different syntaxes, and the last time I tried I ended up screwing up the whole application because I couldn't remember what I had changed.
If you are using sqlite 3. Then,
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths lastObject];
NSString* dbPath = [documentsDirectory stringByAppendingPathComponent:#"database.sqlite"];
if(sqlite3_open([dbPath UTF8String], &dataBase) == SQLITE_OK)
{
NSLog(#"Opened Database");
//Your code
}
else
{
NSLog(#"Failed to open database");
sqlite3_close (database);
}

Save NSMutableArray to load when reopenning iphone app

I have a NSMutableArray, each item in this array is different class. In each class has many field such as CPPlot, identifier,... (I am using CorePlot to develop a stock application). Now I would like to store this NSMutableArray to load when user reopen application, this will load all the chart they used before.
I try to figure out how to do that in Stackoverflow. And I found out there were 2 solutions:
NSUserDefaults
SQLite database
In NSUserDefaults, when I want to store NSMutableArray, I must implement with NSKeyedArchiver to archive and unarchive array object, also do NSCoding protocol for each item in array object. But I can not do this solution because in each item, it has some fields from CorePlot library, so that I can not use NSCoding to these fields.
SQLite database, I can not use this solution because each item in array object is different class.
I would like to ask if any other solution to solve this problem?
I hope my words are clear enough to understand.
Thanks
I would suggest you figure out what kind of data is at the root of your CorePlot objects. If it is integers, then simply store them in NSUserDefaults, and then simply rebuild your NSMutableArray on re-opening the app. Another option is to store your items in a separate plist file.
Use this method to save:
- (NSArray *)applicationDataFromFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirecotiresInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory
stringByAppendingPathComponent:fileName];
NSArray *myData = [NSArray arrayWithContentsOfFile:appFile];
return myData;
}
- (BOOL)saveToFileForStringArray:(NSMutableArray *)array
toFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(#"Documents directory not found!");
return NO;
}
NSString *appFile = [documentsDirectory
stringByAppendingPathComponent:fileName];
return ([array writeToFile:appFile atomically:YES]);
}

Accessing a database through Obj-c

I am trying to find some source code on how to access and store variables from a database to my program via obj-c(iPhone). I have look for many hours now and no one has provided a sure fire way on how to go about this. If you have any advice or recommendations please post some source code or a link to it.
Thanks for the help.
If using the sqlite3 database which your program has access to on the phone for local database storage.
#import <sqlite3.h>
And create an openDatabase() method. Also add a variable for keeping the db around.
sqlite3 *db = nil;
Just make sure you call your open database method before using the database. Check this page out http://ved-dimensions.blogspot.com/2009/03/iphone-development-sqlite3-populating.html
Might give you something that you can use.
+(sqlite3 *) getNewDBConnection{
sqlite3 *newDBconnection;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"data.sqlite"];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &newDBconnection) == SQLITE_OK) {
NSLog(#"Database Successfully Opened :)");
} else {
NSLog(#"Error in opening database :(");
}
return newDBConnection;
}

Inexplicable EXC_BAD_ACCESS in the iPhone simulator

I'm trying to build a class for handling all sqlite3 work and I've encountered an EXC_BAD_ACCESS which I just can't explain. I am new to Objective-C development and memory management in general so I apologize if this is a stupid question.
When initializing the class I get the path to the database file and keep it around:
NSArray * documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
databasePath = [[[documentPaths objectAtIndex:0] stringByAppendingPathComponent:#"Database.sql"] retain];
Then I try to use it and it crashes on me:
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { // crashes
The odd thing is that the exact same line in a different function works perfectly. I tried adding the variable initialization to above the line, but to no avail:
NSArray * documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
databasePath = [[[documentPaths objectAtIndex:0] stringByAppendingPathComponent:#"Database.sql"] retain];
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { // crashes
In both cases the retain count is 2.
However, putting any form of static text in there works fine:
databasePath = #"I have a balloon";
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { // fine
// or
if (sqlite3_open([#"APPLES!" UTF8String], &database) == SQLITE_OK) { // fine
A little more experimentation revealed that it was the UTF8String function that was crashing, but I don't understand why.
Edit: Even more experimentation resulted in me being able to call the UTF8String function but not even use the results:
const char * test = [databasePath UTF8String];
NSLog(#"%#", [NSString stringWithUTF8String:test]); // fine
if (sqlite3_open(test, &database) == SQLITE_OK) { // fails
Edit: Messed around even more and discovered that the problem was in a parameter that I was passing to the function. I had breakpoints all over the place so I was 100% sure that the point it fails is at the line I specified, a line which doesn't use the passed variable, but somehow it caused everything to fail. Sometimes. I have no idea how it went wrong, but I rewrote the entire thing, changed my function call and now it works.
Thanks to all who answered.
Have you checked the contents of the databasePath variable before the open call is made? Add an NSLog statement, or use the debugger, to find its value – it could be either NULL, which may not be handled by the sqlite3_open function, or it could even be that the crash is because the path is valid, and the database it points to is corrupt.
My guess is that the database variable is not declared properly. It doesn't seem to have anything to do with the path to the database.
Did you declare database like this?:
sqlite3 *database;
I suspect you are chasing the wrong problem. You haven't shown a crash dump but claim that opening "APPLES!" is 'fine' which sounds odd - surely you don't have a database with that name?
Are you sure you don't have a bad database file lying around - ie, the database file you are trying to open is corrupted in some way?
I think you have to post the crash log, your crash makes no sense. The code you posted should work.
Try a copy instead of a retain on the variable - it'll make an immutable copy of the string so it shouldn't go changing or disappearing.
EDIT::
What happens when you simply type
if (sqlite3_open[#"myDatabase.sqlite" UTF8String], &database) == SQLITE_OK) {
Also are you sure that the
NSArray * documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
databasePath = [[[documentPaths objectAtIndex:0]
stringByAppendingPathComponent:#"Database.sql"] retain];
is actually returning where the database is?
The code I use to grab the database from the app directory where kFilename is the database file is:
- (NSString *)dataFilePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
and then to use it I do:
if(sqlite3_open([[self dataFilePath] UTF8String], &database) != SQLITE_OK){
This works on both my iPod touch and the iPhone simulator.
EDIT2::
I've just used your above method and it seems to work here - I know it sounds stupid but have you tried rebooting? Its possible something strange is happening with the simulator