Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-19-2011, 01:02 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 2
SoloDolo is on a distinguished road
Default 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?
SoloDolo is offline   Reply With Quote
Old 12-19-2011, 01:43 AM   #2 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 25
AamirKhurshid is on a distinguished road
Default

Quote:
Originally Posted by SoloDolo View Post
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?
Hi you can check this link for database.
iPhone Development: Database with iPhone
and
iPhone SDK Tutorial: Reading data from a SQLite Database | dBlog.com.au

and for dictionary
Code:
// 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);

Regards,
Aamir
AamirKhurshid is offline   Reply With Quote
Old 12-19-2011, 08:49 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2011
Posts: 2
SoloDolo is on a distinguished road
Default

Quote:
Originally Posted by AamirKhurshid View Post
Hi you can check this link for database.
iPhone Development: Database with iPhone
and
iPhone SDK Tutorial: Reading data from a SQLite Database | dBlog.com.au

and for dictionary
Code:
// 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);

Regards,
Aamir
thz u so much AamirKhurshid, let me try
SoloDolo is offline   Reply With Quote
Reply

Bookmarks

Tags
dictionary, objective c, sqlite database

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 393
13 members and 380 guests
7twenty7, AppsBlogger, Creativ, Dalia, David-T, Duncan C, HemiMG, heshiming, LunarMoon, Murphy, pbart, teebee74, Tomsky
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,127
Posts: 402,915
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:50 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0