连接mysql

发布时间:2020-06-17 16:33:14 作者:leiwenbin627
来源:网络 阅读:184

pip install pymysql

import pymysql
conn=pymysql.connect(host='192.168.0.6',port=3306,user='root',password='123456',db='oldboydb')#创建连接
cursor=conn.cursor()#创建游标

#执行并返回受影响的行数
result=cursor.execute('insert into student(name,age,register_date) values ("haha","22","2016-03-02")')
print('受影响的行数:',result)
new_id=cursor.lastrowid#最近的自增id
print("最近的自增id:",new_id)#不提交id也会自增上去

#执行一行
update=cursor.execute("update student set name='Laha' where name='haha' ")

delete=cursor.execute("delete from student where name='Laha'")

#执行多行
result_many=cursor.executemany("insert into B(b) values(%s)",['7','8','9','10','11','12'])
print("result many:",result_many)

#查询
effect_row=cursor.execute('select * from B')
row_1=cursor.fetchone()#获取一行
print("第一行查询结果:",row_1)
row_2=cursor.fetchmany(2)#获取多行
print("第二行第三行结果:",row_2)
row_all=cursor.fetchall()#获取全部
print("剩下的结果:",row_all)

conn.commit()# 递交之后就会写入数据库 不提交就不会 默认事务
cursor.close()#关闭游标
conn.close()#关闭连接

推荐阅读:
  1. mysql 自连接
  2. python连接mysql

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

连接mysql

上一篇:framework层和native层实现联网控制(iptable方式)

下一篇:java的学习路线

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》