I have just upgraded to MySql 1.2.0 for python 2.4 and am encountering the following glitch with the automatic type conversion. In the first two calls
the result is returned as a long integer (which is what I was expecting). In the last call it is being returned as a string. Hmmm.
>>> d.cur.execute("select Number from Application limit 1;")
1L
>>> d.cur.fetchone()[0]
10000014L #INTEGER
>>>
>>> d.cur.execute("select Number from Application where Number is not null limit 2;")
2L
>>> d.cur.fetchone()[0]
10000014L #INTEGER
>>>
>>> d.cur.execute("select ContractNumber from FactApplication where ContractNumber is not null limit 1;")
1L
>>> d.cur.fetchone()[0]
'10000014' #STRING!
I have a similar problem with a call that returns the average of a column of numbers.
self.cur.execute("select avg(Number) from Application;")
In the new version the result is being returned as a string, whereas previously it was being returned as a number.
Cheers :-)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
>>> d.cur.execute("select Number from Application limit 1;")
1L
>>> d.cur.fetchone()[0]
10000014L #INTEGER
>>>
>>> d.cur.execute("select Number from Application where Number is not null limit 2;")
2L
>>> d.cur.fetchone()[0]
10000014L #INTEGER
>>>
>>> d.cur.execute("select Number from Application where Number is not null limit 1;")
1L
>>> d.cur.fetchone()[0]
'10000014' #STRING!
Duh!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You shouldn't have the trailing semicolon; it's not needed, and you can't have multiple statements in a single execute() anyway. (Well, it's possible, but not normally.)
In your tests, also try printing d.cur.description. This is a sequence of tuples, one for each column. Item 1 will be the column type that the MySQL C API returns. In other words, what is d.cur.description[0][1]? The values are defined in MySQLdb.constants.FIELD_TYPE:
I have just upgraded to MySql 1.2.0 for python 2.4 and am encountering the following glitch with the automatic type conversion. In the first two calls
the result is returned as a long integer (which is what I was expecting). In the last call it is being returned as a string. Hmmm.
>>> d.cur.execute("select Number from Application limit 1;")
1L
>>> d.cur.fetchone()[0]
10000014L #INTEGER
>>>
>>> d.cur.execute("select Number from Application where Number is not null limit 2;")
2L
>>> d.cur.fetchone()[0]
10000014L #INTEGER
>>>
>>> d.cur.execute("select ContractNumber from FactApplication where ContractNumber is not null limit 1;")
1L
>>> d.cur.fetchone()[0]
'10000014' #STRING!
I have a similar problem with a call that returns the average of a column of numbers.
self.cur.execute("select avg(Number) from Application;")
In the new version the result is being returned as a string, whereas previously it was being returned as a number.
Cheers :-)
Sorry,
The correct code should have been
>>> d.cur.execute("select Number from Application limit 1;")
1L
>>> d.cur.fetchone()[0]
10000014L #INTEGER
>>>
>>> d.cur.execute("select Number from Application where Number is not null limit 2;")
2L
>>> d.cur.fetchone()[0]
10000014L #INTEGER
>>>
>>> d.cur.execute("select Number from Application where Number is not null limit 1;")
1L
>>> d.cur.fetchone()[0]
'10000014' #STRING!
Duh!
You don't say what MySQL version you are using.
You shouldn't have the trailing semicolon; it's not needed, and you can't have multiple statements in a single execute() anyway. (Well, it's possible, but not normally.)
In your tests, also try printing d.cur.description. This is a sequence of tuples, one for each column. Item 1 will be the column type that the MySQL C API returns. In other words, what is d.cur.description[0][1]? The values are defined in MySQLdb.constants.FIELD_TYPE: