Apr 30, 2018

Example of Python Script to deal with MySQL

Use

sudo yum -y install MySQL-python

to install MySQLdb for Python in CentOS or Fedora.
(See also:  How to install mysql-python on Linux)

Following is the sample script:



You use "%s" even you insert a float to a table

Example 2:
cursor = conn.cursor (MySQLdb.cursors.DictCursor )
while (True):
 cursor.execute ("delete low_priority from historyv2  where date_add(`date`, interval 366 day)< now() limit 10000;")

 print "result: %d" % conn.affected_rows()
 if(conn.affected_rows()<1) :
  break
 conn.commit()

cursor.close ()
conn.close ()