本文摘自php中文网,作者巴扎黑,侵删。
这篇文章主要为大家详细介绍了Python3.6简单操作Mysql数据库,具有一定的参考价值,感兴趣的小伙伴们可以参考一下本文为大家分享了Python3.6操作Mysql数据库的具体实例,供大家参考,具体内容如下
安装pymysql
参考https://github.com/PyMySQL/PyMySQL/
实例一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import pymysql
conn = pymysql.connect(host = '127.0.0.1' , user = 'root' , passwd = '123456' , db = 'demo' )
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
effect_row = cursor.execute( "select * from course" )
print (effect_row)
result = cursor.fetchall()
result = cursor.fetchone()
result = cursor.fetchone()
conn.commit()
cursor.close()
conn.close()
|
实例二
1 2 3 4 5 6 7 8 9 10 11 12 | import pymysql
conn = pymysql.connect(host = '127.0.0.1' , user = 'root' , passwd = '123456' , db = 'demo' )
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
effect_row = cursor.execute( "insert into course(cou_name,time) values(%s,%s)" , ( "Engilsh" , 100 ))
print (effect_row)
conn.commit()
cursor.close()
conn.close()
|
实例三
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import pymysql.cursors
connection = pymysql.connect(host = 'localhost' ,
user = 'user' ,
password = 'passwd' ,
db = 'db' ,
charset = 'utf8mb4' ,
cursorclass = pymysql.cursors.DictCursor)
try :
with connection.cursor() as cursor:
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
cursor.execute(sql, ( 'webmaster@python.org' , 'very-secret' ))
connection.commit()
with connection.cursor() as cursor:
sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
cursor.execute(sql, ( 'webmaster@python.org' ,))
result = cursor.fetchone()
print (result)
finally :
connection.close()
|
以上就是介绍Python3.6简单操作Mysql数据库的方法的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
Python实现超简单的视频对象提取功能
业余学Python有用吗
Python中tornado下websocket客户端编程的介绍
Python续行符是什么
用matplotlib如何绘制条形图、直方图和散点图
Python针对给定字符串求解所有子序列是否为回文序列的方法
数学建模可以用Python吗
怎么运行 桌面上的Python快捷方式
Python怎么变成中文版
Python和大数据有什么关系
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » 介绍Python3.6简单操作Mysql数据库的方法