byteSlayer
byteSlayer

Reputation: 2143

Rows order in SQLite Database (iOS)

I have a database with a table called 'connection', for simplicities' sake, let's say I only have one column which is called 'rowName'. Now let's say I add a row with rowName = a; now I add a row with rowName = q, and lastly I add a row with rowName = w (letters are completely random). Now, I irritate thru the results with the statement:

    NSString * queryStatements = [NSString stringWithFormat:@"SELECT rowName,  FROM tableName"];

and using the code:

NSMutableArray * rows = [[NSMutableArray alloc] init]; //create a new array
sqlite3_stmt * statement;
    if(sqlite3_prepare_v2(databaseHandle, [queryStatements UTF8String], -1, &statement, NULL) == SQLITE_OK){
        while (sqlite3_step(statement) == SQLITE_ROW){
           NSString * rowName = [NSString stringWithUTF8String : (char*) sqlite_column_text(statement, 1)];
[rows addObject : connection];
} sqlite3_finalize(statement_;
}

In the array rows, will the object at index 0 be rowName = a, and at index 1 rowName=q, and at index 2 rowName = w? or will it be random? Is there a way to make it not-random?

Also, if i delete a row, will it have any affect on the other rows order?

Upvotes: 0

Views: 1858

Answers (1)

Terry Wilcox
Terry Wilcox

Reputation: 9040

Never depend on a sort order from your database. Always specify one if it is required.

SELECT rowName FROM tableName order by rowName

gives you the data sorted by rowName. If you need a different order, you need another column.

You can also sort your NSArray if need be.

What sort order are you looking for?

Upvotes: 3

Related Questions