py连接数据库

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()
'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值