-
当我们和数据库交互是,查询操作是必不可少的,所以我来分享一下如何用python来对mysql数据库进行操作
基本的查询操作:select*from quote;查询表quote中所有的数据
-
select author from quote;查询表quote中包含author的数据
-
1. select和where连用 可以筛选出表中所需要的数据select author from quote where tags ='humor'
意义为筛选出表quote中tags标签包含humor的author
共6条数据如下:
2. 在PyCharm中连接数据库
def get_conn(): conn = pymysql.connect(host='localhost',port=3306,user='root',passwd='123456',db='zpark09') 称 return conn
python之插入操作
def insert(sql):
conn = get_conn()
cur = conn.cursor()
result = cur.execute(sql)
print(result)
conn.commit()
cur.close()
conn.close()
if __name__ == '__main__':
sql = "INSERT INTO QUOTE (TEXT,author,tags)VALUES('ds','fvfvfv','love');"
insert(sql)
在这段时间中我们学习了很多关于sql的操作,在python中都可以实现。



