how to create dictionary for iphone with Objective C and SQLite databases?
hello all;
I just start learn objective c, I want to create simple dictionary for iphone with Objective C and SQLite databases. but I don't know where to start. plz give me some information how to create?
I just start learn objective c, I want to create simple dictionary for iphone with Objective C and SQLite databases. but I don't know where to start. plz give me some information how to create?
// Setup the database object
sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = [query UTF8String];
sqlite3_stmt *compiledStatement;
// Database handle , SQL statement, UTF-8 encoded , Maximum length of zSql in bytes. , OUT:Statement handle , OUT: Pointer to unused portion of zSql
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
// sqlite3_data_count returns the number of values (columns) of the currently executing statement. With no results it returns 0.
NSInteger dataCount = sqlite3_data_count(compiledStatement);
// sqlite3_column_count on the other hand, returns always the number of columns, with or without results.
NSInteger columnCount = sqlite3_column_count(compiledStatement);
NSArray* keyArray = [[NSArray alloc] init ];
NSArray* valueArray = [[NSArray alloc ] init ];
NSDictionary* dic;
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
@try {
// loop through column
for(int i = 0; i< dataCount ;i++)
{
// Read the data from the result row, key is columnName and value is value
NSString* key = [NSString stringWithUTF8String:(char *)sqlite3_column_name(compiledStatement, i)];
NSString* value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, i)];
if(value == NULL || [value isEqualToString:@""])
{
value = @"";
}
keyArray = [keyArray arrayByAddingObject:key];
valueArray = [valueArray arrayByAddingObject:value];
}// end for loop of column count
}
@catch (NSException *exception)
{
NSLog(@"%@,%@",[exception name],[exception reason]);
}
dic= [NSDictionary dictionaryWithObjects:(id*) valueArray forKeys:(id *) keyArray count:[keyArray count]];
} // End While
// Inserted
NSInteger lastInsertedID = sqlite3_last_insert_rowid(database);
NSInteger rowsUpdated = sqlite3_total_changes(database);
// add also these to Dictionary
// dic setValue:<#(id)#> forKey:<#(NSString *)#>
}else
{
sqlite3_errmsg(database);
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}else
{
sqlite3_close(database);
NSAssert1(0,@"Failed to open database. '%s'",sqlite3_errmsg(database));
}
sqlite3_close(database);
// Setup the database object
sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK)
{
// Setup the SQL Statement and compile it for faster access
const char *sqlStatement = [query UTF8String];
sqlite3_stmt *compiledStatement;
// Database handle , SQL statement, UTF-8 encoded , Maximum length of zSql in bytes. , OUT:Statement handle , OUT: Pointer to unused portion of zSql
if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)
{
// sqlite3_data_count returns the number of values (columns) of the currently executing statement. With no results it returns 0.
NSInteger dataCount = sqlite3_data_count(compiledStatement);
// sqlite3_column_count on the other hand, returns always the number of columns, with or without results.
NSInteger columnCount = sqlite3_column_count(compiledStatement);
NSArray* keyArray = [[NSArray alloc] init ];
NSArray* valueArray = [[NSArray alloc ] init ];
NSDictionary* dic;
// Loop through the results and add them to the feeds array
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
@try {
// loop through column
for(int i = 0; i< dataCount ;i++)
{
// Read the data from the result row, key is columnName and value is value
NSString* key = [NSString stringWithUTF8String:(char *)sqlite3_column_name(compiledStatement, i)];
NSString* value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, i)];
if(value == NULL || [value isEqualToString:@""])
{
value = @"";
}
keyArray = [keyArray arrayByAddingObject:key];
valueArray = [valueArray arrayByAddingObject:value];
}// end for loop of column count
}
@catch (NSException *exception)
{
NSLog(@"%@,%@",[exception name],[exception reason]);
}
dic= [NSDictionary dictionaryWithObjects:(id*) valueArray forKeys:(id *) keyArray count:[keyArray count]];
} // End While
// Inserted
NSInteger lastInsertedID = sqlite3_last_insert_rowid(database);
NSInteger rowsUpdated = sqlite3_total_changes(database);
// add also these to Dictionary
// dic setValue:<#(id)#> forKey:<#(NSString *)#>
}else
{
sqlite3_errmsg(database);
}
// Release the compiled statement from memory
sqlite3_finalize(compiledStatement);
}else
{
sqlite3_close(database);
NSAssert1(0,@"Failed to open database. '%s'",sqlite3_errmsg(database));
}
sqlite3_close(database);