python串連MySQL資料庫方法詳解

來源:互聯網
上載者:User

python串連mysql教程資料庫教程方法詳解


host,串連的資料庫伺服器主機名稱,預設為本地主機(localhost)。  
        user,串連資料庫的使用者名稱,預設為目前使用者。
        passwd,串連密碼,沒有預設值。
        db,串連的資料庫名,沒有預設值。
        conv,將文字映射到python類型的字典。預設為

mysqldb.converters.conversions
        cursorclass,cursor()使用的種類,預設值為mysqldb.cursors.cursor


        compress,啟用協議壓縮功能。
        named_pipe,在windows中,與一個具名管道相串連。
        init_command,一旦串連建立,就為資料庫伺服器指定一條語句來運行。

            
        read_default_file,使用指定的mysql設定檔。
        read_default_group,讀取的預設組。
        unix_socket,在unix中,串連使用的通訊端,預設使用tcp。
        port,指定資料庫伺服器的串連連接埠,預設是3306。

import mysqldb 

conn = mysqldb.connect(host="localhost",  

                       user="root",  

                       passwd="root",  

                       db="testdb") 

cursor = conn.cursor() 

cursor.execute("select * from users") 

res = cursor.fetchall() 

print res 

cursor.close() 

conn.close()

查詢資料執行個體二
cn = connection('localhost', 'root', '')
cn.select_db('yphp教程')
cur = cn.cursor()
cur.execute('set names utf8')
cur.execute('select id,url,content from yphp_spider_url where checked=0

limit 0,1')
rs = cur.fetchone()
print rs

mysql connector/python 是一個用 python 語言實現的 mysql 用戶端伺服器的連

接協議。該軟體包無需安裝任何 mysql 的軟體。使用的是 python 3.1

  該版本支援回 python 2.4 版本,可處理大的資料包,支援串連壓縮協議和

ssl。

  mysql connector/python 是 mysql 官方提供的 python 串連 mysql 資料庫

的驅動程式。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.