Python操作MySQL數(shù)據(jù)庫(kù)的簡(jiǎn)單步驟分享
現(xiàn)在Python越來(lái)越被大眾所使用,特別是進(jìn)入AI人工智能時(shí)代,對(duì)編程要求更加高效根據(jù)快捷,所以Python也經(jīng)常成為人工智和大數(shù)據(jù)編程的重要語(yǔ)音。既然是編程語(yǔ)言就多多少少會(huì)需求對(duì)數(shù)據(jù)進(jìn)行操作,這一篇我們帶大家使用python對(duì)mysql進(jìn)行的操作。
別的不說(shuō),直接上代碼
MySQL 建表建表的時(shí)候,遇到一些坑,沒(méi)有解決,如修改 MySQL 的默認(rèn)引擎,default-storage-engine=InnoDB;執(zhí)行報(bào)錯(cuò) 。。。無(wú)奈
use mybatistable;drop table Test;-- INNODB 支持事務(wù) -- Mysql 默認(rèn)的引擎是 MyISAM ,不支持事務(wù)操作-- 在創(chuàng)建 mysql 表時(shí),最好指定表使用的引擎 -- 或者直接修改Mysql 默認(rèn)的數(shù)據(jù)庫(kù)引擎為 InnoDB-- default-storage-engine=InnoDB; 執(zhí)行報(bào)錯(cuò) 。。。無(wú)奈create table Test( id int(10) not null auto_increment, name varchar(20) not null, password varchar(30) not null, constraint pk_id primary key(id), constraint uk_name unique(name))engine=InnoDB charset=utf8;-- 查看表的引擎show create table Test;-- 更新表的引擎 ,執(zhí)行報(bào)錯(cuò)-- alter table Test type = InnoDB; insert into Test values(default,’小紅’,123);insert into Test values(default,’小李’,123);insert into Test values(default,’小趙’,123);insert into Test values(default,’小軍’,123);insert into Test values(default,’小方’,123);select * from Test;python 操作 MySQL
import pymysql’’’ 連接 mysql 數(shù)據(jù)庫(kù)的步驟 fetchall 接受全部的返回結(jié)果行 PS:只有 innodb 類(lèi)型的表才可以設(shè)置 autocommit;’’’def connectMySql(): host = ’127.0.0.1’ username = ’root’ password = ’root’ # dbName = ’MyBatistable’ # 獲得數(shù)據(jù)庫(kù)連接對(duì)象 conn = pymysql.connect(host,username,password) #關(guān)閉數(shù)據(jù)庫(kù)的自動(dòng)提交事務(wù) conn.autocommit(False) # 選擇要操作的數(shù)據(jù)庫(kù) conn.select_db(’MyBatistable’) #覆蓋之前操作的數(shù)據(jù)庫(kù)名 # 獲得游標(biāo) cursor = conn.cursor() #定義 SQL 語(yǔ)句 sql = ’select * from Test’ sql1 = ’insert into test values(default,'小鍋','120')’ sql2 = ’update test set name='小庫(kù)2' where id = 2’ sql3 = ’delete from test where id = 2’ #執(zhí)行 SQL 語(yǔ)句 # row = cursor._query(sql) #執(zhí)行 execute 方法,返回影響的行數(shù) row = cursor.execute(sql1) print(’row type:’,type(row)) print(’受影響的行數(shù)為:’,row) if row > 0:conn.commit() # 提交事務(wù)print(’SUCCESS’) else:conn.rollback() # 回滾事務(wù)print(’Failure’) #使用DQL ,返回結(jié)果集,以元組的形式 nums = cursor.fetchall() print(’nums Type:’,type(nums)) #處理結(jié)果集 if nums != () :for num in nums: print(’--’,num)if __name__ == ’__main__’: connectMySql()總結(jié)
Python 操作 MySQL 時(shí),由于MySQL 默認(rèn)使用時(shí) MyISAM 引擎,不支持事務(wù)操作。而在Python操作 Mysql 中關(guān)閉自動(dòng)提交事務(wù),發(fā)現(xiàn)并沒(méi)有卵用,然后到網(wǎng)上百度說(shuō),Mysql 中 InnoDB 支持事務(wù),然后我查找一哈我自己表的引擎發(fā)現(xiàn)是 MyISAM ,欲哭無(wú)淚啊。然后我就重新開(kāi)始建表,測(cè)試。
到此這篇關(guān)于Python操作MySQL數(shù)據(jù)庫(kù)的簡(jiǎn)單步驟的文章就介紹到這了,更多相關(guān)Python操作MySQL數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 關(guān)于WPF WriteableBitmap類(lèi)直接操作像素點(diǎn)的問(wèn)題2. JavaScript前端中的偽類(lèi)元素before和after使用詳解3. PHP JSAPI調(diào)支付API實(shí)現(xiàn)微信支付功能詳解4. asp取整數(shù)mod 有小數(shù)的就自動(dòng)加15. 源碼分析MinimalApi是如何在Swagger中展示6. ThinkPHP5實(shí)現(xiàn)JWT Token認(rèn)證的過(guò)程(親測(cè)可用)7. ASP基礎(chǔ)入門(mén)第一篇(ASP技術(shù)簡(jiǎn)介)8. 熊海CMS代碼審計(jì)漏洞分析9. 表單中Readonly和Disabled的區(qū)別詳解10. PHP laravel實(shí)現(xiàn)基本路由配置詳解
