久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

pymysql實(shí)現(xiàn)增刪改查的操作指南(python)

瀏覽:4日期:2022-06-19 17:25:50

1.安裝pymysql:pip install pymysql (在命令行窗口中執(zhí)行)

2.卸載pymysql:pip uninstall pymysql (在命令行窗口中執(zhí)行)

數(shù)據(jù)庫的連接

需要注意的是port是不用引號(hào)括起來 charset是utf8不是utf-8

# 獲取數(shù)據(jù)庫連接對象connection = pymysql.connect(host=’localhost’, port=3306, user=’root’, passwd=’2732195202’, db=’book’, charset=’utf8’)# 獲取一個(gè)游標(biāo)driver = connection.cursor()# 執(zhí)行一條sqldriver.execute('select version()')# 獲取執(zhí)行sql的返回值resultData=driver.fetchall()print(resultData)# 關(guān)閉數(shù)據(jù)庫connection.close()創(chuàng)建數(shù)據(jù)庫表

import pymysql#獲取數(shù)據(jù)庫連接對象connection = pymysql.connect(host=’localhost’, port=3306, user=’root’, passwd=’2732195202’, db=’book’, charset=’utf8’)#獲取一個(gè)游標(biāo)driver=connection.cursor()# 如果該數(shù)據(jù)庫存在就刪除driver.execute('drop table if exists t_emp ')# 定義sql語句sql=''' CREATE TABLE `t_emp` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT ’主鍵’, `department` varchar(20) DEFAULT NULL COMMENT ’部門’, `salary` decimal(10,2) DEFAULT NULL COMMENT ’工資’, `age` int(11) DEFAULT NULL COMMENT ’年齡’, `sex` varchar(4) DEFAULT NULL COMMENT ’性別’, PRIMARY KEY (`id`))ENGINE=InnoDB DEFAULT CHARSET=utf8; '''# 執(zhí)行sqldriver.execute(sql)# 關(guān)閉數(shù)據(jù)連接connection.close()向數(shù)據(jù)庫中添加數(shù)據(jù)

1.需要注意的是規(guī)范sql,該寫的字段都寫上,不使用默認(rèn)對應(yīng)

2.提交事務(wù)的對象是數(shù)據(jù)庫連接對象,而不是游標(biāo)對象

3.pycharm連接mysql數(shù)據(jù)時(shí),如果連接驅(qū)動(dòng)是高版本,需要加上時(shí)區(qū),jdbc:mysql://localhost/book?serverTimezone=GMT%2B8

4.如果主鍵是自動(dòng)遞增,則不能手動(dòng)指定值,不能寫該字段,讓其自增長

# 獲取數(shù)據(jù)庫連接對象connection=pymysql.connect(host=’localhost’,port=3306,user=’root’,passwd=’2732195202’,db=’book’,charset=’utf8’)# 獲取一個(gè)游標(biāo)driver=connection.cursor()# 定義sql語句sql=''' insert into t_emp(name,department,salary,age,sex)values('tom','開發(fā)部',8000,25,'男'), ('tom','開發(fā)部',8000,25,'男') '''# 嘗試捕捉錯(cuò)誤try: # 執(zhí)行SQL,并返回收影響行數(shù) result=driver.execute(sql) # 提交事務(wù) connection.commit() print('sql(insert)->error')except: # 如果發(fā)生錯(cuò)誤 則回滾事務(wù) print('sql(insert)->error') driver.rollback()# 關(guān)閉數(shù)據(jù)庫連接connection.close()修改表中的數(shù)據(jù)

注意點(diǎn):在操作數(shù)據(jù)庫之前,需要確認(rèn)是否獲取連接數(shù)據(jù)庫成功,并且選中了數(shù)庫

2.卸載第三方庫:pip uninstall pymysql

