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语句。
报错:
Traceback (most recent call last): File “E:/untitled1/lvs_rr.py”, line 15, in password=“1”, db=“mysql”, port=3306) File “E:\untitled1\lib\site-packages\pymysql_init_.py”, line 94, in Connect return Connection(*args, **kwargs)
解决方法 添加用户允许从任何主机连接到mysql服务器
GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘password’ WITH GRANT OPTION;
刷新用户更改:
FLUSH PRIVILEGES;