資源簡介
首先在mysql中創建chat數據庫,
然后確保本地環境安裝了 pymysql 【管理員模式>>命令行工具>>pip3 install PyMySQL】
最后確保連接串與你本地一致,運行即可看到如下圖:
代碼片段和文件信息
import?pymysql
#??創建連接
conn?=?pymysql.connect(
????host?=?‘localhost‘
????port?=?3306
????user?=?‘root‘
????passwd=?‘root‘
????db???=?‘chat‘
????charset?=?‘utf8‘
)
#??實例化游標
cur?=?conn.cursor()
sql_dict?=?{
????‘show‘:‘show?databases‘
????‘creat‘:‘create?table?stu?(id?INT?PRIMARY?KEY?name?CHAR?(10)addr?CHAR?(15));‘
????‘insert‘:“insert?into?stu(idnameaddr)?VALUE?(1‘XXX‘‘XXXXX‘)(2‘XXX‘‘XXXXX‘);“
????‘select‘:‘select?*?from?stu;‘
????‘update‘:“update?stu?set?addr=‘??‘?where?id=2;“
????‘delete‘:“delete?from?stu?where?id=1;“
????‘drop‘:‘drop?table?stu;‘
}
#??執行SQL語句
cur.execute(sql_dict[‘show‘])
#??獲取所有數據
datas?=?cur.fetchall()
for?db_info?in?datas:
?????print(db_info)
#??創建表格
tr
- 上一篇:python 求平均數入門級
- 下一篇:python編寫windows桌面程序
評論
共有 條評論