[Python_6] Python 配置 MySQL 访问
生活随笔
收集整理的這篇文章主要介紹了
[Python_6] Python 配置 MySQL 访问
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?0. 說明
Python 訪問 MySQL 數據庫,需要安裝?MySQL?的 Python 插件。
?
?
1. 安裝 MySQL 插件
pip install PyMySQL?
2. 編寫代碼
# -*-coding:utf-8-*- import pymysql"""Python 訪問 MySQL 數據庫 """"""創建表的命令 create table t1( id int, name varchar(20), age int ); """# 連接到數據庫 ,注意使用127.0.0.1 conn = pymysql.Connect("127.0.0.1", "root", "123456", "python") # 得到游標 cur = conn.cursor()# 執行語句 # cur.execute("select version()") # 提取第一條記錄 # result = cur.fetchone() # print(result)# 插入記錄 # cur.execute("insert into t1(id ,name ,age) values(2 ,'amy' , 23)")# 查詢 t1表 所有記錄 print("=================") cur.execute("select * from t1") results_list = cur.fetchall() for r in results_list:id = r[0]name = r[1]age = r[2]print("%d/%s/%d\r\n" % (id, name, age), )# 刪除記錄 # cur.execute("delete from t1 where id >= 1000") conn.commit() cur.close()?
?
?
轉載于:https://www.cnblogs.com/share23/p/9811550.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的[Python_6] Python 配置 MySQL 访问的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【收藏】一篇快速帮企业转型区块链的锦囊
- 下一篇: 10.19文件管理课程笔记