自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python連接數(shù)據(jù)庫兩種常用方法介紹

開發(fā) 后端
我們在這里為大家介紹的兩種比較常見的Python連接數(shù)據(jù)庫主要是通過使用psycopg2來連接以及使用pymssql來連接等方法。

Python編程語言在實(shí)際使用中往往會遇到與數(shù)據(jù)庫有關(guān)系的操作。那么今天,我們就會為大家系統(tǒng)的介紹兩種比較常見的Python連接數(shù)據(jù)庫的基本方法,希望朋友們可以從中學(xué)到更多的東西。

Python連接數(shù)據(jù)庫之postgresql:

使用psycopg2來連接

示例代碼:

  1. import psycopg2  
  2. conn = psycopg2.connect("dbname='dbname' user='username' 
    host='localhost' password='password'")  
  3. cur = conn.cursor()  
  4. cur.execute("select * from dbtable")  
  5. for row in cur:  
  6. print row[0]  
  7. conn.close() 

Python連接數(shù)據(jù)庫之ms sql server:

使用pymssql來連接

示例代碼:

  1. import psmssql  
  2. conn = psmssql.connect(host='yourhost'user='loginname'
    password='password'database='dbname'charset='utf8')  
  3. cur = conn.cursor()  
  4. cur.execute('select * from dbtable')  
  5. for row in cur:  
  6. print row[0]  
  7. conn.close() 

以上就是我們?yōu)榇蠹医榻B的Python連接數(shù)據(jù)庫的相關(guān)操作方法。

【編輯推薦】

  1. Python流程控制關(guān)鍵字基本內(nèi)容總結(jié)
  2. Python String類型基本應(yīng)用情況分析
  3. Python異?;咎幚頇C(jī)制詳解
  4. Python文件路徑具體操作方法經(jīng)典講解
  5. Python優(yōu)化圖片實(shí)際操作技巧淺析
責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2009-11-12 11:29:42

ADO.NET連接數(shù)據(jù)

2009-08-25 11:35:27

C#連接數(shù)據(jù)庫

2010-04-06 10:52:06

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

2009-11-11 16:36:19

路由協(xié)議介紹

2010-04-28 16:23:18

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

2010-10-26 14:06:43

oracle連接遠(yuǎn)程數(shù)

2019-03-27 14:41:41

Python數(shù)據(jù)庫Windows

2011-04-06 11:05:21

SQL Server數(shù)交換數(shù)據(jù)

2010-08-04 10:32:56

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

2011-04-25 09:53:31

C++mysql

2009-04-20 14:29:41

Oracle連接創(chuàng)建連接

2011-05-24 09:18:59

C++連接mysql數(shù)據(jù)庫

2010-08-13 15:12:05

CISCO密碼

2010-08-20 13:51:44

路由器密碼

2021-11-02 18:27:48

數(shù)據(jù)挖掘項(xiàng)目

2011-06-08 11:15:21

web.configASP.NET

2009-07-06 23:23:15

Sybase數(shù)據(jù)庫修復(fù)

2010-11-29 09:56:00

sybase數(shù)據(jù)庫備份

2009-12-07 13:42:24

WCF框架

2010-03-26 09:06:06

Python連接數(shù)據(jù)庫
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號