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 04-02-2010, 02:23 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 1
oregano is on a distinguished road
Default SQLite text bind not working properly

Hello all -

So I've scoured forums and Google, but I've been unable to figure out why this isn't working. Maybe someone here can help.

I have a sqlite call, and it opens the database, creates the array, etc. Everything is great. If I pass an "ORDER BY" in my sqlite const char string, it sorts as I expect.

However, I'd like to change the sort order programmatically, so naturally I want to use a variable so I can manipulate that outside the const char. So, I create an NSString, give it the column value I want, and test it.

No sorting change.

No errors, in console or anything, but no sorting change either. ???

Here's the relevant function that I'm trying to fix:

Code:
// Open the database connection and retrieve primary keys for all entries.
- (void)initializeDatabase {
     NSMutableArray *uorArray = [[NSMutableArray alloc] init];
     self.uors = uorArray;
     [uorArray release];
     
     // sort variable will go here, I'm testing with it coded as "Description". Description is a valid column name in my db.
     NSString *orderBy = @"Description";
     
     // The database is stored in the application bundle.
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *path = [documentsDirectory stringByAppendingPathComponent:@"uors.sqlite"];
     // Open the database. The database was prepared outside the application.
     if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
          
          // Get the primary key for all uors.
                // If I change the end of this line to ORDER BY Description it works perfectly. ???
          const char *sql = "SELECT pk FROM Vio_Code_order ORDER BY ?";
          sqlite3_stmt *statement;

          if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {

               // Bind the sort variable
               sqlite3_bind_text(statement, 1, [orderBy UTF8String], -1, SQLITE_TRANSIENT);
               
               // We "step" through the results - once for each row.
               while (sqlite3_step(statement) == SQLITE_ROW) {
                    // The second parameter indicated the column index into the result set.
                    int primaryKey = sqlite3_column_int(statement, 0);                    

                    UORs *td = [[UORs alloc] initWithPrimaryKey:primaryKey database:database];
                    [uors addObject:td];
                    [td release];
               }
          }
          // "Finalize" the statement - releases the resources associated with the statement.
          sqlite3_finalize(statement);
     } else {
     // Even though the open failed, call close to properly clean up resources.
     sqlite3_close(database);
     NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
     // Additional error handling, as appropriate...
     }
}
Any thoughts? Thanks in advance!
oregano is offline   Reply With Quote
Old 04-04-2010, 05:41 PM   #2 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 183
bugnote is on a distinguished road
Default

removed as my initial comment was rubbish ;-)


looks ok to me, the only thing I have different in my similiar code snippets is to use ADC or DSC with order by.

can you just use a sort descriptor on uors for the time being?
__________________
Burnsoft Ltd. Apps for your iOS device.

Last edited by bugnote; 04-05-2010 at 04:30 AM.
bugnote is offline   Reply With Quote
Old 09-09-2011, 02:54 AM   #3 (permalink)
eva
Registered Member
 
Join Date: Sep 2011
Posts: 1
eva is on a distinguished road
Default

Never mind, got it working! It didn't use order by, but ill take a look at it later today.
Eva

Last edited by eva; 09-09-2011 at 12:18 PM.
eva is offline   Reply With Quote
Reply

Bookmarks

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: 313
7 members and 306 guests
chemistry, Fstuff, heshiming, iAppDeveloper, jbro, kapps11, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,648
Threads: 94,112
Posts: 402,872
Top Poster: BrianSlick (7,990)
Welcome to our newest member, brandon6031
Powered by vBadvanced CMPS v3.1.0

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