<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">博客园_空地</title><subtitle type="text">生活，工作，记录(内容基本都是转载，自己工作用到的时候看看而已)</subtitle><id>http://feed.cnblogs.com/blog/u/22656/rss</id><updated>2010-11-18T14:59:51Z</updated><author><name>小梅</name><uri>http://www.cnblogs.com/ceniy/</uri></author><generator>CNBlogs BlogServer</generator><link rel="alternate" type="text/html" href="http://www.cnblogs.com/ceniy/"/><link rel="self" type="application/atom+xml" href="http://feed.cnblogs.com/blog/u/22656/rss"/><entry><id>http://www.cnblogs.com/ceniy/archive/2010/09/01/1814807.html</id><title type="text">ZendStudio 6.0.1 升级时 RSE 错误的问题</title><summary type="text">ZendStudio 6.0.1 在线升级的时候会出现如下错误:  When performing new features (plug-ins) installation the  following message may appear in the Search Results dialog:RSE Feature  Patch (3.0.2.v20090119-1307w311_122_)...</summary><published>2010-09-01T05:55:00Z</published><updated>2010-09-01T05:55:00Z</updated><author><name>小梅</name><uri>http://www.cnblogs.com/ceniy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/ceniy/archive/2010/09/01/1814807.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/ceniy/archive/2010/09/01/1814807.html"/><content type="html">&lt;div clear-block=""&gt;     &lt;p&gt;ZendStudio 6.0.1 在线升级的时候会出现如下错误:&lt;/p&gt;  &lt;p&gt;&lt;code&gt;When performing new features (plug-ins) installation the  following message may appear in the Search Results dialog:RSE    Feature  Patch (3.0.2.v20090119-1307w311_122_) requires feature  "org.eclipse.rse"&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;解决办法为&lt;a href="http://www.zend.com/en/support/knowledgebase.php?kbid=356&amp;amp;view_only=1"&gt;http://www.zend.com/en/support/knowledgebase.php?kbid=356&amp;amp;view_only=1&lt;/a&gt;，其实就是需要安装RSE相关的一些包。&lt;/p&gt;  &lt;p&gt;转载如下：&lt;/p&gt;  &lt;ol&gt;&lt;li&gt;In Zend Studio for Eclipse, go to Help | Software Updates | Find and Install... &lt;img src="http://ftp.zend.com/support/ext/kb356/RSEupd_1.png" alt="ZendStudioRse" title="ZendStudioRse问题辅助图片" /&gt;&lt;/li&gt;&lt;li&gt;In the Feature Updates dialog, select the second option Search for new features to install and click Next &lt;img src="http://ftp.zend.com/support/ext/kb356/RSEupd_2.png" alt="ZendStudioRse" title="ZendStudioRse问题辅助图片" /&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;In the Update sites to visit dialog, select the Target Management Updates and click Finish &lt;img src="http://ftp.zend.com/support/ext/kb356/RSEupd_3.png" alt="ZendStudioRse" title="ZendStudioRse问题辅助图片" /&gt;&lt;/p&gt;  &lt;p&gt;&lt;code&gt;Note: You may want to check the Automatically select mirrors  checkbox before clicking Finish, in order to skip the mirror selection  prompt&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;&lt;img src="http://ftp.zend.com/support/ext/kb356/RSEupd_4.png" alt="ZendStudioRse" title="ZendStudioRse问题辅助图片" /&gt;&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;In the Search Results dialog, uncheck the Show the latest version of a feature only checkbox&lt;/p&gt;  &lt;p&gt;Open the hierarchy Target Management Updates | TM and RSE 3.0.2 Mark the first three plug-ins, namely:&lt;/p&gt;  &lt;ul&gt;&lt;li&gt;Remote System Explorer Core 3.0.2.v200812041720-7Z37BEB7sQS9UkXMIjJI&lt;/li&gt;&lt;li&gt;RSE DStore Services 3.0.2.v200812041720-7J35DUUEFDHoHxAT97&lt;/li&gt;&lt;li&gt;Remote System Explorer End-User Runtime 3.0.2.v200812041720-7H3788qfeqHMeUOEQ2IKjVLIiQ8&lt;/li&gt;&lt;/ul&gt;  &lt;p&gt;&lt;img src="http://ftp.zend.com/support/ext/kb356/RSEupd_5.png" alt="ZendStudioRse" title="ZendStudioRse问题辅助图片" /&gt; &lt;strong&gt;注：这里需要注意，由于选中了显示最新的软件包，可能需要的版本没有出现。这就需要取消最新版本那个选项，然后选择出现错误所需要的那个版本即可。&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;&lt;li&gt;Click Next and complete the installation by accepting the defaults in the dialogs to follow&lt;/li&gt;&lt;li&gt;Restart Zend Studio for Eclipse&lt;/li&gt;&lt;/ol&gt;   &lt;/div&gt;&lt;img src="http://www.cnblogs.com/ceniy/aggbug/1814807.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/ceniy/archive/2010/09/01/1814807.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/ceniy/archive/2010/08/31/1814066.html</id><title type="text">python mysql 数据库交互</title><summary type="text">1.确认 已经安装了MySQLdb module然后用这个短小的python 代码，检索并打印出每条记录然后我们一步一步认识下这个过程，先是 python 导入 MySQLdb module之后打开一个数据库连接，需要参数 服务器，用户名，密码，数据库名成功连接后可以得到一个db对象，可以生成游标这个游标用于执行SQL，并返回得到的结果我们还可以用fetchmany()方法去指定获取的条数。很明显...</summary><published>2010-08-31T13:57:00Z</published><updated>2010-08-31T13:57:00Z</updated><author><name>小梅</name><uri>http://www.cnblogs.com/ceniy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/ceniy/archive/2010/08/31/1814066.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/ceniy/archive/2010/08/31/1814066.html"/><content type="html">1.确认 已经安装了&amp;nbsp;&lt;span  style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: normal; font-size: 12px; "&gt;MySQLdb module&amp;nbsp;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;font color="#008000"&gt;Python 1.5.2 (#1, Feb 1 2000, 16:32:16) [GCC egcs-2.91.66&#xD;
19990314/Linux&#xD;
(egcs- on linux-i386&#xD;
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam&#xD;
&amp;gt;&amp;gt;&amp;gt; import MySQLdb&#xD;
Traceback (innermost last):&#xD;
File "&amp;lt;stdin&amp;gt;", line 1, in ?&#xD;
ImportError: No module named MySQLdb&#xD;
&amp;gt;&amp;gt;&amp;gt; &lt;/font&gt;&lt;/pre&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;font  face="verdana, 'courier new'" size="4"&gt;&lt;span  style="font-size: 14px; line-height: 21px; white-space: normal;"&gt;这样的消息说明没有安装MySQLdb, 从&lt;/span&gt;&lt;/font&gt;&lt;span  style="font-family: Verdana, Arial, Helvetica, sans-serif; white-space: normal; "&gt;&lt;a rel="nofollow" href="http://sourceforge.net/projects/mysql-python"&gt;http://sourceforge.net/projects/mysql-python&lt;/a&gt;下载安装。&lt;/span&gt;&lt;/pre&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;span  style="font-family: Verdana, Arial, Helvetica, sans-serif; white-space: normal; "&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;$ tar -xzvf MySQL-python-0.9.2.tar.gz&#xD;
$ cd MySQL-python-0.9.2&#xD;
$ python setup.py build&#xD;
$ python setup.py install&lt;/pre&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;br /&gt;&#xD;
&lt;/pre&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;再检查：&lt;/pre&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;span  style="font-family: Verdana, Arial, Helvetica, sans-serif; white-space: normal; "&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;font color="#008000"&gt;Python 1.5.2 (#1, Feb 1 2000, 16:32:16) [GCC egcs-2.91.66&#xD;
19990314/Linux&#xD;
(egcs- on linux-i386&#xD;
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam&#xD;
&amp;gt;&amp;gt;&amp;gt; import MySQLdb&#xD;
&amp;gt;&amp;gt;&amp;gt; &lt;/font&gt;&lt;/pre&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;font  color="#008000"&gt;&lt;span  style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; white-space: normal; "&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;span  style="font-family: Verdana, Arial, Helvetica, sans-serif; white-space: normal; "&gt;&lt;br /&gt;&#xD;
&lt;/span&gt;&lt;/pre&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;span  style="font-family: Verdana, Arial, Helvetica, sans-serif; white-space: normal; "&gt;然后我们用以下这个数据表：&lt;/span&gt;&lt;/pre&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;span  style="font-family: Verdana, Arial, Helvetica, sans-serif; white-space: normal; "&gt;&#xD;
&lt;div  align="center" style="text-align: right; width: 672px; background-color: #ffffff; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; clear: both; "&gt;&#xD;
&lt;div align="center"&gt;&lt;a rel="nofollow" href="http://www.devshed.com/c/a/Python/MySQL-Connectivity-With-Python/4/"&gt;&#xD;
&lt;/a&gt;&lt;/div&gt;&#xD;
&lt;/div&gt;&#xD;
&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;font color="#008000"&gt;&lt;span  style="white-space: nowrap;"&gt;mysql&lt;/span&gt;&amp;gt; SELECT * FROM animals;&#xD;
+---------+----------+&#xD;
| name | species |&#xD;
+---------+----------+&#xD;
| Wallace | Walrus |&#xD;
| Polly | Parrot |&#xD;
| Freddie | Frog |&#xD;
| Tusker | Elephant |&#xD;
| Sammy | Skunk |&#xD;
+---------+----------+&#xD;
5 rows in set (0.01 sec)&lt;/font&gt;&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;/pre&gt;&#xD;
&lt;/blockquote&gt;然后用这个短小的python 代码，检索并打印出每条记录&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
#!/usr/bin/python&#xD;
# import MySQL module&#xD;
import MySQLdb&#xD;
# connect&#xD;
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",&#xD;
db="db56a")&#xD;
# create a cursor&#xD;
cursor = db.cursor()&#xD;
# execute &lt;span  style="white-space: nowrap;"&gt;SQL&lt;/span&gt; statement&#xD;
cursor.execute("SELECT * FROM animals")&#xD;
# get the resultset as a tuple&#xD;
result = cursor.fetchall()&#xD;
# iterate through resultset&#xD;
for record in result:&#xD;
print record[0] , "--&amp;gt;", record[1]&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;/pre&gt;&#xD;
&lt;/blockquote&gt;然后我们一步一步认识下这个过程，先是 python 导入 MySQLdb module&lt;br /&gt;&#xD;
&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
# import MySQL module&#xD;
import MySQLdb&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;/pre&gt;&#xD;
&lt;/blockquote&gt;之后打开一个数据库连接，需要参数 服务器，用户名，密码，数据库名&lt;br /&gt;&#xD;
&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
# connect&#xD;
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",&#xD;
db="db56a")&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;/pre&gt;&#xD;
&lt;/blockquote&gt;成功连接后可以得到一个db对象，可以生成游标&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
# create a cursor&#xD;
cursor = db.cursor()&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;/pre&gt;&#xD;
&lt;/blockquote&gt;这个游标用于执行SQL，并返回得到的结果&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
# execute SQL statement&#xD;
cursor.execute("SELECT * FROM animals")&#xD;
# get the resultset as a tuple&#xD;
result = cursor.fetchall()&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;/pre&gt;&#xD;
&lt;/blockquote&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;span  style="font-family: Verdana, Arial, Helvetica, sans-serif; white-space: normal; "&gt;有很多种方法去获得这个SQL 结果，这里使用的是fetchall(),他会返回一个元祖集,它里面的每一个元祖表示一行数据，可以用for进行循环显示每一个数据。&lt;/span&gt;&lt;/pre&gt;&#xD;
&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;# iterate through resultset&#xD;
for record in result:&#xD;
print record[0] , "--&amp;gt;", record[1]&lt;/pre&gt;&#xD;
&lt;/blockquote&gt;&lt;/span&gt;&lt;/pre&gt;&#xD;
&lt;/span&gt;&lt;/font&gt;&lt;/pre&gt;&#xD;
&lt;/span&gt;&lt;/pre&gt;&#xD;
&lt;/span&gt;&lt;/pre&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;span  style="font-family: Verdana, Arial, Helvetica, sans-serif; white-space: normal; "&gt;也可以用fetchone()每次去获取一条数据，看下面的例子：&lt;/span&gt;&lt;/pre&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;span  style="font-family: Verdana, Arial, Helvetica, sans-serif; white-space: normal; "&gt;&lt;blockquote style="background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #efefef; border-right-color: #efefef; border-bottom-color: #efefef; border-left-color: #efefef; color: #333333; margin-left: 25px; padding-left: 10px; padding-right: 10px; padding-top: 5px; padding-bottom: 5px; margin-top: 10px; margin-bottom: 10px; background-position: initial initial; background-repeat: initial initial; "&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;#!/usr/bin/python&#xD;
# import MySQL module&#xD;
import MySQLdb&#xD;
# connect&#xD;
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",&#xD;
db="db56a")&#xD;
# create a cursor&#xD;
cursor = db.cursor()&#xD;
# execute SQL statement&#xD;
cursor.execute("SELECT * FROM animals")&#xD;
# get the number of rows in the resultset&#xD;
numrows = int(cursor.rowcount)&#xD;
# get and display one row at a time&#xD;
for x in range(0,numrows):&#xD;
row = cursor.fetchone()&#xD;
print row[0], "--&amp;gt;", row[1]&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;/pre&gt;&#xD;
&lt;/blockquote&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;span  style="font-family: Verdana, Arial, Helvetica, sans-serif; white-space: normal; "&gt;这里，我们先用游标的rowcount ()方法去获取结果集的条数。然后用在for循环中，去用fetchone（）方法去获取每行数据。&lt;/span&gt;&lt;/pre&gt;&#xD;
我们还可以用fetchmany()方法去指定获取的条数。&lt;br /&gt;&#xD;
&lt;blockquote style="background-image: none; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: initial; border-top-width: 2px; border-right-width: 2px; border-bottom-width: 2px; border-left-width: 2px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #efefef; border-right-color: #efefef; border-bottom-color: #efefef; border-left-color: #efefef; color: #333333; margin-left: 25px; padding-left: 10px; padding-right: 10px; padding-top: 5px; padding-bottom: 5px; margin-top: 10px; margin-bottom: 10px; background-position: initial initial; background-repeat: initial initial; "&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
#!/usr/bin/python&#xD;
# import MySQL module&#xD;
import MySQLdb&#xD;
# connect&#xD;
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",&#xD;
db="db56a")&#xD;
# create a cursor&#xD;
cursor = db.cursor()&#xD;
# execute SQL statement&#xD;
cursor.execute("SELECT * FROM animals")&#xD;
# limit the resultset to 3 items&#xD;
result = cursor.fetchmany(3)&#xD;
# iterate through resultset&#xD;
for record in result:&#xD;
print record[0] , "--&amp;gt;", record[1]&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;/pre&gt;&#xD;
&lt;/blockquote&gt;&#xD;
&lt;div  align="center" style="text-align: right; width: 672px; background-color: #ffffff; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; clear: both; "&gt;&#xD;
&lt;div align="center"&gt;&#xD;
&lt;div &gt;&lt;br /&gt;&#xD;
&lt;/div&gt;&#xD;
&lt;/div&gt;&#xD;
&lt;/div&gt;&#xD;
&lt;/span&gt;&lt;/pre&gt;&#xD;
&lt;/span&gt;&lt;span  style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: normal; font-size: 12px; "&gt;&#xD;
&lt;p&gt;很明显的，你可以执行 插入，更新 和 删除通过 MySQLdb &amp;nbsp;module&amp;nbsp;&lt;/p&gt;&#xD;
&lt;p&gt;&lt;/p&gt;&#xD;
&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;#!/usr/bin/python&#xD;
# import &lt;span  style="white-space: nowrap;"&gt;MySQL&lt;/span&gt; module&#xD;
import MySQLdb&#xD;
# connect&#xD;
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",&#xD;
db="db56a")&#xD;
# create a cursor&#xD;
cursor = db.cursor()&#xD;
# execute SQL statement&#xD;
cursor.execute("""INSERT INTO animals (name, species) VALUES ("Harry",&#xD;
"Hamster")""")&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;/pre&gt;&#xD;
&lt;/blockquote&gt;修改一下，我们让用户输入信息，保存到数据库&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
#!/usr/bin/python&#xD;
# import MySQL module&#xD;
import MySQLdb&#xD;
# get user input&#xD;
name = raw_input("Please enter a name: ")&#xD;
species = raw_input("Please enter a species: ")&#xD;
# connect&#xD;
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",&#xD;
db="db56a")&#xD;
# create a cursor&#xD;
cursor = db.cursor()&#xD;
# execute &lt;span  style="white-space: nowrap;"&gt;SQL&lt;/span&gt; statement&#xD;
cursor.execute("INSERT INTO animals (name, species) VALUES (%s, %s)",&#xD;
(name, species))&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;/pre&gt;&#xD;
&lt;/blockquote&gt;这次运行的时候，你需要输入信息：&lt;br /&gt;&#xD;
&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;font color="#008000"&gt;Please enter a name: Rollo&#xD;
Please enter a species: Rat&lt;/font&gt;&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;/pre&gt;&#xD;
&lt;/blockquote&gt;&#xD;
&lt;p&gt;注意%s位置 会被 (name,species)元组的值 替换。&lt;/p&gt;&#xD;
&lt;p&gt;&lt;/p&gt;&#xD;
&lt;p&gt;如果你有自动增长的字段，你可以使用insert_id()去获取最后一条插入的数据的ID。&amp;nbsp;&lt;/p&gt;&#xD;
&lt;br /&gt;&#xD;
&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&#xD;
&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
#!/usr/bin/python&#xD;
# import MySQL module&#xD;
import MySQLdb&#xD;
# connect&#xD;
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",&#xD;
db="db56a")&#xD;
# create a cursor&#xD;
cursor = db.cursor()&#xD;
# execute SQL statement&#xD;
cursor.execute("""INSERT INTO test (field1, field2) VALUES ("val1",&#xD;
"val2")""")&#xD;
# get ID of last inserted record&#xD;
print "ID of inserted record is ", int(cursor.insert_id())&lt;hr color="#cccccc" noshade="" size="1" /&gt;&#xD;
&lt;/pre&gt;&#xD;
&lt;/blockquote&gt;&#xD;
&lt;div  align="center" style="text-align: right; width: 672px; background-color: #ffffff; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; clear: both; "&gt;&#xD;
&lt;div align="center"&gt;&lt;br /&gt;&lt;/div&gt;&#xD;
&lt;/div&gt;&#xD;
&lt;/span&gt;&lt;span  style="font-family: Verdana, Arial, Helvetica, sans-serif; line-height: normal; font-size: 12px; "&gt;&lt;p&gt;许多数据库脚本涉及到多次的数据操作（比如插入），每次插入大量的不同的值，MySQLdb有一个executemany()方法，可以简化这种操作。&amp;nbsp;&lt;/p&gt;&lt;p&gt;看下面的例子：&lt;/p&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;&lt;hr color="#cccccc" noshade="" size="1" /&gt;#!/usr/bin/python&#xD;
# import &lt;span  style="white-space: nowrap;"&gt;SQL&lt;/span&gt; module&#xD;
import MySQLdb&#xD;
# connect&#xD;
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",&#xD;
db="db56a")&#xD;
# create a cursor&#xD;
cursor = db.cursor()&#xD;
# dynamically generate SQL statements from  list&#xD;
cursor.executemany("INSERT INTO animals (name, species) VALUES (%s,&#xD;
%s)", [  ('Rollo', 'Rat'),  ('Dudley', 'Dolphin'),  ('Mark', 'Marmoset')&#xD;
])&lt;hr color="#cccccc" noshade="" size="1" /&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p&gt;这里，同样的操作被执行了很多遍（不同的值），通过传递给executemany()一个Python list，每个元素是一个元组。&amp;nbsp;&lt;/p&gt;&lt;p&gt;用这种方式，我们可以让用户一次输入大量的数据，然后一次性保存。&lt;/p&gt;&lt;br /&gt;&lt;blockquote style='border:2px solid #EFEFEF;color:#333333;padding:5px 10px;'&gt;&lt;pre style="font-family: 'Courier New'; font-size: 9pt; "&gt;#!/usr/bin/python&#xD;
# import MySQL module&#xD;
import MySQLdb&#xD;
# initialize some variables&#xD;
name = ""&#xD;
data = []&#xD;
# loop and ask for user input&#xD;
while (1):&#xD;
name = raw_input("Please enter a name (EOF to end): ")&#xD;
if name == "EOF":&#xD;
break&#xD;
species = raw_input("Please enter a species: ")&#xD;
# put user input into a tuple&#xD;
tuple = (name, species)&#xD;
# and append to data[] list&#xD;
data.append(tuple)&#xD;
# connect&#xD;
db = MySQLdb.connect(host="localhost", user="joe", passwd="secret",&#xD;
db="db56a")&#xD;
# create a cursor&#xD;
cursor = db.cursor()&#xD;
# dynamically generate &lt;span  style="white-space: nowrap;"&gt;SQL&amp;nbsp;&lt;/span&gt;statements from data[] list&#xD;
cursor.executemany("INSERT INTO animals (name, species) VALUES (%s,&#xD;
%s)",&#xD;
data)&lt;hr color="#cccccc" noshade="" size="1" /&gt;&lt;/pre&gt;&lt;/blockquote&gt;&lt;p&gt;这里我们用while 循环让用户输入信息，保存到values 列表，当用户输入完成的时候，执行executemany（）&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;MySQLdb 类还有很多其他有用的方法 ：&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;connection.begin() - 开始一个事务&lt;br /&gt;&lt;br /&gt;connection.apilevel() - returns the current DB&amp;nbsp;&lt;span  style="white-space: nowrap;"&gt;API&lt;/span&gt;level&lt;br /&gt;&lt;br /&gt;connection.conv() - set type conversion options between&amp;nbsp;&lt;span  style="white-space: nowrap; "&gt;MySQL&lt;/span&gt;&amp;nbsp;and Python&lt;br /&gt;&lt;br /&gt;connection.commit() - commit a transaction&lt;br /&gt;&lt;br /&gt;connection.rollback() - roll back a transaction&lt;br /&gt;&lt;br /&gt;有用的链接：&lt;br /&gt;&lt;br /&gt;The MySQLdb project page on SourceForge, at&lt;a rel="nofollow" href="http://sourceforge.net/projects/mysql-python"&gt;http://sourceforge.net/projects/mysql-python&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The Python home page, at&amp;nbsp;&lt;a rel="nofollow" href="http://www.python.org/"&gt;http://www.python.org/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The MySQL home page, at&amp;nbsp;&lt;a rel="nofollow" href="http://www.mysql.com/"&gt;http://www.mysql.com/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Note: All examples in this article have been tested on&amp;nbsp;&lt;a  classname="iAs" href="http://www.devshed.com/c/a/Python/MySQL-Connectivity-With-Python/6/#" target="_blank" itxtdid="23156308" style="float: none; left: auto; right: auto; top: auto; bottom: auto; border-top-style: none; border-right-style: none; border-bottom-style: dotted !important; border-left-style: none; border-width: initial; border-color: initial; background-color: transparent !important; line-height: normal; text-align: left; position: static !important; display: inline; font-family: inherit !important; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px !important; padding-left: 0px; font-weight: normal !important; font-size: 12px; text-decoration: none !important; border-bottom-color: #2b65b0 !important; border-bottom-width: 0.2em !important; color: #2b65b0 !important; "&gt;&lt;nobr id="itxt_nobr_13_0" style="color: #2b65b0; font-weight: normal; font-size: 12px; "&gt;Linux&lt;img src="http://images.intellitxt.com/ast/adTypes/mag-glass_10x10.gif" name="itxt-icon-0" style="display: inline !important; height: 10px; width: 10px; position: relative; top: 1px; left: 1px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; float: none; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "  alt="" /&gt;&lt;/nobr&gt;&lt;/a&gt;/i586 with Python 1.5.2, MySQL 3.23 and MySQLdb 0.9.2. Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!&lt;/p&gt;&lt;/span&gt;&lt;img src="http://www.cnblogs.com/ceniy/aggbug/1814066.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/ceniy/archive/2010/08/31/1814066.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/ceniy/archive/2008/02/02/1062963.html</id><title type="text">交叉表 查询 例子(Sql Server 2005)</title><summary type="text"/><published>2008-02-02T12:52:00Z</published><updated>2008-02-02T12:52:00Z</updated><author><name>小梅</name><uri>http://www.cnblogs.com/ceniy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/ceniy/archive/2008/02/02/1062963.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/ceniy/archive/2008/02/02/1062963.html"/></entry><entry><id>http://www.cnblogs.com/ceniy/archive/2006/11/09/555600.html</id><title type="text">showModalDialog()、showModelessDialog()方法使用详解(转载)</title><summary type="text"/><published>2006-11-09T09:31:00Z</published><updated>2006-11-09T09:31:00Z</updated><author><name>小梅</name><uri>http://www.cnblogs.com/ceniy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/ceniy/archive/2006/11/09/555600.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/ceniy/archive/2006/11/09/555600.html"/></entry><entry><id>http://www.cnblogs.com/ceniy/archive/2006/11/09/555566.html</id><title type="text">表结构的复制方法</title><summary type="text"/><published>2006-11-09T09:15:00Z</published><updated>2006-11-09T09:15:00Z</updated><author><name>小梅</name><uri>http://www.cnblogs.com/ceniy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/ceniy/archive/2006/11/09/555566.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/ceniy/archive/2006/11/09/555566.html"/></entry><entry><id>http://www.cnblogs.com/ceniy/archive/2006/11/09/555023.html</id><title type="text">今天发现的东西</title><summary type="text"/><published>2006-11-09T03:02:00Z</published><updated>2006-11-09T03:02:00Z</updated><author><name>小梅</name><uri>http://www.cnblogs.com/ceniy/</uri></author><link rel="alternate" href="http://www.cnblogs.com/ceniy/archive/2006/11/09/555023.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/ceniy/archive/2006/11/09/555023.html"/></entry></feed>
