Reading SIM contacts in jailbroken iPhone - iphone

I am working on an application which needs to read the contacts from the SIM.
I know that it is not possible using the official Apple SDK.
I am developing this app for the jailbroken iPhones.
I have searched a lot but the only answer I got is NOT POSSIBLE.
Any help towards the path will really be appreciated.

NSString *addressbookDatabasePath = #"/private/var/wireless/Library/AddressBook/addressbook.db";
addressbookFileExist = [fileManager fileExistsAtPath:addressbookDatabasePath];
[fileManager release];
NSMutableArray *addressbook = [[NSMutableArray alloc] init];
if(addressbookFileExist) {
if ([fileManager isReadableFileAtPath:addressbookDatabasePath]) {
sqlite3 *database;
if(sqlite3_open([addressbookDatabasePath UTF8String], &database) == SQLITE_OK) {
sqlite3_stmt *compiledStatement;
NSString *sqlStatement = [NSString stringWithString:#"SELECT * FROM call;"];
int errorCode = sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1,
&compiledStatement, NULL);
if( errorCode == SQLITE_OK) {
int count = 1;
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// Read the data from the result row
NSMutableDictionary *addressbookItem = [[NSMutableDictionary alloc] init];
int numberOfColumns = sqlite3_column_count(compiledStatement);
NSString *data;
NSString *columnName;
for (int i = 0; i < numberOfColumns; i++) {
columnName = [[NSString alloc] initWithUTF8String:
(char *)sqlite3_column_name(compiledStatement, i)];
data = [[NSString alloc] initWithUTF8String:
(char *)sqlite3_column_text(compiledStatement, i)];
[addressbookItem setObject:data forKey:columnName];
[columnName release];
[data release];
}
[callHistory addObject:callHistoryItem];
[callHistoryItem release];
count++;
}
}
else {
NSLog(#"Failed to retrieve table");
NSLog(#"Error Code: %d", errorCode);
}
sqlite3_finalize(compiledStatement);
}
}
}

Related

How to print all data in given table?

i am iPhone application developer, and now we understood the concept of database.
i want to print all data from database. but i m not getting how can i print all data. here is i pest some code. please give me correct direction to print all data..
for example in sql we print all data as "select * from contact5;" we fire this string. can we done in iPhone coding?
-(IBAction)PrintData:(id)sender
{
NSLog(#"Button Pressed");
sqlite3_stmt *statement1;
NSString *querySQL=#"SELECT * FROM CONTACT5";
const char *query_stmt = [querySQL UTF8String];
sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement1, NULL);
while (sqlite3_step(statement1) == SQLITE_ROW)
{
NSLog(#"Enter in the denger zone");
NSString *idField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement1, 0)];
NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement1, 2)];
NSString *NameField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement1, 1)];
NSString *phoneFiels = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement1, 3)];
NSLog(#"ID is=%#",idField);
NSLog(#"Name is=%#",NameField);
NSLog(#"Address is=%#",addressField);
NSLog(#"Phone No. is=%#",phoneFiels);
[idField release];
[NameField release];
[phoneFiels release];
[addressField release];
}
sqlite3_finalize(statement1);
sqlite3_close(contactDB);
}
thanks in advance.
We are sharing Some come which is using NSMutableDictionary to store your data and you can easily use that code to print your data,where do you want
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "Select * from CalculateTime";
sqlite3_stmt *insertStmt;
if(sqlite3_prepare_v2(database, sql, -1, &insertStmt, NULL) != SQLITE_OK)
NSAssert1(0,#"Error: Failed to prepare statement with message '%s'.",sqlite3_errmsg(database));
arrayCountry = nil;
arrayCountry = [[NSMutableArray alloc]init];
arrayCity = nil;
arrayCity = [[NSMutableArray alloc]init];
arrayFlag = nil;
arrayFlag = [[NSMutableArray alloc]init];
arrayZone = nil;
arrayZone = [[NSMutableArray alloc]init];
NSString *str2;
while(sqlite3_step(insertStmt)==SQLITE_ROW)
{
char *row;
row = (char*)sqlite3_column_text(insertStmt, 0);
if(row != NULL)
{
str2 = [NSString stringWithUTF8String:row];
[arrayCountry addObject:str2];
}
row = (char*)sqlite3_column_text(insertStmt, 1);
if(row != NULL)
{
str2 = [NSString stringWithUTF8String:row];
[arrayCity addObject:str2];
}
row = (char*)sqlite3_column_text(insertStmt, 2);
if(row != NULL)
{
str2 = [NSString stringWithUTF8String:row];
[arrayFlag addObject:str2];
}
row = (char*)sqlite3_column_text(insertStmt, 3);
if(row != NULL)
{
str2 = [NSString stringWithUTF8String:row];
[arrayZone addObject:str2];
}
}
}
Access these dictionary data to print anywhere..

How to return sqlite3_stmt to called object

I am working on an iPhone app. I have created a re usable class in which a sqlite getData method is written. I want to pass a sql statement from my controller and want to get an array back with all of the rows.
Can I get sqlite3_stmt object stored into array and return that array, so at calling point I can cast it and find out each columns value?
My current code is like that :
-(NSMutableArray*)getData:(NSString*) SqlQuery
{
// The Database is stoed in the application bundle
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"sqliteClasses.sqlite"];
if(sqlite3_open([path UTF8String], &contactDB) == SQLITE_OK)
{
const char *sql = (const char*)[SqlQuery UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(contactDB, sql, -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the feeds array
while (sqlite3_step(compiledStatement) == SQLITE_ROW)//(stepResult == SQLITE_ROW)
{
// [arrayRecords addObject:compiledStatement];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(contactDB);
return arrayRecords;
}
The error line is : [arrayRecords addObject:compiledStatement];
How Can I achieve this ? any alternate for implementing this ?
Thanks.
- (NSArray *) getActionWithFilters:(NSDictionary *)dictionary ClassName:(NSString *)className Error:(NSError **)error{
NSString *query = [NSString stringWithFormat:#"SELECT * FROM %# %#",className,[self getFitlerArrayByDictionary:dictionary]];
sqlite3_stmt *statement = [self getItemsWithQuery:query];
NSMutableArray *array = [[NSMutableArray alloc] init];
while (sqlite3_step(statement) == SQLITE_ROW) {
NSMutableDictionary *itemDic = [[NSMutableDictionary alloc] init];
int columns = sqlite3_column_count(statement);
for (int i=0; i<columns; i++) {
char *name = (char *)sqlite3_column_name(statement, i);
NSString *key = [NSString stringWithUTF8String:name];
switch (sqlite3_column_type(statement, i)) {
case SQLITE_INTEGER:{
int num = sqlite3_column_int(statement, i);
[itemDic setValue:[NSNumber numberWithInt:num] forKey:key];
}
break;
case SQLITE_FLOAT:{
float num = sqlite3_column_double(statement, i);
[itemDic setValue:[NSNumber numberWithFloat:num] forKey:key];
}
break;
case SQLITE3_TEXT:{
char *text = (char *)sqlite3_column_text(statement, i);
[itemDic setValue:[NSString stringWithUTF8String:text] forKey:key];
}
break;
case SQLITE_BLOB:{
//Need to implement
[itemDic setValue:#"binary" forKey:key];
}
break;
case SQLITE_NULL:{
[itemDic setValue:[NSNull null] forKey:key];
}
default:
break;
}
}
[array addObject:itemDic];
[itemDic release];
}
return [array autorelease];
}

Sqlite database file is getting encrypted or is not a database

I have no idea what was the problem is in my program. When run this select code for fetching data from SQlite in my program, the first time it crashes with this error message:
kill error while killing target (killing anyway):
warning: error on line 2179 of "/SourceCache/gdb/gdb-1510/src/gdb/macosx/macosx-nat-inferior.c" in function "macosx_kill_inferior_safe": (os/kern) failure (0x5x)
quit
Here's my insert code:
-(id)init {
self = [super init];
sqlite3 *database;
NSMutableArray *locations;
NSString *result = nil;
NSString *dbPath = [self getWritableDBPath];
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
NSString *sqlStr = [NSString stringWithFormat:#"select Longitude,Latitude from myLocation"];
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) {
double longitude = sqlite3_column_double(compiledStatement, 0);
double latitude = sqlite3_column_double(compiledStatement, 1);
NSLog(#"%f , %f",longitude,latitude);
NSString *coords = [[[NSString alloc] initWithFormat:#"%f,%f\n",longitude,latitude] autorelease];
[locations addObject:coords];
NSLog(#"this location :-%#",locations);
//[coords release];
}
result = [locations componentsJoinedByString:#","]; // same as `fake_location`
NSLog(#"this for resulte data :- %#",result);
// 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);
}
sqlite3_close(database);
pointsArray = [[result componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] retain];
pointsArrayIndex = 0;
oldLocationsIndex = 0;
[result release];
oldLocations = [[NSMutableArray alloc] init];
return self;
}
The second time I run my application, it shows me that on the console:
Save Error: file is encrypted or is not a database
What do these errors mean, and how do I solve that?
You need to fire insert query using following:
-(void)insertLocation:(double)latitude withLongitude:(double)longitude
{
sqlite3_stmt *insertStatement = nil;
const char *sql = "insert into UserJourneyLocation(Latitude, Longitude) Values(?,?)";
int returnValue = sqlite3_prepare_v2(database, sql, -1, &insertStatement, NULL);
if(returnValue == SQLITE_OK)
{
sqlite3_bind_double(insertStatement,1,latitude);
sqlite3_bind_double(insertStatement,2,longitude);
if(sqlite3_step(insertStatement)==SQLITE_DONE)
{
//Data;
}
}
sqlite3_finalize(insertStatement);
}
Yes I have.
Go to iphone application document folder
/users/(yourname)/library/application support/iphone simulator/user/application
And remove all Targets.After that Restart your application.

Problem with SQLITE SELECT IN for multiple parameters in iPhone

I have a method, which generates a dictionary of returned values from database:
- (NSDictionary *)getParametersForPreset:(NSUInteger)presetID plants:(NSArray *)plants
{
NSString *loggers = #"";
NSString *invertors = #"";
NSString *plantsList = #"";
const char *sql = "SELECT loggerID, invertorID FROM records WHERE presetID IN (?) AND plantID IN (?)";
BOOL isEmpty = YES;
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) != SQLITE_OK)
{
NSLog(#"Error: '%s'.", sqlite3_errmsg(database));
}
for (int i = 0; i < [plants count]; i++) {
if (i == 0)
{
plantsList = [NSString stringWithFormat:#"'%#'",[[plants objectAtIndex:0] valueForKey:#"id"]];
}
else
{
plantsList = [plantsList stringByAppendingFormat:#",'%#'",[[plants objectAtIndex:i] valueForKey:#"id"]];
}
}
NSLog(#"plants: %#", plantsList);
NSLog(#"preset: %d", presetID);
sqlite3_bind_int(statement, 1, presetID);
sqlite3_bind_text(statement, 2, [plantsList UTF8String], -1, SQLITE_TRANSIENT);
NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] init] autorelease];
int i = 0;
while (sqlite3_step(statement) == SQLITE_ROW)
{
if (i == 0)
{
loggers = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
invertors = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
isEmpty = NO;
i++;
}
else
{
loggers = [loggers stringByAppendingFormat:#",%#",[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]];
invertors = [invertors stringByAppendingFormat:#",%#",[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)]];
}
}
sqlite3_reset(statement);
if (isEmpty == YES)
{
return nil;
}
[dictionary setValue:[NSString stringWithString:[plantsList stringByReplacingOccurrencesOfString:#"'" withString:#""]] forKey:#"plants"];
[dictionary setValue:loggers forKey:#"loggers"];
[dictionary setValue:invertors forKey:#"invertors"];
return dictionary;
}
This query returns me nothing in the code, but then I do the same query in the SQLite Manager in Firefox for the same database, it returns me the correct data. Please, help me find my mistakes, I'm really exhausted of this.
Here is the query I do in Manager:
SELECT loggerID, invertorID FROM records WHERE presetID=1 AND plantID IN ('3','2','1','6','5','4')
And here are logged values from the code:
preset: 1
plants: '3','2','1','6','5','4'
Thanks a lot!
I have recently been working on similar query.
I had a NSMutableArray which stored a list of ID's. I joined them as a string using the function componentsJoinedByString.
I then had a NSString object which held my SQL statement, using the stringWithFormat function.
So your code to generate the SQLite query could be along the lines of:
NSString * query = [NSString stringWithFormat:#"SELECT loggerID, invertorID FROM records WHERE presetID IN (%d) AND plantID IN (%#)",presetID,[plantsList componentsJoinedByString:#","]];
Hope this helps.

Selecting the values from the database through sqlite in iPhone

I am developing an iphone application using sqlite. In that, I have a method to retrieve the values from the table which is shown partially.
NSString *sqlQuery = [NSString stringWithFormat: #”select * from %#”, tableName];
If(sqlite3_prepare_v2(db, [sqlQuery UTF8STRING] , -1, &statement, NULL)== SQLITE_OK)
{
While(sqlite3_step(statement) == SQLITE_ROW)
{
}
Sqlite3_finalize(statement);
}
What my doubt is , inside the while loop we can get the values of the column through the index of the table like the following code.
NSString *addressField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];
address.text = addressField;
For one column we can retrieve like this. In my case, I don’t know the number of columns to be retrieved. In this case, how to iterate over the columns. Please help me out.
Thanks.
NSString *sqlQuery = [NSString stringWithFormat: #”select * from %#”, tableName];
If(sqlite3_prepare_v2(db, [sqlQuery UTF8STRING] , -1, &statement, NULL)== SQLITE_OK)
{
While(sqlite3_step(statement) == SQLITE_ROW)
{
int columnCount = YouKnowColumnCount;
NSMutableArray* array = [[NSMutableArray alloc]init];
for( int i=0; i<columnCount ; ++i) {
[array addObject:[[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, i)]];
}
Sqlite3_finalize(statement);
}
Something like this it depend what do you want to do ...