Posts

Showing posts with the label Python SQL

Python and MySQL

Although PHP is used a lot for SQL scripts, Python is a fine alternative. The commonly used SQL library in Python is MySQLdb.  MySQLdb is an thread-compatible interface to the popular MySQL database server that provides the Python database API. ( from the documentation ) An example showing the guts of a Python MySQL script. import MySQLdb conn = MySQLdb.connect (host = " localhost ", user = " root ", passwd = " admin ", db = " databasename ") db = conn.cursor() db.execute ( " SELECT * FROM table1 ") data = db.fetchall() #do something with the data db.close () conn.commit () conn.close () Looking over the code example : After the import is a connect request to an SQL database.  The database in on the localhost and has the default username and password.  The the actual database name must be specified.  MySQL databases can have multiple databases on one host. Note that on all the code accessing data or running commands on the data