小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Python訪問數(shù)據(jù)庫

 首家i55ryzehof 2018-11-27

說明:

在實(shí)際工作中,我們經(jīng)常要使用Python對(duì)數(shù)據(jù)庫進(jìn)行操作(增刪改查)。不同的數(shù)據(jù)庫我們需要下載不同的使用模塊,例如我們需要訪問Oracle數(shù)據(jù)庫和Mysql數(shù)據(jù),你需要下載Oracle和MySQL數(shù)據(jù)庫模塊。

Python 標(biāo)準(zhǔn)數(shù)據(jù)庫接口為 Python DB-API,Python 數(shù)據(jù)庫接口支持非常多的數(shù)據(jù)庫,你可以選擇適合你項(xiàng)目的數(shù)據(jù)庫。詳細(xì)可以訪問官方文檔查看相關(guān)信息。

https://wiki./moin/DatabaseInterfaces

本次介紹主流數(shù)據(jù)庫的連接:Oracle、Sql Server、Mysql

1、Oracle數(shù)據(jù)庫

這里使用的是cx_Oracle模塊來操作Oracle數(shù)據(jù)庫

#Oracle

importcx_Oracle

host ='192.168.1.1'

port ='1521'

dbname ='testdb'

username ='test'

password ='test'

dsn = cx_Oracle.makedsn(host, port, dbname)

connection = cx_Oracle.connect(username, password, dsn)

# 使用cursor()方法獲取操作游標(biāo)

cursor = connection.cursor()

# SQL 查詢語句

sql ='select*from testtable'

# 執(zhí)行SQL語句

cursor.execute(sql)

# 獲取一條記錄

# fetchall可以獲取所有記錄列表

res = cursor.fetchone()

print(res)

# 關(guān)閉數(shù)據(jù)庫連接

connection.close()

2、Sql Server數(shù)據(jù)庫

這里使用的是pyodbc模塊來操作Sql Server數(shù)據(jù)庫

#Sql Server

importpyodbc

host ='192.168.1.1'

username ='sa'

password ='test'

dbname ='testdb'

conn_infomssql ='DRIVER=;DATABASE=%s;

SERVER=%s;UID=%s;PWD=%s'% (dbname, host, username, password)

conn = pyodbc.connect(conn_infomssql)

# 使用cursor()方法獲取操作游標(biāo)

cursor = conn.cursor()

# SQL 查詢語句

sql ='select*from testtable'

# 執(zhí)行SQL語句

cursor.execute(sql)

# 獲取一條記錄

# fetchall可以獲取所有記錄列表

res = cursor.fetchone()

print(res)

# 關(guān)閉數(shù)據(jù)庫連接

conn.close()

3、Mysql數(shù)據(jù)庫

這里使用的是pymysql模塊來操作Mysql數(shù)據(jù)庫

#Mysql

importpymysql

conn = pymysql.connect(host='192.168.1.1',port=3308,user='root',

passwd='root',db='testdb',charset='utf8')

# 使用cursor()方法獲取操作游標(biāo)

cursor = conn.cursor()

# SQL 查詢語句

sql ='select*from testtable'

# 執(zhí)行SQL語句

cursor.execute(sql)

# 獲取一條記錄

# fetchall可以獲取所有記錄列表

res = cursor.fetchone()

print(res)

# 關(guān)閉數(shù)據(jù)庫連接

conn.close()

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請遵守用戶 評(píng)論公約

    類似文章 更多