简体   繁体   中英

Why is the maximum value of a column never equal to or greater than 10?

In my application I have table name is dataset_master . And Table has field name is dataset_id , I want to get maximum value of this field so simple I fire following Query with code.

-(NSString *) getLastRowFromDataset_masterTableByProjectID:(NSString *) project_id
{
    NSString *dataset_id = @"";
    sqlite3_stmt *statement = NULL;
    NSString *query = [NSString stringWithFormat:@"SELECT MAX(dataset_id) FROM dataset_master where project_id = ?;"];

    if (sqlite3_prepare_v2(_database, [query UTF8String], -1, &statement, nil) == SQLITE_OK)
    {
        sqlite3_bind_text(statement, 1, [project_id UTF8String], -1, SQLITE_TRANSIENT);

        while (sqlite3_step(statement) == SQLITE_ROW)
        {
            char *datasetIDChars = (char *) sqlite3_column_text(statement, 0);

            if(datasetIDChars != NULL)
                dataset_id  = [[NSString alloc] initWithUTF8String:datasetIDChars];

        }
    }

    return dataset_id;
}

Here project_id is require for me because I need to get dataset_id base on project_id .

All is well but when dataset_id is 10 or more then 10 then I can not get proper maximum value, i mean each time i get 9 .

I don't know what is real problem I simple fire Query but I can not get proper answer.

If it's a text field "9" will be greater than "10". For an id field you should use an integer to get good comparisons.

Then you can retrieve it with something like:

NSNumber *id = [NSNumber numberWithLongLong:sqlite_column_int64(statement, i)];

When you create the sqlite table , use an INTEGER column and not TEXT. Better yet, you can define the column INTEGER PRIMARY KEY and it will return the rowid of the table. More can be read here under ROWIDs and INTEGER PIMARY KEYS heading . Since it's an alias for the rowid, you shouldn't need to assign a value when inserting data - it will get the next highest id.

If you do not want this column to be a unique id, then simply define the column as an integer and supply a value when inserting.

Finally, if you're trying to get the max id, consider using sqlite3_last_insert_rowid after inserting. It will return the last id from the rowid.

Use the following query but be aware this is a bit costly call:

NSString *query = [NSString stringWithFormat:@"SELECT MAX(CAST(dataset_id AS INTEGER)) FROM dataset_master where project_id = ?;"];

Else

Changing datatype of dataset_id to integer should solve your issue.

For Ranju Patel's query:

  • Why MAX function would not work with TEXT datatype?

In case of TEXT datatype, the sorting order would be ASCII sorting order,

where in "9" > "10" , "09" < "10" , [Ref: http://en.wikipedia.org/wiki/ASCII#Order], Hence the MAX function would not work if the data stored is "9", "10".

on the other hand,

if the data sotred is "09", "10", and your query is:

NSString *query = [NSString stringWithFormat:@"SELECT MAX(dataset_id) FROM dataset_master where project_id = ?;"];

the result you would get is "10".

  • Would my answer be valid for long time?

It would work if you don't have too many data, else it would slow you down, Hence I have mentioned it as: its very costly to call CAST function in the raw SELECT query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM