python怎么操作mysql

python怎么操作mysql?

先附一段代码:

import pymysql
import redis
import datetime
def truncateDateBase():
    connect = pymysql.Connect(
        host='10.1.1.123',
        port=1111,
        user='user_name',
        passwd='password',
        db='database_name',
        charset='utf8'
    )
    cursor = connect.cursor()
    tableList = ["table1",
                 "table2",
                 table3"]
    for tableName in tableList:
        cursor.execute("delete from " + tableName)
        connect.commit()
        print(tableName + "表格数据已经清空")
    connect.close()

需要注意的地方:

(1)建立数据库连接

(2)执行数据库操作

(3)提交执行的结果(connect.commit())

  (4)关闭数据库(connect.close())

你可能感兴趣的:(python怎么操作mysql)