安装pymysql:
pip install pymysql
操作:
import pymysql as db conn=db.connect(host='192.168.73.131',user='wzh',passwd='123456',port=3306,database='TestDB',charset='utf8') try: cur=conn.cursor() cur.execute("insert into users(name,age) values('xx',22)") conn.commit() cur.execute('select * from users') for item in cur.fetchall(): print(f'{item[1]}{item[2]}') except: print('error') finally: cur.close() conn.close()