!/usr/bin/env python
coding:utf-8
@Time : 2021/6/29 16:31
@Author: sandra
@File : pymysql.py
import pymysql
• MySQLdb.connect 建立和MySQL 数据库的连接
• cursor=conn.cursor() 通过上一步建立的连接获取个cursor对象
• cursor.execute(sql) 执行SQL语句, 但返回结果不是SQL执行的结果,是此SQL执行后收影响的行数
• cursor.fetchall(): 获取SQL执行的所有结果,返回 结果是个嵌套的元组
• cursor.fetchone()获取SQL 执行的结果,只获取第一条
• cursor.close() , conn.close() : 关闭连接和cursor
conn = pymysql.connect(host='rm-bp1199ayx32ah1u80gM.mysql.rds.aliyuncs.com',
port=3306,
user='books_dev',
password='Y4GPcRojd8wfZzIqMLM7VUle9OS356',
database='books',
charset='utf8')
cursor = conn.cursor()
sql = 'show databases;'
res = cursor.execute(sql)
print(cursor.fetchall())
print(cursor.fetchone())
select_sql = "SELECT * FROM books
.ecs_users
WHERE user_id
= 11260482"
res = cursor.execute(select_sql)
print(cursor.fetchall())
网友评论