Like PHP also Python has its own MySQL connector library (see here). However other connectors exist. Here we choose to use MySQLdb (documentation – see also the DB API specification PEP-249). This is fine for Python 3. You can install it via pip: pip install MySQL-python.
Otherwise, to install on Linux (assuming Debian/Ubuntu):
sudo apt-get install build-essential python-dev libmysqlclient-dev
or on Mac OS via port:
sudo port install py-mysql
Tutorial
MySQL Python tutorial
A simple demo program. We assume you have the BSC, ucac2_initial and ascc25 tables are present in the mpe2018db database (see reference page):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb as mdb
con = mdb.connect('localhost', 'mpeusr', 'mpe2018pass', 'mpe2018db')
with con:
cur = con.cursor(mdb.cursors.DictCursor)
cur.execute("SELECT * FROM BSC LIMIT 4")
rows = cur.fetchall()
for row in rows:
print (row["Name"], row["RA"], row["DE"], row["Vmag"])
The demo programs provided show some simple examples using this connector to perform queries also via SID / DIF facilities on the mpe2018db database tables:
shell> cd Soft/Python ls dif_mypydemo1.py mcs_mypydemo1.py mypydemo1.py mypydemo2.py sid_mypydemo1.py
If you want to run the file directly, check they are executable and if not use a chmod +x mypydemo1.py. Otherwise simply run python mypydemo1.py. Also note that for Python 2 you need to change the print statements.
So, assuming you had installed SID (but not DIF / MCS):
./mypydemo1.py ./mypydemo2.py ./sid_mypydemo1.py ./sid_mypydemo2.py
Have a look to the programs source code. A simple (not-parameterised) use case below:
import MySQLdb
# connect to the DB
db = MySQLdb.connect(host="127.0.0.1",
user="mpeusr",
passwd="mpe2018pass",
db="mpe2018db")
# a Cursor object: it will let you execute all the queries you need
cur = db.cursor()
# execute a query to get the column names
cur.execute("describe ucac2_initial")
print ([column[0] for column in cur.fetchall()])
# columns description
print ("\n# ", cur.description)
# another query: use all the SQL you like
qry = "SELECT * FROM ucac2_initial limit 20"
# execute the query
cur.execute(qry)
# get the number of rows in the result set
numrows = int(cur.rowcount)
print ("Returned number of rows: ", numrows)
# get and display one row at a time
for x in range(0, numrows):
print (cur.fetchone())
db.close()
And here is a query using the SID procedures:
import MySQLdb
# connect to the DB
db = MySQLdb.connect(host="127.0.0.1",
user="mpeusr",
passwd="mpe2018pass",
db="mpe2018db")
# a Cursor object: it will let you execute all the queries you need
cur = db.cursor()
qry = "CALL SID.SelectCircleHTM('', 'RAmas/3.6e6 as ra, DECmas/3.6e6 as `Dec`, Vmm/1e3 as V', 'mpe2018db.ascc25', 'htm6', 6, 'RAmas/3.6e6', 'DECmas/3.6e6', 30, -20, 30, 'WHERE Vmm<12000')"
cur.execute(qry)
# first returned string is the call to the procedure that creates the temporary region IDs table SID.sid
row = cur.fetchone()
print (row[0])
# the second string reports the actual join query to select objects in the region
cur.nextset()
row = cur.fetchone()
print (row[0])
# now we move to the returned data set (empty if an output table was given)
cur.nextset()
# get the number of rows in the resultset
numrows = int(cur.rowcount)
print ("\nReturned number of rows: ", numrows)
# get the number of fields and their names
num_fields = len(cur.description)
field_names = [f[0] for f in cur.description]
print ("Field names:\n", field_names, "\n")
# get and display one row at a time
for i in range(0, numrows):
row = cur.fetchone()
v = [row[j] for j in range(0, num_fields)]
print (v)
cur.close()
db.close()
See the demo programs source code for more details.
