99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀最全總結(jié),聊聊 Python 數(shù)據(jù)處理全家桶(PgSQL篇)
最全總結(jié),聊聊 Python 數(shù)據(jù)處理全家桶(PgSQL篇)
2021-08-11
收藏

來源:AirPython

作者:星安果

最全總結(jié),聊聊 Python <a href='/map/shujuchuli/' style='color:#000;font-size:inherit;'>數(shù)據(jù)處理</a>全家桶(Pg<a href='/map/sql/' style='color:#000;font-size:inherit;'>SQL</a>篇)

1. 前言

大家好,我是安果!

PgSQL,全稱為 PostgreSQL,是一款免費(fèi)開源的關(guān)系型數(shù)據(jù)庫

相比最流行的 Mysql 數(shù)據(jù)庫,PgSQL 在可靠性、數(shù)據(jù)完整性、擴(kuò)展性方面具有絕對(duì)的優(yōu)勢(shì)

本篇文章將聊聊如何使用 Python 操作 PgSQL 數(shù)據(jù)庫

2. PgSQL 使用

Python 操作 PgSQL,需要先安裝依賴包「 psycopg2 」

# 安裝依賴包
pip3 install psycopg2

接下來,就可以使用 Python 來操作數(shù)據(jù)庫了

2-1 數(shù)據(jù)庫連接及游標(biāo)對(duì)象

使用 psycopg2 中的「 connect() 」方法連接數(shù)據(jù)庫,創(chuàng)建數(shù)據(jù)庫連接對(duì)象及游標(biāo)對(duì)象

import psycopg2
# 獲得連接對(duì)象
# database:數(shù)據(jù)庫名稱
# user:用戶名
# password:密碼
# host:數(shù)據(jù)庫ip地址
# port:端口號(hào),默認(rèn)為5432
conn = psycopg2.connect(database="db_name", user="postgres", password="pwd", host="127.0.0.1", port="5432")
# 獲取游標(biāo)對(duì)象
cursor = conn.cursor()

獲取游標(biāo)對(duì)象后,就可以執(zhí)行 SQL,進(jìn)而操作數(shù)據(jù)庫了

2-2 插入數(shù)據(jù)

首先,編寫插入數(shù)據(jù)的 SQL 語句及參數(shù)( 可選 )

# 構(gòu)建SQL語句
# 方式一:直帶參數(shù)
sql = "INSERT INTO student (name,age)
VALUES (%s, '%s')" %
('xag',23)
# 方式二:參數(shù)分離
sql = """INSERT INTO student (name,age) VALUES (%s, %s)"""
# 參數(shù)
params = ('xag',23)

然后,使用游標(biāo)對(duì)象執(zhí)行 SQL

# 執(zhí)行sql
# 注意:params可選,根據(jù)上面的參數(shù)方式來選擇設(shè)置
cursor.execute(sql,[params])

接著,使用連接對(duì)象提交事務(wù)

# 事務(wù)提交
conn.commit()

最后,釋放游標(biāo)對(duì)象及數(shù)據(jù)庫連接對(duì)象

# 釋放游標(biāo)對(duì)象及數(shù)據(jù)庫連接對(duì)象
cursor.close()
conn.close()

2-3 查詢數(shù)據(jù)

游標(biāo)對(duì)象的 fetchone()、fetchmany(size)、fetchall() 這 3個(gè)函數(shù)即可以實(shí)現(xiàn)單條數(shù)據(jù)查詢、多條數(shù)據(jù)查詢、全部數(shù)據(jù)查詢

# 獲取一條記錄
one_data = cursor.fetchone()
print(one_data)
# 獲取2條記錄
many_data = cursor.fetchmany(2)
print(many_data)
# 獲取全部數(shù)據(jù)
all_data = cursor.fetchall()
print(all_data)

需要注意的是,條件查詢與上面的插入操作類似,條件語句可以將參數(shù)分離出來

# 條件查詢 SQL語句
sql = """SELECT * FROM student where id = %s;"""
# 對(duì)應(yīng)參數(shù),參數(shù)結(jié)尾以逗號(hào)結(jié)尾
params = (1,)
# 執(zhí)行SQL
cursor.execute(sql, params)
# 獲取所有數(shù)據(jù)
datas = cursor.fetchall()
print(datas)

2-4 更新數(shù)據(jù)

更新操作和上面操作一樣,唯一不同的是,執(zhí)行完 SQL 后,需要使用連接對(duì)象提交事務(wù),才能將數(shù)據(jù)真實(shí)更新到數(shù)據(jù)庫中

def update_one(conn, cursor):
"""更新操作"""
# 更新語句
sql = """update student set name = %s where id = %s """
params = ('AirPython', 1,)
# 執(zhí)行語句
cursor.execute(sql, params)
# 事務(wù)提交
conn.commit()
# 關(guān)閉數(shù)據(jù)庫連接
cursor.close()
conn.close()

2-5 刪除數(shù)據(jù)

刪除數(shù)據(jù)同更新數(shù)據(jù)操作類似

def delete_one(conn, cursor):
"""刪除操作"""
# 語句及參數(shù)
sql = """delete from student where id = %s """
params = (1,)
# 執(zhí)行語句
cursor.execute(sql, params)
# 事物提交
conn.commit()
# 關(guān)閉數(shù)據(jù)庫連接
cursor.close()
conn.close()

3. 最后

通過上面操作,可以發(fā)現(xiàn) Python 操作 PgSQl 與 Mysql 類似,但是在原生 SQL 編寫上兩者還是有很多差異性

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }