Python連接不同數(shù)據(jù)庫的操作分別是什么
Admin 2022-08-15 群英技術(shù)資訊 778 次瀏覽
今天小編跟大家講解下有關(guān)“Python連接不同數(shù)據(jù)庫的操作分別是什么”的內(nèi)容 ,相信小伙伴們對這個話題應(yīng)該有所關(guān)注吧,小編也收集到了相關(guān)資料,希望小伙伴們看了有所幫助。1、mongodb連接數(shù)據(jù)庫
import pymongo #連接數(shù)據(jù)庫 client = pymongo.MongoClient('localhost') #連接指定數(shù)據(jù)庫 db = client['shop'] #指定goods 集合 result = db['goods'].find() ww = db['goods'].insert_one({'a':'qw'}) aa = db['test'].insert_many([{'a':'qw'},{'a':'qw'}]) # print(ww) #<pymongo.cursor.Cursor object at 0x0000000002F3A7F0> print(result) #<pymongo.cursor.Cursor object at 0x0000000002F3A7F0> for i in result: #需要遍歷才能拿到每條document的信息 print(i)
2、連接redis數(shù)據(jù)庫
import redis #開啟一個鏈接 rd = redis.Redis() #建立連接池,為多個用戶開啟好鏈接,用戶直接拿,用完放回 red_pool = redis.ConnectionPool(host='localhost',port=6379,db=4) rds = redis.Redis(connection_pool=red_pool) r = rd.set('test1',1) print(r) #插入成功返回 1 就是True g = rd.get('test1') #返回b'1 二進(jìn)制,,需要解碼 print(g.decode('utf-8'))
3、python 和 mysql連接
import pymysql #與mysql數(shù)據(jù)庫建立鏈接,加cursorclass=pymysql.cursors.DictCursor每條數(shù)據(jù)返回的是字典類型,不加是元組類型 db = pymysql.connect(host='127.0.0.1',user='root',password='123456',database='hei',port=3306,cursorclass=pymysql.cursors.DictCursor) #創(chuàng)建游標(biāo)對象 cursor = db.cursor() re = 'select * from user' #插入數(shù)據(jù) # ins = 'insert into user (id,name) values(null,"q")' #cursor.execute(ins) # db.commit() #循環(huán)插入數(shù)據(jù) for i in range(97,123): ins = 'insert into user (id,name) values(null,%s)' excu = cursor.execute(ins,[str(chr(i))]) db.commit() #執(zhí)行命令 # cursor.execute(ins) r = cursor.execute(re) print(r) # f = cursor.fetchall() #[{'id': 6, 'name': 'c'},{'id': 7, 'name': 'r'}] 多條數(shù)據(jù) 列表類型 # ((6, 'c'), (9, 'd'), (11, 'q'), (8, 'v'), (4, 'w'), (1, '星期八')) d = cursor.fetchone() #{'id': 6, 'name': 'c'}一臺數(shù)據(jù)字典類型 (6, 'c') # print(f) print(d) #執(zhí)行插入 更新修改 刪除 命令需要提交 db.close()
轉(zhuǎn)載:https://blog.csdn.net/panjunxiao/java/article/details/102616251
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:[email protected]進(jìn)行舉報,并提供相關(guān)證據(jù),查實(shí)之后,將立刻刪除涉嫌侵權(quán)內(nèi)容。
猜你喜歡
這篇文章主要為大家介紹了python基礎(chǔ)中的文件對象,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助<BR>
這篇文章主要介紹了python-pandas創(chuàng)建Series數(shù)據(jù)類型的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)tf.name_scope和tf.variable_scope函數(shù)的使用區(qū)別,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
這篇文章主要為大家介紹了Python簡單實(shí)現(xiàn)gif動圖倒放的示例過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
這篇文章主要介紹了Python函數(shù)中閉包和延遲綁定詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
推薦內(nèi)容
相關(guān)標(biāo)簽
成為群英會員,開啟智能安全云計(jì)算之旅
立即注冊關(guān)注或聯(lián)系群英網(wǎng)絡(luò)
7x24小時售前:400-678-4567
7x24小時售后:0668-2555666
24小時QQ客服
群英微信公眾號
CNNIC域名投訴舉報處理平臺
服務(wù)電話:010-58813000
服務(wù)郵箱:[email protected]
投訴與建議:0668-2555555
Copyright ? QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版權(quán)所有
增值電信經(jīng)營許可證 : B1.B2-20140078 ICP核準(zhǔn)(ICP備案)粵ICP備09006778號 域名注冊商資質(zhì) 粵 D3.1-20240008