sandwichstealer
Posts: 2
Joined: Thu Mar 14, 2019 12:25 am

How to copy MySQL table into Python variables

Sun Mar 24, 2019 12:53 am

I have a working connection between Python and MySQL. Now I would like to copy a table from MySQL into Python. The first column of a 20 row table into an array.

The internet has lots of read/print examples but no copy into a variable example.

If someone knows how to write back the same data to same table that would be great too. I thought it would be easy to read and write to a database table but it doesn’t seem to be the case.

Thanks

ghp
Posts: 1517
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: How to copy MySQL table into Python variables

Sun Mar 24, 2019 11:04 am

Reading data from mysql is explained in many places, e.g. in
https://www.w3schools.com/python/python ... select.asp

An example is

Code: Select all

mycursor = mydb.cursor()
sql = "select A,B from table_AB"
mycursor.execute(sql)
myresult = mycursor.fetchall()
print ("myresult", myresult)
The printout shows:
myresult [('A_value_1', 1), ('A_value_2', 2),('A_value_3', 3 ), ... ]

So myresult is an array with tuples containing the data.

Return to “Python”