資源簡介
首先在mysql中創(chuàng)建chat數(shù)據(jù)庫,
然后確保本地環(huán)境安裝了 pymysql 【管理員模式>>命令行工具>>pip3 install PyMySQL】
最后確保連接串與你本地一致,運(yùn)行即可看到如下圖:
代碼片段和文件信息
import?pymysql
#??創(chuàng)建連接
conn?=?pymysql.connect(
????host?=?‘localhost‘
????port?=?3306
????user?=?‘root‘
????passwd=?‘root‘
????db???=?‘chat‘
????charset?=?‘utf8‘
)
#??實(shí)例化游標(biāo)
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;‘
}
#??執(zhí)行SQL語句
cur.execute(sql_dict[‘show‘])
#??獲取所有數(shù)據(jù)
datas?=?cur.fetchall()
for?db_info?in?datas:
?????print(db_info)
#??創(chuàng)建表格
tr
評論
共有 條評論