python資料庫模組

來源:互聯網
上載者:User

標籤:

python開發中,使用資料庫是已經再普通不過的事情了。現在的NoSQL也很流行,但暫不涉及。本文主要記錄python中串連常用關係型資料庫的問題。今天在配置sql server串連時遇到了不同的模組,從而整理整個資料庫模組的操作。(環境:windows,python2.7) 首先概括下面會談到的常用關係型資料庫:SQLite、MySQL、PoesgreSQL、Oracle、SQL Server、 excel
  • SQLite:sqlite3。(python2.5+內建)
  • MySQL: MySQLdb
  • PoesgreSQL:postgresql_psycopg2()
  • Oracle:  cx_Oracle
  • SQL Server: pymssql、pyodbc、adodbapi
  • excel:  pyExcelertor
  1.1 SQL Server: pymssql   安裝pymssql,cmd下執行pip install pymssql,然後 在互動頁面或者IDLE中 import pymmsql  出現找不到指定的模組。pip安裝最新的pymssql2.1.2,但是  於是我們需要自己安裝 FreeTDS和openssl。
  • 安裝FreeTDS
Python 版本 VS
2.7 vs2008
3.3 或者 3.4 vs2010
3.5 vs2015
下載解壓出來後找到裡面一個包含 DLL 檔案的檔案夾,將這個目錄添加到系統或使用者的 PATH 變數裡面。
  • 安裝openssl

接著下載先行編譯好的 openssl,根據自己的 Python 版本選擇對應 vs 版本的 7z 檔案(見上表)。解壓出來後同樣找到裡麵包含 DLL 檔案的檔案夾(64 位元系統可以選擇檔案夾名帶 64 的),然後將這個目錄添加到PATH 變數裡面。

我的兩個目錄是: C:\freetds-v0.95.81-win-x86_64-vs2008\lib; C:\openssl-1.0.1q-vs2008\bin64; 使用:import pymssqlconn=pymssql.connect(server="127.0.0.1",port="1433",user="sa",password="123",database="myblog",charset="UTF-8") cursor = conn.cursor()conn.close()1.2  SQL Server: pyodbc   安裝pyodbc,cmd下 pip  install  pyodbc,使用如下:  import pyodbc

串連資料庫

 

1)直接連接資料庫和建立一個遊標(cursor)

?
12 cnxn = pyodbc.connect(‘DRIVER={SQL Server};SERVER=localhost;DATABASE=testdb;UID=me;PWD=pass‘)cursor = cnxn.cursor()

 

 

2)使用DSN串連。通常DSN串連並不要求輸入密碼,還是需要提供一個PSW的關鍵字。

?
12 cnxn = pyodbc.connect(‘DSN=test;PWD=password‘)cursor = cnxn.cursor()
 1.3 SQL Server: adodbapi         安裝adodbapi ,cmd下 pip install adodbapi 或者安裝pywin32(Since pywin32 release 211, adodbapi is included )  使用:import adodbapi conn={‘server‘:‘192.168.29.86\\eclexpress‘,‘password‘:‘xxxx‘,‘db‘:‘pscitemp‘}    constr = r"Provider=SQLOLEDB.1; Initial Catalog=%s; Data Source=%s; user ID=%s; Password=%s; " \
         % (conn[‘db‘], conn[‘server‘], ‘sa‘, conn[‘password‘])    conn=adodbapi.connect(constr) 2、SQLite:sqlite3的使用import sqlite3#開啟db檔案,獲得串連
conn = sqlite3.connect(‘資料檔案名‘)
#獲得遊標
c = conn.cursor()
#執行SQL
c.execute(‘‘‘SQL 片段‘‘‘)
#如果有對資料的修改操作,那就需要commit一下
conn.commit()
#關閉遊標
c.close()
#關閉串連conn.close() 3、MySQL: MySQLdb的串連        import MySQLdbconn=MySQLdb.connect(host="localhost",user="root",passwd="",db="test",charset="utf8") cursor = conn.cursor()      4、PoesgreSQL:postgresql_psycopg2 import psycopg2  conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432") print "Opened database successfully"  cur = conn.cursor()  5、Oracle:  cx_Oracle import cx_Oracle                                          #引用模組cx_Oracle
conn=cx_Oracle.connect(‘load/[email protected]/ora11g‘)    #串連資料庫
c=conn.cursor()                                           #擷取cursor 6、excel:  pyExcelertor from pyExcelerator import *sheets=pyExcelerator.parse_xls(‘xxx.xls‘) #讀取檔案內容

python資料庫模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.