mysql和Python3 连接 pymysql 模块

安装模块:pip install pymysql

import pymysql

conn=pymysql.connect(host='127.0.0.1',port=3306, user='root', passwd='12345qq',db='project')
 #建立连接 
 host:'127.0.0.1,不用联网也可访问数据库
 port:2206
 user:登录mysql的用户名,可以通过 select user(); 语句查看用户名
 passwd: 密码
 db:要连接的数据库名称
 
cur=conn.cursor() #建立游标
cur.execute('select * from project.wc limit 5')  #execute方法执行select语句

for r in cur.fetchall():
    print(r)
cur.close() #关闭
conn.close()

#输出:
('767', 33, '', 'Absolutely wonderful - silky and sexy and comfortable', 4, 1, 0, 'Initmates', 'Intimate', 'Intimates')
('1080', 34, '', 'Love this dress!  it\'s sooo pretty.  i happened to find it in a store, and i\'m glad i did bc i never would have ordered it online bc it\'s petite.  i bought a petite and am 5\'8".  i love the length on me- hits just a little below the knee.  would definitely be a true midi on someone who is truly petite.', 5, 1, 4, 'General', 'Dresses', 'Dresses')
('1077', 60, 'Some major design flaws', 'I had such high hopes for this dress and really wanted it to work for me. i initially ordered the petite small (my usual size) but i found this to be outrageously small. so small in fact that i could not zip it up! i reordered it in petite medium, which was just ok. overall, the top half was comfortable and fit nicely, but the bottom half had a very tight under layer and several somewhat cheap (net) over layers. imo, a major design flaw was the net over layer sewn directly into the zipper - it c', 3, 0, 0, 'General', 'Dresses', 'Dresses')
										...

个人觉得用select选取数据比 pandas 的groupby 好用 XD

将excel数据读入mysql的方法见:
https://blog.csdn.net/weixin_44595372/article/details/88723191

猜你喜欢

转载自blog.csdn.net/weixin_44595372/article/details/88944359