Python使用数据库
# pip3 install mysql-connector
import mysql.connector
conn = mysql.connector.connect(host='192.168.1.10', user='mogu', password='123456', database='mogu')
cursor = conn.cursor()
# 创建表
cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
# 插入一行记录
cursor.execute('insert into user (id, name) values (%s, %s)', ('1', 'Michael'))
print('rowcount =', cursor.rowcount)
conn.commit()
cursor.close()
# 运行查询:
cursor = conn.cursor()
cursor.execute('select * from user where id = %s', ('1',))
values = cursor.fetchall()
print(values)
cursor.close()
# 关闭数据库连接
conn.close()