使用pymysql驱动访问数据库,所以前提要装好了pymysql。(可通过pip install pymysql安装)

 

#-*- coding:utf-8 -*-

import pymysql

 

#连接数据库

connection=pymysql.connect(db='数据库库名', user='用户名', password='密码', host='IP', port=端口号,charset='utf8')

 

#通过cursor创建游标

cursor=connection.cursor()

#执行数据查询

cursor.execute(“select * from 表名 limit 10”)

 

#查询多条数据

result=cursor.fetchall()

for data in result:

    print(data)

 

#查询单条数据

result=cursor.fetchone()

print(result)

 

#关闭数据连接

connection.close()

                           

 

 

                                                                                                                内容参考于网络

 

相关文章:

  • 2022-02-26
  • 2021-05-23
  • 2021-12-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-25
  • 2021-09-26
猜你喜欢
  • 2022-12-23
  • 2021-12-14
  • 2022-12-23
  • 2021-06-13
  • 2021-10-21
  • 2022-12-23
相关资源
相似解决方案