i cant Load value into array - iphone

const char *dbpath = [databasePath UTF8String];
UserArray = [[NSMutableArray alloc]init];
sqlite3_stmt *compiledStatement;
if(sqlite3_open(dbpath, &db) == SQLITE_OK)
{
NSString *querySQL = [NSString stringWithFormat: #"select UserName,FullName,Email from User" ];
const char *sqlStatement =[querySQL UTF8String];
if(sqlite3_prepare_v2(db ,sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement) == SQLITE_ROW )
{
NSString *aUserName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
NSString *aFullName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
NSString *aEmail = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];
// Create a new animal object with the data from the database
User *obj_user = [[User alloc] initWithUserName:aUserName FullName:aFullName Email:aEmail];
// Add the animal object to the animals Array
[UserArray addObject:obj_user];
[User release];
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}
sqlite3_close(db);
}
Hey All,
I am trying to put value into array of object but it throw's error Like Below...
anyone guide me why i got this type of error.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UICachedDeviceRGBColor UserArray]: unrecognized selector sent to instance 0x6a396f0'

Related

Sqlite data not getting fetching

I am trying fetch the value from data base But it is not getting
I am trying this code but didn't get the value ,When i use the Break point on the program
I am having Longitude,Latitude his data type is double in data base
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *sqlStr = [NSString stringWithFormat:#"select Longitude,Latitude from Location"];
const char *sqlStatement = [sqlStr UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
locations = [NSMutableArray array];
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
int i = sqlite3_step(compiledStatement);
NSLog(#"%i",i); **//over here i am getting the 101 value in console** And my pointer getting out from here
NSString *dLongitude = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
NSString *dLatitude = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 4)];
[locations addObject:[NSString stringWithFormat:#"%# ,%#",dLongitude,dLatitude]];
NSLog(#"%#",locations);
}
result = [locations componentsJoinedByString:#" "]; // same as `fake_location`
// Get file path here
NSError *error;
if ( [result writeToFile:dbPath atomically:YES encoding:NSUTF8StringEncoding error:&error] ) {
NSLog(#"%#", [error localizedDescription]);
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
int i = sqlite3_step(compiledStatement);
NSLog(#"%i",i); **//over here i am getting the 101 value in console** And my pointer getting out from here
double longitude = sqlite3_column_double(compiledStatement, 3);
double latitude = sqlite3_column_double(compiledStatement, 4);
NSString *coords = [[[NSString alloc] initWithFormat:#"%f,%f",longitude,latitude] autorelease];
[locations addObject:coords];
NSLog(#"%#",locations);
}
Change the code to the following :
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *sqlStr = [NSString stringWithFormat:#"select Longitude,Latitude from UserJourneyLocation"];
const char *sqlStatement = [sqlStr UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
locations = [NSMutableArray array];
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
int i = sqlite3_step(compiledStatement);
// NSLog(#"%i",i); / comment this line or use NSLog(#"%d",i);
// Make sure your longitude is double in database. else change the fetching value declaration
// We use 0 for dLongitude instead of 3 because in the select statement, it is the first value to be fetched irrespective of your table structure
// and same for dLatitude.
double dLongitude = sqlite3_column_double(compiledStatement, 0);
double dLatitude = sqlite3_column_double(compiledStatement, 1);
[locations addObject:[NSString stringWithFormat:#"%d ,%d",dLongitude,dLatitude]];
NSLog(#"%#",locations);
}
result = [locations componentsJoinedByString:#" "]; // same as `fake_location`
// Get file path here
NSError *error;
if ( [result writeToFile:dbPath atomically:YES encoding:NSUTF8StringEncoding error:&error] ) {
NSLog(#"%#", [error localizedDescription]);
}
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);

Accessing an object through SQLIte database

I am passing an object to the method in which I am executing a query. My method is:
-(BOOL)searchWordInDatabase:(NSString *)string
{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory= [paths objectAtIndex:0];
NSString *path=[documentsDirectory stringByAppendingPathComponent:#"SymbolTalkLanguageElement.sqlite"];
//Open the database
//might have to make database as property
if(sqlite3_open([path UTF8String], &dataBase) ==SQLITE_OK)
{
const char *sql="select ImageName from tblLanguageElement where Category= string";
sqlite3_stmt *statement;
if(sqlite3_prepare(dataBase, sql, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSLog(#"%#",[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]);
//[list addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]];
return YES;
}
}
}
return NO;
}
But I am not getting the value of the String in the query. How can I do it?
There is a mistake in your query. You can not substitute a string like that. Do it in the following way.
NSString *sqlStr = [NSString stringWithFormat:#"select ImageName from tblLanguageElement where Category = '%#'", string];
char *sql = (char *)[sqlStr UTF8String];
Make sure that your database exists at that path you retrieve. And you can bind string variables as follows while executing statements.
const char *sql = "select ImageName from tblLanguageElement where Category = ?";
sqlite3_stmt *statement;
if(sqlite3_prepare(dataBase, sql, -1, &statement, NULL) == SQLITE_OK)
{
sqlite3_bind_text(statement, 1, [string UTF8String], -1, SQLITE_TRANSIENT);
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSLog(#"%#",[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]);
//[list addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]];
return YES;
}
}

Problem With Sqlite Data Retrieving

I have an SQLite database, and when I am trying to get the data from the database, I get the last inserted element repeatedly. How can I get all the elements with no repetition.
The code I've written:
- (NSMutableArray *) gettingData {
sqlDict = [[NSMutableDictionary alloc] init];
membersInfoArray =[[NSMutableArray alloc]init ];
[self checkAndCreateDatabase];
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "select * from ProductList";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK)
{
while(sqlite3_step(selectstmt) == SQLITE_ROW)
{
NSString *prdbcode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
[sqlDict setObject:prdbcode forKey:#"Barcode"];
[prdbcode release];
NSString *prdname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
[sqlDict setObject:prdname forKey:#"ProductName"];
[prdname release];
NSString *prdDesc = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
[sqlDict setObject:prdDesc forKey:#"ProductDescription"];
[prdDesc release];
NSString *prdstatus = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 3)];
[sqlDict setObject:prdstatus forKey:#"ProductStatus"];
[prdstatus release];
[membersInfoArray addObject:sqlDict];
[sqlDict release];
}
}
sqlite3_finalize(selectstmt);
}
sqlite3_close(database);
return membersInfoArray;
}
I am retrieving the data as follows:
NSMutableArray *sqlArray = [sqlViewController gettingData];
Thank you.
Just Declare your array globally instead of declare locally in your method. Your problem will resolved.

problem while loading data stored in data base into table view

hii every one
i am using following code to loading data stored in data base into table view but its crasing when i re-launch the application
InsertRecord is the instance of class insertUpdateDelete
+ (void) getInitialDataToDisplay:(NSString *)dbPath
{
iICS_testAppDelegate *appDelegate = (iICS_testAppDelegate *)[[UIApplication sharedApplication] delegate];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select * from tbl_Users";
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);
insertUpdateDelete *InsertRecord = [[insertUpdateDelete alloc] initWithPrimaryKey:primaryKey];
InsertRecord.strFirstName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
InsertRecord.strMiddleName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 2)];
[appDelegate.arrObjects addObject:InsertRecord];
[InsertRecord release];
}
}
}
else
{
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
NSLog(#"arrObjects----%#",appDelegate.arrObjects);
}
application is crashing at the following line
InsertRecord.strFirstName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
crash log is-- Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* +[NSString stringWithUTF8String:]: NULL cString'
You need to check for null data in your database;
// load char in temporary variable
char *tempChar = sqlite3_column_text(selectstmt, 1);
if(tempChar == null) // test for null
InsertRecord.strFirstName = nil;
else
InsertRecord.strFirstName = [NSString stringWithUTF8String:tempChar];
// go to the next field in the database
tempChar = sqlite3_column_text(selectstmt, 2);
// do the same type of test
Test if the sqlite3_column_text(selectstmt, 1) return an valid string.
(char *)sqlite3_column_text(selectstmt, 1) should be given nil (null) value.
So, Add validation when you get data from database.
Like,
if ((char *)sqlite3_column_text(selectstmt, 1) != nil)
{
InsertRecord.strFirstName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
}
else
{
InsertRecord.strFirstName = #"";
}

Variable Not a CFString

I am using a database to populate a table view.
I created an NSMutable Array of object in the AppDelegate. The Array is populated from the DB as follows:
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select barcode, vintage, vineyard, varietal from wine";
sqlite3_stmt *statement;
if(sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
while(sqlite3_step(statement) == SQLITE_ROW) {
NSString *barcode = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
DBHelper *wineObj = [[DBHelper alloc] initWithBarcode:barcode];
wineObj.vintage = sqlite3_column_int(statement, 1);
wineObj.vineyard = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)];
wineObj.varietal = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
[appDelegate.wineList addObject:wineObj];
[wineObj release];
}
}
}
All of the data goes into the array correctly with barcode being of type NSString *. However, when the table view is displayed, the barcode turns into "Variable Not a CFString." When I select an item, I am using:
//get the bottle details for selected object
DBHelper *tempObj = [appDelegate.wineList objectAtIndex:indexPath.row];
DBHelper getBottleDetails:[appDelegate getDBPath]:tempObj.barcode];
Because the barcode is not longer being interpreted as an NSString, the app crashes. How do I ensure that the barcode remains valid to use?