Python連接數(shù)據(jù)庫(kù)的實(shí)際操作方案的介紹
Python是計(jì)算機(jī)常用的計(jì)算機(jī)語(yǔ)言,在實(shí)際的操作中我們會(huì)涉及到Python連接數(shù)據(jù)庫(kù)的相關(guān)實(shí)際操作,假如你對(duì)Python連接數(shù)據(jù)庫(kù)的實(shí)際操作方案感興趣或是有疑問,你都可以瀏覽下面的文章。
一. Python和mysql數(shù)據(jù)庫(kù)連接
Python 要連接 MySQL 可以使用 MySQL_python模塊首先確定是否安裝,在指令模式輸入 python,然後便可以開始檢查:
Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
- [GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
- Type "help", "copyright", "credits" or "license"
for more information.- >>> import MySQLdb
- Traceback (most recent call last):
- File "", line 1, in
- ImportError: No module named MySQLdb
- >>> exit()
如果見以上面的
- "ImportError: No module named MySQLdb"
一句,便表示系統(tǒng)沒有安裝,注意:在shell中,輸出是 區(qū)分大小寫的也可以通過輸入下面這些命令來測(cè)試你的數(shù)據(jù)庫(kù)配置:
- >>> from django.db import connection >>>
cursor = connection.cursor()
如果沒有顯示什么錯(cuò)誤信息,那么你的數(shù)據(jù)庫(kù)配置是正確的。 否則,你就得查看錯(cuò)誤信息來糾正錯(cuò)誤。上面的相關(guān)代碼是對(duì)Python連接數(shù)據(jù)庫(kù)中Python和mysql數(shù)據(jù)庫(kù)連接的前部分代碼的示例。
安裝mysql_python模塊
到 MySQLdb 官方網(wǎng)站 下載并安裝
MySQLdb版本:
- (win)MySQL-python-1.2.2.win32-py2.6.exe
直接運(yùn)行安裝即可按如下步驟安裝
- $ tar zxvf MySQL-python-1.2.2.tar.gz
- $ cd MySQL-python-1.2.2
- $ python setup.py build
- $ python setup.py install
附件:
- libguide40.dll.zip(77.3 KB)
- libmmd.dll.zip(169 KB)
- libmySQL.dll.zip(861 KB)
以上就是對(duì)python連接數(shù)據(jù)庫(kù)中 python和mysql數(shù)據(jù)庫(kù)連接相關(guān)的內(nèi)容的介紹,望你會(huì)有所收獲。
【編輯推薦】