(13)python串連msql

來源:互聯網
上載者:User

標籤:net   open   abc   href   hone   close   modified   read   target   

python串連mysql兩種方法一、python官網提供的 MySQL-python 軟體

https://pypi.python.org/pypi/MySQL-python/1.2.5      (MySQL-python-1.2.5.win32-py2.7.exe )

http://download.csdn.net/detail/seven_zhao/6607625(MySQL-python-1.2.3.win-amd64-py2.7)

 

安裝時如果報一下錯誤

Python version 2.7 required, which was not found in the registry

用一下方法解決

方法:轉自(http://www.cnblogs.com/min0208/archive/2012/05/24/2515584.html)

建立一個register.py 檔案,把一下代碼貼進去,儲存(G盤)

## script to register Python 2.0 or later for use with win32all# and other extensions that require Python registry settings## written by Joakim Loew for Secret Labs AB / PythonWare## source:# http://www.pythonware.com/products/works/articles/regpy20.htm## modified by Valentine Gogichashvili as described in http://www.mail-archive.com/[email protected]/msg10512.html import sys from _winreg import * # tweak as necessaryversion = sys.version[:3]installpath = sys.prefix regpath = "SOFTWARE\\Python\\Pythoncore\\%s\\" % (version)installkey = "InstallPath"pythonkey = "PythonPath"pythonpath = "%s;%s\\Lib\\;%s\\DLLs\\" % (    installpath, installpath, installpath) def RegisterPy():    try:        reg = OpenKey(HKEY_CURRENT_USER, regpath)    except EnvironmentError as e:        try:            reg = CreateKey(HKEY_CURRENT_USER, regpath)            SetValue(reg, installkey, REG_SZ, installpath)            SetValue(reg, pythonkey, REG_SZ, pythonpath)            CloseKey(reg)        except:            print "*** Unable to register!"            return        print "--- Python", version, "is now registered!"        return    if (QueryValue(reg, installkey) == installpath and        QueryValue(reg, pythonkey) == pythonpath):        CloseKey(reg)        print "=== Python", version, "is already registered!"        return    CloseKey(reg)    print "*** Unable to register!"    print "*** You probably have another Python installation!" if __name__ == "__main__":    RegisterPy()

 

 

安裝成功後,

1、匯入模組

import MySQLdb 

如果沒報錯說明安裝成功

 

2、建立連線物件,傳入參數

conn=MySQLdb.connect(host=‘localhost‘,user=‘root‘,passwd=‘abc‘,db=‘db_meng‘,port=3306)

 

3、建立遊標對象並擷取資料庫連接

cur=conn.cursor()

 

4、執行sql

增刪改和建庫建表的純sql語句

cur.execute()

例如:

cur.execute(‘CREATE TABLE tb_meng7(id INT,name1 VARCHAR(50)‘)

 

5、關閉遊標

cur.close()

 

6、提交事物

conn.commit()

 

7、關閉資料庫連接

conn.close()

 

8、顯示查詢資料

每次只顯示一行,遊標向後移動一位

cur.fetchone()

 

9、查詢結果,遊標控制

定位到查詢的第一條資料

cur.scroll(0,‘absolute‘)

 

10、獲得多條資料,必須要指定資料條數

info = cur.fetchmany(i)

然後用遍曆的方法,得出所有資料

for ii in info:        print ii

 

完整代碼:

 

二、mysql connector python v2.1.6 for python v2.7

mysql 5.7.18.1 裡附帶的軟體(別的版本沒用過,只能拿這個舉例子)

mysql https://dev.mysql.com/downloads/mysql/

安裝時選自訂可以看到此選項

安裝成功後

import mysql.connector

如果沒報錯說明安裝成功

 

(13)python串連msql

相關文章

聯繫我們

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