pymysql:访问MySQL数据库必要标题
conn=pymysql.connect(host=“localhost”,user=“root”,password=“960127”,db=“excel_mysql”,port=3306,charset=‘utf8’)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
‘’‘host:数据库服务器地址,user:数据库用户名称,password:登录密码,port:数据库访问端口号,db:访问的库名,charset:转码格式’’’
sql = “查询语句”
re = cursor.execute(sql)
data = cursor.fetchall()
fetchone() 这种方法每次只取一条数据
fetchmany()一次多条数据,括号内填入要读取的数据条数。不填则为1条数据,如果读数超过实际条数,只显示实际条数。
fetchall()一次读取全部数据,如果管道内没有数据,则返回空元组或空列表。
sql = “删改查语句”
cursor = cursor.execute(sql)
conn.commit()
删改查语句利用执行语句(commit),作为提交创建的sql语句。