18 12 07 MySQL ???python ?????????
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了18 12 07 MySQL ???python ?????????相关的知识,希望对你有一定的参考价值。
?????????__name__ exec value ?????? ?????? ?????? mysq ?????? ??????
---??????????????????---
???python ??? ??????SQL???????????????
from pymysql import * # ????????????????????????MySQL ???????????????????????? def main(): # ??????Connection?????? conn = connect(host=???localhost???,port=3306,user=???root???,password=???root???,database=???jing_dong???,charset=???utf8???) # ??????Cursor?????? cs1 = conn.cursor() # ??????select????????????????????????????????????????????????????????? execute????????? ?????????SQL?????? count = cs1.execute(???select id,name from goods where id>=4???) # ???????????????????????? print("?????????%d?????????:" % count) for i in range(count): # ????????????????????? result = cs1.fetchone() # ????????????????????? print(result) # ????????????????????? # ??????Cursor?????? cs1.close() conn.close() if __name__ == ???__main__???: main()
fetch ?????????????????? ??? fecthone fecthmany fecthall
python ??? SQL ??????????????????
from pymysql import * def main(): # ??????Connection?????? conn = connect(host=???localhost???,port=3306,database=???jing_dong???,user=???root???,password=???root???,charset=???utf8???) # ??????Cursor?????? cs1 = conn.cursor() # ??????insert????????????????????????????????????????????????????????? # ?????? count = cs1.execute(???insert into goods_cates(name) values("??????")???) #???????????????????????? print(count) count = cs1.execute(???insert into goods_cates(name) values("??????")???) print(count) # # ?????? # count = cs1.execute(???update goods_cates set name="????????????" where name="??????"???) # # ?????? # count = cs1.execute(???delete from goods_cates where id=6???) # ???????????????????????????????????????????????????????????????execute??????????????????????????? conn.commit() # ??????Cursor?????? cs1.close() # ??????Connection?????? conn.close() if __name__ == ???__main__???: main()
??????sql?????????????????????
- sql???????????????????????????????????????sql??????
- ????????????????????????python????????????????????????????????????%s??????
-
from pymysql import * def main(): find_name = input("????????????????????????") # ??????Connection?????? conn = connect(host=???localhost???,port=3306,user=???root???,password=???mysql???,database=???jing_dong???,charset=???utf8???) # ??????Cursor?????? cs1 = conn.cursor() # # ?????????????????? # # ?????? " or 1=1 or " (?????????????????????) # sql = ???select * from goods where name="%s"??? % find_name # print("""sql===>%s<====""" % sql) # # ??????select????????????????????????????????????????????????????????? # count = cs1.execute(sql) # ??????????????? # ?????????????????? params = [find_name] # ??????select????????????????????????????????????????????????????????? count = cs1.execute(???select * from goods where name=%s???, params) # ????????? # ??????????????????????????????????????????????????? # ??????params = [??????1, ??????2....]?????????sql??????????????????%s?????? # ???????????????????????? print(count) # ????????????????????? # result = cs1.fetchone() result = cs1.fetchall() # ????????????????????? print(result) # ??????Cursor?????? cs1.close() # ??????Connection?????? conn.close() if __name__ == ???__main__???: main()
??????
以上是关于18 12 07 MySQL ???python ?????????的主要内容,如果未能解决你的问题,请参考以下文章