1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| // Python3连接操作MySQL import mysql.connector
conn = mysql.connector.connect(host='', port='3306', user='', password='', database='', charset='utf8') cursor = conn.cursor()
cursor.execute("INSERT INTO account ( DATE, CR, DR, SUM, NOTE) VALUES ( CURRENT_DATE(), '1004', '3001', '5', '示例')") conn.commit() print("写入行数:", cursor.rowcount, "\n")
cursor.execute("SELECT * FROM account")
results = cursor.fetchall() for row in results: ID = row[0] DATE = row[1] CR = row[2] DR = row[3] SUM = row[4] NOTE = row[5] print("%s \t%s \t%s \t%s \t%s \t%s" % (ID, DATE, CR, DR, SUM, NOTE))
cursor.close() conn.close()
|