美文网首页
使用 PyMySQL 操作 MySQL

使用 PyMySQL 操作 MySQL

作者: MayerBin | 来源:发表于2019-08-15 23:26 被阅读0次

安装

pip  install pymysql

创建数据库连接

import pymysql
conn = pymysql.connect(
    host = 'localhost',
    port = 3306,
    user = 'root',
    password = 'root',
    db = 'demo',
    charset = 'utf8'
)

执行 SQL

# 获取光标
cur = conn.cursor()

# 执行查询 SQL
cur.execute('select * from company_sql')

# 获取所有数据(要先执行楼上的查询sql)
data = cur.fetchall()

# 获取单条数据
cursor.fetchone()
# 获取前N条数据
cursor.fetchmany(3)

# 以python的方式操作
for i in data:
    if "150-500人" in i[4]:
        print(i)

# 关闭游标
cur.close()
conn.close()

相关文章

网友评论

      本文标题:使用 PyMySQL 操作 MySQL

      本文链接:https://www.haomeiwen.com/subject/oskxsctx.html