python连接mysql

import pymysql  

# 创建数据库连接(方法一)
# conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='test', charset='utf8')

# 也可以使用字典进行连接参数的管理(方法二)
config = {
    'host': '127.0.0.1',
    'port': 3306,
    'user': 'root',
    'passwd': '123456',
    'db': 'test',
    'charset': 'utf8'
}
conn = pymysql.connect(**config)

# 初始化游标(创建游标)
cursor = conn.cursor()  

# 返回执行结果
# result = cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
# result = cursor.execute('insert into user (id, name) values (%s, %s)', ['2', 'Bob'])
# result = cursor.execute("update user set name = 'Bob' where id = %s", (2))
# result = cursor.execute("delete from user where id = %s", (2))
# result=cursor.fetchmany(cursor.execute("select * from user where id = %s",(2)))

# 执行查询,并返回受影响的行数
effect_row = cursor.execute("select * from user")
# 返回所有的结果集 
result = cursor.fetchall()
# 打印
for k,v in result:
    print(k,v)
print(list(map(lambda x:x%2,range(3))))
print("{0}{0}".format("py"))
#打印查询结果  
# for r in result:
#     print(r); 
    
# 提交事务
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值