#獲取數(shù)據(jù)庫連接對象 autocommit=True:設(shè)置數(shù)據(jù)庫自動(dòng)提交connection=pymysql.connect(host='localhost',port=3306,user=’root’,passwd=’2732195202’,db=’book’,charset=’utf8’,autocommit=True)# 獲取游標(biāo)對象driver=connection.cursor()# 定義sqlsql='update t_emp set salary=%s,name=%s where id=%s;'# 如果sql錯(cuò)誤就執(zhí)行回滾操作,成功就提交try: # 執(zhí)行sql,并且返回影響的行數(shù) result=driver.execute(sql,[6000,'admin',19]) connection.commit() print('sql(update)->success')except: print('sql(update)->error') connection.rollback()# 關(guān)閉數(shù)據(jù)庫連接對象connection.close()查詢數(shù)據(jù)

1.項(xiàng)目中的.py文件不能和python庫中的文件進(jìn)行沖突,否則會(huì)出現(xiàn)異常

# 獲取數(shù)據(jù)庫連接對象connection=pymysql.connect(host=’localhost’,port=3306,user=’root’,passwd=’2732195202’,db=’book’,charset=’utf8’)# 獲取一個(gè)游標(biāo)對象driver=connection.cursor()#定義sqlsql='select id, name, department, salary, age, sex from t_emp where id>%s and sex=%s'# 只能獲取一次,獲取多次的時(shí)候會(huì)獲取到null 如果是多個(gè)參數(shù),需要傳遞一個(gè)元組try: driver.execute(sql,(1,'女')) # 獲取所有的查詢結(jié)果 返回一個(gè)元組 resultAll=driver.fetchall() print('resultAll:', resultAll) # 獲取2條數(shù)據(jù) resultTwo=driver.fetchmany(2) print('resultTwo:', resultTwo) # 獲取一條數(shù)據(jù) resultOne=driver.fetchone() print('resultThree:', resultOne) print('sql(select)->success')except: connection.rollback() print('sql(select)->error')# 關(guān)閉數(shù)據(jù)庫連接connection.close()刪除表中的記錄

import pymysql# 獲取數(shù)據(jù)庫連接對象connection = pymysql.connect(host=’localhost’, port=3306, user=’root’, passwd=’2732195202’, db=’book’, charset=’utf8’)# 獲取一個(gè)游標(biāo)driver = connection.cursor()# 定義sqlsql='delete from t_emp where id=%s'try: # 執(zhí)行一條sql driver.execute(sql, (21)) # 提交事務(wù) connection.commit() print('sql(delete)->success')except Exception as e: # 回滾事務(wù) connection.rollback() print('sql(delete)->error') print(e)#關(guān)閉數(shù)據(jù)庫連接connection.close()事務(wù)操作

pymysql實(shí)現(xiàn)增刪改查的操作指南(python)

提交事務(wù): connection.commit()

回滾事務(wù): connection.rollback()

總結(jié)

到此這篇關(guān)于pymsql實(shí)現(xiàn)增刪改查(python)的文章就介紹到這了,更多相關(guān)pymsql增刪改查內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产婷婷精品av在线 | 亚洲h视频在线观看 | 看片地址| 人人草在线观看视频 | 夜夜爽99久久国产综合精品女不卡 | 亚洲成人av一区二区 | 亚洲成人免费 | 久久国产精彩视频 | 成人18视频在线观看 | 成年人在线看片 | 欧美成人免费在线视频 | 日本五月婷婷 | 老黄网站在线观看 | 欧美视频精品在线 | 中国大陆高清aⅴ毛片 | 国产视频福利在线观看 | 国产成人在线电影 | 久草观看| 亚洲h视频 | 久久视频一区 | 久久久久久久久久影院 | 一级大片免费观看 | 一区三区在线观看 | 亚洲视频免费 | 欧美精品一区二区三区四区 | 中文字幕视频在线观看 | 在线视频 91 | 久久com| 久久人| 亚洲一区二区免费看 | wwwsihu| 中文在线播放 | 日韩欧美一级精品久久 | 成人国产一区 | 亚洲永久免费视频 | 精品国产黄a∨片高清在线 激情网站免费 | a在线观看 | 一区二区三区精品 | 久久精品一 | av伊人网| 亚洲一区二区三区高清 |