py连接数据库
18,py 连接 MySQL ----- pip install pymysql
以太网--自动获取IP
'''
'''
PYMYSQL 连接 MySQL
PYMYSQL 操作 MYSQL增删改查
防止SQL注入
PYMYSQL---PYTHON 操作MYSQL 模块
PIP INSTALL MYSQL
connect = Connection = Connect
'''
import pymysql
#创建连接
conn = pymysql.connect(host = '127.0.0.1',port = 3306,user = 'root',password = 'root',db = 'gly',charset = 'utf8')
#创建游标
cursor = conn.cursor()
#执行SQL语句,并返回受影响的行数
#effect_r = cursor.execute("select * from emp")
#print(effect_r)
#print(cursor.fetchall()) 显示所有行
#print(cursor.fetchmany(3)) 显示前三行
#print(cursor.fetchone()) 显示一行
#effect_row = cursor.execute("insert into emp(empno,ename,sal) values(9999,'lhy',2)") 插入
#conn.commit()
try:
effect_row = cursor.executemany("insert into emp(empno,ename,sal) values (%s,%s,%s)",[(8,'ll',7),(9,'nn',3)])
conn.commit()
except(Exception):
print("插入错误")
finally:
cursor.close()
conn.close()
'''
py连接数据库
最新推荐文章于 2024-07-15 17:46:33 发布