﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-一江春水向东流-文章分类-python</title><link>http://www.blogjava.net/huyi2006/category/36908.html</link><description>                            做一个有思想的人,期待与每一位热爱思考的人交流,您的关注是对我最大的支持。</description><language>zh-cn</language><lastBuildDate>Tue, 23 Dec 2008 19:41:53 GMT</lastBuildDate><pubDate>Tue, 23 Dec 2008 19:41:53 GMT</pubDate><ttl>60</ttl><item><title>Python 連接 MySQL</title><link>http://www.blogjava.net/huyi2006/articles/247966.html</link><dc:creator>allic</dc:creator><author>allic</author><pubDate>Tue, 23 Dec 2008 12:19:00 GMT</pubDate><guid>http://www.blogjava.net/huyi2006/articles/247966.html</guid><wfw:comment>http://www.blogjava.net/huyi2006/comments/247966.html</wfw:comment><comments>http://www.blogjava.net/huyi2006/articles/247966.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/huyi2006/comments/commentRss/247966.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/huyi2006/services/trackbacks/247966.html</trackback:ping><description><![CDATA[
		<h2 class="post-title">
				<a title="Python 連接 MySQL" href="http://www.real-blog.com/programming/417" rel="bookmark">Python 連接 MySQL</a>
		</h2>
		<div class="post-content">
				<p>MySQL 是十分流行的開源資料庫系統，很多網站也是使用 MySQL 作為後台資料儲存，而 Python 要連接 MySQL 可以使用 MySQL 模組。MySQLdb 模組可以讓 Python 程式連線到 MySQL server, 執行 SQL 語句及擷取資料等。</p>
				<p>開始前要確定系統內的 Python 有安裝 MySQLdb 模式，你可以 Python command line interpreter 檢查，在指令模式輸入 python，然後便可以開始檢查：</p>
				<div class="shell">Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)<br />[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2<br />Type "help", "copyright", "credits" or "license" for more information.<br />&gt;&gt;&gt; import MySQLdb<br />Traceback (most recent call last):<br />File "<stdin>", line 1, in <module><br />ImportError: No module named MySQLdb<br />&gt;&gt;&gt; exit()</module></stdin></div>
				<p>
						<br />如果見以上面的 "ImportError: No module named MySQLdb" 一句，便表示系統沒有安裝，到 <a title="MySQLdb" href="http://sourceforge.net/projects/mysql-python" target="_blank">MySQLdb 官方網站</a> 下載 MySQLdb，並用以下方法安裝：</p>
				<div class="shell">$ tar zxvf MySQL-python-1.2.2.tar.gz<br />$ cd MySQL-python-1.2.2<br />$ python setup.py build<br />$ python setup.py install</div>
				<p>
						<br />安裝好 MySQLdb 後便可以編寫程式碼，以下是簡單的例子：</p>
				<div class="igBar">
						<span id="lcode-1">
								<a onclick="javascript:showPlainTxt('code-1'); return false;" href="http://www.real-blog.com/programming/417#">PLAIN TEXT</a>
						</span>
				</div>
				<div class="syntax_hilite">
						<span class="langName">CODE:</span>
						<div id="code-1">
								<div class="code">
										<ol>
												<li style="FONT-WEIGHT: normal; COLOR: #3a6a8b; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace">#!/usr/bin/python</div>
												</li>
												<li style="FONT-WEIGHT: bold; COLOR: #26536a">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"> </div>
												</li>
												<li style="FONT-WEIGHT: normal; COLOR: #3a6a8b; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"># 引入 MySQL 模組</div>
												</li>
												<li style="FONT-WEIGHT: bold; COLOR: #26536a">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace">import MySQLdb</div>
												</li>
												<li style="FONT-WEIGHT: normal; COLOR: #3a6a8b; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"> </div>
												</li>
												<li style="FONT-WEIGHT: bold; COLOR: #26536a">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"># 連接到 MySQL</div>
												</li>
												<li style="FONT-WEIGHT: normal; COLOR: #3a6a8b; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace">db = MySQLdb.<span>connect</span><span style="FONT-WEIGHT: bold; COLOR: #006600">(</span>host=<span style="COLOR: #cc0000">"localhost"</span>, user=<span style="COLOR: #cc0000">"db_user"</span>, passwd=<span style="COLOR: #cc0000">"db_pass"</span>, db=<span style="COLOR: #cc0000">"db_name"</span><span style="FONT-WEIGHT: bold; COLOR: #006600">)</span></div>
												</li>
												<li style="FONT-WEIGHT: bold; COLOR: #26536a">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace">cursor = db.<span>cursor</span><span style="FONT-WEIGHT: bold; COLOR: #006600">(</span><span style="FONT-WEIGHT: bold; COLOR: #006600">)</span></div>
												</li>
												<li style="FONT-WEIGHT: normal; COLOR: #3a6a8b; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"> </div>
												</li>
												<li style="FONT-WEIGHT: bold; COLOR: #26536a">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"># 執行 SQL 語句</div>
												</li>
												<li style="FONT-WEIGHT: normal; COLOR: #3a6a8b; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace">cursor.<span>execute</span><span style="FONT-WEIGHT: bold; COLOR: #006600">(</span><span style="COLOR: #cc0000">"SELECT * FROM db_table"</span><span style="FONT-WEIGHT: bold; COLOR: #006600">)</span></div>
												</li>
												<li style="FONT-WEIGHT: bold; COLOR: #26536a">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace">result = cursor.<span>fetchall</span><span style="FONT-WEIGHT: bold; COLOR: #006600">(</span><span style="FONT-WEIGHT: bold; COLOR: #006600">)</span></div>
												</li>
												<li style="FONT-WEIGHT: normal; COLOR: #3a6a8b; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"> </div>
												</li>
												<li style="FONT-WEIGHT: bold; COLOR: #26536a">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace"># 輸出結果</div>
												</li>
												<li style="FONT-WEIGHT: normal; COLOR: #3a6a8b; FONT-STYLE: normal; FONT-FAMILY: 'Courier New', Courier, monospace">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace">for record in result:</div>
												</li>
												<li style="FONT-WEIGHT: bold; COLOR: #26536a">
														<div style="FONT-WEIGHT: normal; FONT-FAMILY: 'Courier New', Courier, monospace">    print record<span style="FONT-WEIGHT: bold; COLOR: #006600">[</span><span style="COLOR: #800000">0</span><span style="FONT-WEIGHT: bold; COLOR: #006600">]</span></div>
												</li>
										</ol>
								</div>
						</div>
				</div>
		</div>
<img src ="http://www.blogjava.net/huyi2006/aggbug/247966.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/huyi2006/" target="_blank">allic</a> 2008-12-23 20:19 <a href="http://www.blogjava.net/huyi2006/articles/247966.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>