python 对 mysql 操作

安装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()

 

上一篇:使用Python读取本地文件的4种方式


下一篇:7、java jdbc如何连接oracle12