</pre><pre name="code" class="python">##设置编码格式为utf8避免中文乱码
encoding:utf8
import pymysql
查询
def select():
conn = pymysql.connect(user='root', passwd='你的密码',
host='localhost', db='test',charset='utf8')
cur = conn.cursor()
cur.execute("SELECT * FROM user")
for r in cur:
print("row_number:" , (cur.rownumber) )
print("id:"+str(r[0])+" name:"+str(r[1])+" password:"+str(r[2]))
cur.close()
conn.close()
插入
def insert(name,pwd):
conn = pymysql.connect(user='root', passwd='你的密码',
host='localhost', db='test',charset='utf8')
cur = conn.cursor()
sql= "INSERT INTO user (Name,Password) VALUES ('"+name+"','"+pwd+"')"
print(sql)
sta=cur.execute(sql)
if sta==1:
print('Done')
else:
print('Failed')
conn.commit()
cur.close()
conn.close()
更新
def update(name,pwd):
conn = pymysql.connect(user='root', passwd='你的密码',
host='localhost', db='test',charset='utf8')
cur = conn.cursor()
sql= "UPDATE USER SET PASSWORD='"+pwd+"' WHERE NAME='"+name+"'""'"
print(sql)
sta=cur.execute(sql)
if sta==1:
print('Done')
else:
print('Failed')
conn.commit()
cur.close()
conn.close()
删除
def delete(name):
conn = pymysql.connect(user='root', passwd='你的密码',
host='localhost', db='test',charset='utf8')
cur = conn.cursor()
sql = "DELETE FROM USER WHERE Name='"+name+"'"
print(sql)
sta=cur.execute(sql)
if sta==1:
print('Done')
else:
print('Failed')
conn.commit()
cur.close()
conn.close()
调用函数进行操作即可
抓取到数据库的主要操作
"""update music_review_douban set review_title = %s,review_content = %s,review_author = %s,
review_music = %s,review_time = %s,review_url = %s
where review_url = %s""",
(item['review_title'],
item['review_content'],
item['review_author'],
item['review_music'],
item['review_time'],
item['review_url'],
item['review_url']))
"""insert into music_review_douban(review_title,review_content,review_author,review_music,review_time,
review_url)
value (%s,%s,%s,%s,%s,%s)""",
(item['review_title'],
item['review_content'],
item['review_author'],
item['review_music'],
item['review_time'],
item['review_url']))
网友评论