如果尚未安裝Python,那麼你可以到Python官網進行下載:
For the MD5 checksums and OpenPGP signatures, look at the detailed Python 3.3.3 page:
- Python 3.3.3 Windows x86 MSI Installer (Windows binary -- does not include source)
- Python 3.3.3 Windows X86-64 MSI Installer (Windows AMD64 / Intel 64 / X86-64 binary [1] -- does not include source)
- Python 3.3.3 Mac OS X 64-bit/32-bit x86-64/i386 Installer (for Mac OS X 10.6 and later [2])
- Python 3.3.3 Mac OS X 32-bit i386/PPC Installer (for Mac OS X 10.5 and later [2])
- Python 3.3.3 compressed source tarball (for Linux, Unix or Mac OS X)
- Python 3.3.3 xzipped source tarball (for Linux, Unix or Mac OS X, better compression)
For the MD5 checksums and OpenPGP signatures, look at the detailed Python 2.7.6 page:
- Python 2.7.6 Windows Installer (Windows binary -- does not include source)
- Python 2.7.6 Windows X86-64 Installer (Windows AMD64 / Intel 64 / X86-64 binary [1] -- does not include source)
- Python 2.7.6 Mac OS X 64-bit/32-bit x86-64/i386 Installer (for Mac OS X 10.6 and later [2])
- Python 2.7.6 Mac OS X 32-bit i386/PPC Installer (for Mac OS X 10.3 and later [2])
- Python 2.7.6 compressed source tarball (for Linux, Unix or Mac OS X)
- Python 2.7.6 xzipped source tarball (for Linux, Unix or Mac OS X, better compression)
或者,你嫌慢,可以找幾個離得近的:
Python for Windows
安裝,很簡單,一步一步NEXT就好了,默認裝到C:\python下,我覺得這個路徑就很好。
2、安裝Python-mysql庫
我試了下,我機器上使用easy_install安裝MySQLdb出錯。
適才發現這是個名稱錯誤的問題,國外友人已經回答了,有如下方案:
windows解決方案:easy_install MySQL-python 或者 pip install MySQL-python不過,我用的最笨的方法,到http://www.cr173.com/soft/22957.html這裏下載了MySQL-python.rar安裝包進行安裝。
Linux解決方案:apt-get install python-dev 或者 yum install python-devel
3、Python-mysql主要操作
主要操作,可以參考如下代碼(轉自Sephiroth):
-
#-*- encoding: gb2312 -*-
-
import os, sys, string
-
import MySQLdb
-
-
# 連接數據庫
-
try:
-
conn = MySQLdb.connect(host='localhost',user='root',passwd='xxxx',db='test1')
-
except Exception, e:
-
print e
-
sys.exit()
-
-
# 獲取cursor對象來進行操作
-
-
cursor = conn.cursor()
-
# 創建表
-
sql = "create table if not exists test1(name varchar(128) primary key, age int(4))"
-
cursor.execute(sql)
-
# 插入數據
-
sql = "insert into test1(name, age) values ('%s', %d)" % ("zhaowei", 23)
-
try:
-
cursor.execute(sql)
-
except Exception, e:
-
print e
-
-
sql = "insert into test1(name, age) values ('%s', %d)" % ("張三", 21)
-
try:
-
cursor.execute(sql)
-
except Exception, e:
-
print e
-
# 插入多條
-
-
sql = "insert into test1(name, age) values (%s, %s)"
-
val = (("李四", 24), ("王五", 25), ("洪六", 26))
-
try:
-
cursor.executemany(sql, val)
-
except Exception, e:
-
print e
-
-
#查詢出數據
-
sql = "select * from test1"
-
cursor.execute(sql)
-
alldata = cursor.fetchall()
-
# 如果有數據返回,就循環輸出, alldata是有個二維的列表
-
if alldata:
-
for rec in alldata:
-
print rec[0], rec[1]
-
-
-
cursor.close()
-
- conn.close()
相關資料:
1.引入MySQLdb庫
import MySQLdb
2.和數據庫建立連接
conn=MySQLdb.connect(host="localhost",user="root",passwd="sa",db="mytable",charset="utf8")
提供的connect方法用來和數據庫建立連接,接收數個參數,返回連接對象.
比較常用的參數包括:
host:數據庫主機名.默認是用本地主機.
user:數據庫登陸名.默認是當前用戶.
passwd:數據庫登陸的祕密.默認爲空.
db:要使用的數據庫名.沒有默認值.
port:MySQL服務使用的TCP端口.默認是3306.
charset:數據庫編碼.
更多關於參數的信息可以查這裏
http://mysql-python.sourceforge.net/MySQLdb.html
然後,這個連接對象也提供了對事務操作的支持,標準的方法
commit() 提交
rollback() 回滾
3.執行sql語句和接收返回值
cursor=conn.cursor()
n=cursor.execute(sql,param)
首先,我們用使用連接對象獲得一個cursor對象,接下來,我們會使用cursor提供的方法來進行工作.這些方法包括兩大類:1.執行命令,2.接收返回值
cursor用來執行命令的方法:
callproc(self, procname, args):用來執行存儲過程,接收的參數爲存儲過程名和參數列表,返回值爲受影響的行數
execute(self, query, args):執行單條sql語句,接收的參數爲sql語句本身和使用的參數列表,返回值爲受影響的行數
executemany(self, query, args):執行單條sql語句,但是重複執行參數列表裏的參數,返回值爲受影響的行數
nextset(self):移動到下一個結果集
cursor用來接收返回值的方法:
fetchall(self):接收全部的返回結果行.
fetchmany(self, size=None):接收size條返回結果行.如果size的值大於返回的結果行的數量,則會返回cursor.arraysize條數據.
fetchone(self):返回一條結果行.
scroll(self, value, mode='relative'):移動指針到某一行.如果mode='relative',則表示從當前所在行移動value條,如果mode='absolute',則表示從結果集的第一行移動value條.
下面的代碼是一個完整的例子.
#使用sql語句,這裏要接收的參數都用%s佔位符.要注意的是,無論你要插入的數據是什麼類型,佔位符永遠都要用%s
sql="insert into cdinfo values(%s,%s,%s,%s,%s)"
#param應該爲tuple或者list
param=(title,singer,imgurl,url,alpha)
#執行,如果成功,n的值爲1
n=cursor.execute(sql,param)
#再來執行一個查詢的操作
cursor.execute("select * from cdinfo")
#我們使用了fetchall這個方法.這樣,cds裏保存的將會是查詢返回的全部結果.每條結果都是一個tuple類型的數據,這些tuple組成了一個tuple
cds=cursor.fetchall()
#因爲是tuple,所以可以這樣使用結果集
print cds[0][3]
#或者直接顯示出來,看看結果集的真實樣子
print cds
#如果需要批量的插入數據,就這樣做
sql="insert into cdinfo values(0,%s,%s,%s,%s,%s)"
#每個值的集合爲一個tuple,整個參數集組成一個tuple,或者list
param=((title,singer,imgurl,url,alpha),(title2,singer2,imgurl2,url2,alpha2))
#使用executemany方法來批量的插入數據.這真是一個很酷的方法!
n=cursor.executemany(sql,param)
4.關閉數據庫連接
需要分別的關閉指針對象和連接對象.他們有名字相同的方法
cursor.close()
conn.close()
四步完成,基本的數據庫操作就是這樣了.下面是兩個有用的連接
MySQLdb用戶指南: http://mysql-python.sourceforge.net/MySQLdb.html
MySQLdb文檔: http://mysql-python.sourceforge.net/MySQLdb-1.2.2/public/MySQLdb-module.html
5 編碼(防止亂碼)
需要注意的點:
1 Python文件設置編碼 utf-8 (文件前面加上 #encoding=utf-8)
2 MySQL數據庫charset=utf-8
3 Python連接MySQL是加上參數 charset=utf8
4 設置Python的默認編碼爲 utf-8 (sys.setdefaultencoding(utf-8)
注:MySQL的配置文件設置也必須配置成utf8
設置 MySQL 的 my.cnf 文件,在 [client]/[mysqld]部分都設置默認的字符集(通常在/etc/mysql/my.cnf):
default-character-set = utf8
[mysqld]
default-character-set = utf8 (本節引自http://drizzlewalk.blog.51cto.com/2203401/448874)