当前位置:Gxlcms > 数据库问题 > 20200204python连接数据库

20200204python连接数据库

时间:2021-07-01 10:21:17 帮助过:4人阅读

1、安装python驱动

  pip3 install pymysql

2、数据库操作命令

https://www.cnblogs.com/UncleYong/p/7753188.html

3、连接测试

查询 端口mysql>show global variables like ‘port‘;

import pymysql
from pymysql.cursors import DictCursor
coon = pymysql.connect(host=‘192.168.72.128‘,port=3306,user=‘root‘,passwd=‘123456‘,db=‘mysql‘,charset=‘utf8‘) # 创建数据库连接
cur = coon.cursor(DictCursor) # 建立游标,指定游标类型,返回字典
sql=‘select * from user limit 2;‘ # 操作语句,只查询前两行
cur.execute(sql) # 执行sql语句
res = cur.fetchall() # 获取查询的所有结果
print(res) # 打印结果
cur.close() # 关闭游标
coon.close() # 关闭连接

 

20200204python连接数据库

标签:fetch   user   ict   执行   bsp   返回字典   hal   UNC   命令   

人气教程排行