Seven steps for Python to connect to a database
1. Import Module
>>> Import mysqldb #因为里面有大小写, you can use as MySQL to make it easier
2. Create a connection
>>> conn = MySQLdb.connect (host= ' 59.110.12.72 ', port=3306, user= ' ****** ', passwd= ' ****** ', db= ' KK3 ',
charset= ' UTF8 ')
3. Fetching cursors
>>> cur = conn.cursor ()
4. Perform operation
>>> cur.execute (' select * from Accesslog ')
>>> Cur.fetchall () #用它来读取, data extracted from the database
>>> Cur.execute ("INSERT into Accesslog (log_date,ip,url,status) VALUES (' 2016-10-11 ', ' 2,2,2,2 ',
' http://www.baidu.com/a ', (); ")
5. Data submission/Data reading
@select only reads data, does not affect the database, does not need to submit data
@update, Delete,insert These types of operations, you need to submit data
>>> conn.commit () #插入的数据需要commit下才能提交到数据库里面
6. Close the cursor
>>> cur.close ()
7. Close the connection
>>> conn.close ()
Python's operation with MySQL