I'm a complete Python/MySQL newbie - trying to retrieve and manipulate data from a MySQL DB over a VPN/LAN from an Ubuntu/Python shell.
I've imported all the modules I think I need - MySQLdb, decimal, scipy, scipy.stats, numpy. I have been able to open the connector to the MySQL DB and retrieve the data of interest. My problem is as follows
1. I open a connection with
mydb = MySQLdb.connect (host = "xxx", port = yyy, user = "zzz", passwd = "nnnn", db = "whatineed" )
cursor = mydb.cursor()
2. For some reason, each entry is a tuple though I am retrieving a single column from the MySQL table with a command
cursor.execute( "SELECT difference FROM growth_record")
3. Data from the DB comes back in the format
((Decimal('-3.0600'),), (Decimal('-0.8500'),), (Decimal('-1.1900'),), (Decimal('-1.7000'),), (Decimal('0.480 0'),),...
4. I am able to pull out the individual values by doing
result = cursor.fetchall ()
for i in range(len(resul t)):
a = result[i][0]
OR to get a floating point result
for i in range(len(resul t)):
a = float(str(resul t[i][0]))
5. My problems
a) I am not able to create an array with the assignment
a[i] = result[i][0] - keep getting a type mismatch. I'd like to create an array with the direct results without the Tuple returned by the DB.
b) I am not able to perform statistical analyses from scipy.stats (e.g. mean) with the results. The functions keep expecting floating point arguments, the numbers returned from the DB are decimal numbers.
Is there an obviously simple way to do either?
Thank you
Bapi
I've imported all the modules I think I need - MySQLdb, decimal, scipy, scipy.stats, numpy. I have been able to open the connector to the MySQL DB and retrieve the data of interest. My problem is as follows
1. I open a connection with
mydb = MySQLdb.connect (host = "xxx", port = yyy, user = "zzz", passwd = "nnnn", db = "whatineed" )
cursor = mydb.cursor()
2. For some reason, each entry is a tuple though I am retrieving a single column from the MySQL table with a command
cursor.execute( "SELECT difference FROM growth_record")
3. Data from the DB comes back in the format
((Decimal('-3.0600'),), (Decimal('-0.8500'),), (Decimal('-1.1900'),), (Decimal('-1.7000'),), (Decimal('0.480 0'),),...
4. I am able to pull out the individual values by doing
result = cursor.fetchall ()
for i in range(len(resul t)):
a = result[i][0]
OR to get a floating point result
for i in range(len(resul t)):
a = float(str(resul t[i][0]))
5. My problems
a) I am not able to create an array with the assignment
a[i] = result[i][0] - keep getting a type mismatch. I'd like to create an array with the direct results without the Tuple returned by the DB.
b) I am not able to perform statistical analyses from scipy.stats (e.g. mean) with the results. The functions keep expecting floating point arguments, the numbers returned from the DB are decimal numbers.
Is there an obviously simple way to do either?
Thank you
Bapi
Comment