{"id":1103,"date":"2010-05-25T15:59:00","date_gmt":"2010-05-25T07:59:00","guid":{"rendered":"\/?p=1103"},"modified":"2010-05-25T15:59:00","modified_gmt":"2010-05-25T15:59:00","slug":"1103","status":"publish","type":"post","link":"https:\/\/blog.vimge.com\/archives\/other\/1103.html","title":{"rendered":"MySQLdb for Python\u4f7f\u7528\u6307\u5357\/Python\u7684\u6570\u636e\u5e93\u64cd\u4f5c"},"content":{"rendered":"
\u6ce8\u610f\u4e00\u70b9<\/strong>\u5c31\u662f\u8fdb\u884c\u6570\u636eADU\u64cd\u4f5c\u65f6,\u8981conn.commit()\u8fdb\u884c\u63d0\u4ea4,\u4e0d\u7136\u6570\u636e\u5728\u6570\u636e\u5e93\u6709\u8fc7\u9017\u7559,\u4f46\u5e76\u672a\u6700\u540e\u5bf9\u6570\u636e\u8fdb\u884c\u5b9e\u8d28\u7684ADU.<\/p>\n \u53ef\u4ee5\u4ece\u8fd9\u91cc\u83b7\u5f97MySQLdb\u8fd9\u4e2a\u5e93http:\/\/sourceforge.net\/projects\/mysql-python<\/a> \u5728\u4ecb\u7ecd\u5177\u4f53\u7684\u64cd\u4f5c\u524d,\u5148\u82b1\u70b9\u65f6\u95f4\u6765\u8bf4\u8bf4\u4e00\u4e2a\u7a0b\u5e8f\u600e\u4e48\u548c\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92 0.\u5f15\u5165MySQLdb\u5e93 1.\u548c\u6570\u636e\u5e93\u5efa\u7acb\u8fde\u63a5<\/strong> \u6bd4\u8f83\u5e38\u7528\u7684\u53c2\u6570\u5305\u62ec \u7136\u540e,\u8fd9\u4e2a\u8fde\u63a5\u5bf9\u8c61\u4e5f\u63d0\u4f9b\u4e86\u5bf9\u4e8b\u52a1\u64cd\u4f5c\u7684\u652f\u6301,\u6807\u51c6\u7684\u65b9\u6cd5 2.\u6267\u884csql\u8bed\u53e5\u548c\u63a5\u6536\u8fd4\u56de\u503c<\/strong> cursor\u7528\u6765\u6267\u884c\u547d\u4ee4\u7684\u65b9\u6cd5: cursor\u7528\u6765\u63a5\u6536\u8fd4\u56de\u503c\u7684\u65b9\u6cd5: \u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50. #\u518d\u6765\u6267\u884c\u4e00\u4e2a\u67e5\u8be2\u7684\u64cd\u4f5c #\u5982\u679c\u9700\u8981\u6279\u91cf\u7684\u63d2\u5165\u6570\u636e,\u5c31\u8fd9\u6837\u505a \u9700\u8981\u6ce8\u610f\u7684\u662f(\u6216\u8005\u8bf4\u662f\u6211\u611f\u5230\u5947\u602a\u7684\u662f),\u5728\u6267\u884c\u5b8c\u63d2\u5165\u6216\u5220\u9664\u6216\u4fee\u6539\u64cd\u4f5c\u540e,\u9700\u8981\u8c03\u7528\u4e00\u4e0bconn.commit()\u65b9\u6cd5\u8fdb\u884c\u63d0\u4ea4.\u8fd9\u6837,\u6570\u636e\u624d\u4f1a\u771f\u6b63\u4fdd \u5b58\u5728\u6570\u636e\u5e93\u4e2d.\u6211\u4e0d\u6e05\u695a\u662f\u5426\u662f\u6211\u7684mysql\u8bbe\u7f6e\u95ee\u9898,\u603b\u4e4b,\u4eca\u5929\u6211\u5728\u4e00\u5f00\u59cb\u4f7f\u7528\u7684\u65f6\u5019,\u5982\u679c\u4e0d\u7528commit,\u90a3\u6570\u636e\u5c31\u4e0d\u4f1a\u4fdd\u7559\u5728\u6570\u636e\u5e93\u4e2d,\u4f46\u662f,\u6570\u636e \u786e\u5b9e\u5728\u6570\u636e\u5e93\u5446\u8fc7.\u56e0\u4e3a\u81ea\u52a8\u7f16\u53f7\u8fdb\u884c\u4e86\u7d2f\u79ef,\u800c\u4e14\u8fd4\u56de\u7684\u53d7\u5f71\u54cd\u7684\u884c\u6570\u5e76\u4e0d\u4e3a0.<\/p>\n 3.\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong> \u4e09\u6b65\u5b8c\u6210,\u57fa\u672c\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u5c31\u662f\u8fd9\u6837\u4e86.\u4e0b\u9762\u662f\u4e24\u4e2a\u6709\u7528\u7684\u8fde\u63a5 \u5f15\u7528:<\/strong> http:\/\/xiaolin0199.javaeye.com\/blog\/585574<\/p>\n","protected":false},"excerpt":{"rendered":" \u6ce8\u610f\u4e00\u70b9<\/strong>\u5c31\u662f\u8fdb\u884c\u6570\u636eADU\u64cd\u4f5c\u65f6,\u8981conn.commit()\u8fdb\u884c\u63d0\u4ea4,\u4e0d\u7136\u6570\u636e\u5728\u6570\u636e\u5e93\u6709\u8fc7\u9017\u7559,\u4f46\u5e76\u672a\u6700\u540e\u5bf9\u6570\u636e\u8fdb\u884c\u5b9e\u8d28\u7684ADU.<\/p>\n
\u5982\u679c\u4f60\u4e0d\u786e\u5b9a\u4f60\u7684python\u73af\u5883\u91cc\u6709\u6ca1\u6709\u8fd9\u4e2a\u5e93,\u90a3\u5c31\u6253\u5f00python shell,\u8f93\u5165 import MySQLdb,\u5982\u679c\u8fd4\u56de\u9519\u8bef\u4fe1\u606f,\u90a3\u5c31\u8868\u793a\u4f60\u7684\u673a\u5668\u4e0a\u6ca1\u6709,\u8d76\u7d27\u53bb\u4e0b\u8f7d\u4e00\u4e2a.\u6211\u7684\u673a\u5668\u662fwin xp,\u6240\u4ee5\u6211\u4e0b\u8f7d\u4e86win\u73af\u5883\u4e0b\u7684exe\u90a3\u4e2a,\u76f4\u63a5\u53cc\u51fb\u5b8c\u6210\u5b89\u88c5.<\/p>\n
1.\u548c\u6570\u636e\u5e93\u5efa\u7acb\u8fde\u63a5
2.\u6267\u884csql\u8bed\u53e5,\u63a5\u6536\u8fd4\u56de\u503c
3.\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5
\u4f7f\u7528MySQLdb\u4e5f\u8981\u9075\u5faa\u4e0a\u9762\u7684\u51e0\u6b65.\u8ba9\u6211\u4eec\u4e00\u6b65\u6b65\u7684\u8fdb\u884c.<\/p>\n
<\/strong>import MySQLdb<\/p>\n
conn=MySQLdb.connect(host="localhost",user="root",passwd="sa",db="mytable")
\u63d0\u4f9b\u7684connect\u65b9\u6cd5\u7528\u6765\u548c\u6570\u636e\u5e93\u5efa\u7acb\u8fde\u63a5,\u63a5\u6536\u6570\u4e2a\u53c2\u6570,\u8fd4\u56de\u8fde\u63a5\u5bf9\u8c61.<\/p>\n
host:\u6570\u636e\u5e93\u4e3b\u673a\u540d.\u9ed8\u8ba4\u662f\u7528\u672c\u5730\u4e3b\u673a.
user:\u6570\u636e\u5e93\u767b\u9646\u540d.\u9ed8\u8ba4\u662f\u5f53\u524d\u7528\u6237.
passwd:\u6570\u636e\u5e93\u767b\u9646\u7684\u79d8\u5bc6.\u9ed8\u8ba4\u4e3a\u7a7a.
db:\u8981\u4f7f\u7528\u7684\u6570\u636e\u5e93\u540d.\u6ca1\u6709\u9ed8\u8ba4\u503c.
port:MySQL\u670d\u52a1\u4f7f\u7528\u7684TCP\u7aef\u53e3.\u9ed8\u8ba4\u662f3306.
\u66f4\u591a\u5173\u4e8e\u53c2\u6570\u7684\u4fe1\u606f\u53ef\u4ee5\u67e5\u8fd9\u91cc
http:\/\/mysql-python.sourceforge.net\/MySQLdb.html<\/a><\/p>\n
commit() \u63d0\u4ea4
rollback() \u56de\u6eda<\/p>\n
cursor=conn.cursor()
n=cursor.execute(sql,param)
\u9996\u5148,\u6211\u4eec\u7528\u4f7f\u7528\u8fde\u63a5\u5bf9\u8c61\u83b7\u5f97\u4e00\u4e2acursor\u5bf9\u8c61,\u63a5\u4e0b\u6765,\u6211\u4eec\u4f1a\u4f7f\u7528cursor\u63d0\u4f9b\u7684\u65b9\u6cd5\u6765\u8fdb\u884c\u5de5\u4f5c.\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec\u4e24\u5927\u7c7b:1.\u6267\u884c\u547d\u4ee4,2.\u63a5\u6536\u8fd4\u56de\u503c<\/p>\n
callproc(self, procname, args):\u7528\u6765\u6267\u884c\u5b58\u50a8\u8fc7\u7a0b,\u63a5\u6536\u7684\u53c2\u6570\u4e3a\u5b58\u50a8\u8fc7\u7a0b\u540d\u548c\u53c2\u6570\u5217\u8868,\u8fd4\u56de\u503c\u4e3a\u53d7\u5f71\u54cd\u7684\u884c\u6570
execute(self, query, args):\u6267\u884c\u5355\u6761sql\u8bed\u53e5,\u63a5\u6536\u7684\u53c2\u6570\u4e3asql\u8bed\u53e5\u672c\u8eab\u548c\u4f7f\u7528\u7684\u53c2\u6570\u5217\u8868,\u8fd4\u56de\u503c\u4e3a\u53d7\u5f71\u54cd\u7684\u884c\u6570
executemany(self, query, args):\u6267\u884c\u5355\u6311sql\u8bed\u53e5,\u4f46\u662f\u91cd\u590d\u6267\u884c\u53c2\u6570\u5217\u8868\u91cc\u7684\u53c2\u6570,\u8fd4\u56de\u503c\u4e3a\u53d7\u5f71\u54cd\u7684\u884c\u6570
nextset(self):\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u7ed3\u679c\u96c6<\/p>\n
fetchall(self):\u63a5\u6536\u5168\u90e8\u7684\u8fd4\u56de\u7ed3\u679c\u884c.
fetchmany(self, size=None):\u63a5\u6536size\u6761\u8fd4\u56de\u7ed3\u679c\u884c.\u5982\u679csize\u7684\u503c\u5927\u4e8e\u8fd4\u56de\u7684\u7ed3\u679c\u884c\u7684\u6570\u91cf,\u5219\u4f1a\u8fd4\u56decursor.arraysize\u6761\u6570\u636e.
fetchone(self):\u8fd4\u56de\u4e00\u6761\u7ed3\u679c\u884c.
scroll(self, value, mode=’relative’):\u79fb\u52a8\u6307\u9488\u5230\u67d0\u4e00\u884c.\u5982\u679cmode=’relative’,\u5219\u8868\u793a\u4ece\u5f53\u524d\u6240\u5728\u884c\u79fb\u52a8value\u6761,\u5982\u679c mode=’absolute’,\u5219\u8868\u793a\u4ece\u7ed3\u679c\u96c6\u7684\u7b2c\u4e00\u884c\u79fb\u52a8value\u6761.<\/p>\n
#\u4f7f\u7528sql\u8bed\u53e5,\u8fd9\u91cc\u8981\u63a5\u6536\u7684\u53c2\u6570\u90fd\u7528%s\u5360\u4f4d\u7b26.\u8981\u6ce8\u610f\u7684\u662f,\u65e0\u8bba\u4f60\u8981\u63d2\u5165\u7684\u6570\u636e\u662f\u4ec0\u4e48\u7c7b\u578b,\u5360\u4f4d\u7b26\u6c38\u8fdc\u90fd\u8981\u7528%s
sql="insert into cdinfo values(%s,%s,%s,%s,%s)"
#param\u5e94\u8be5\u4e3atuple\u6216\u8005list
param=(title,singer,imgurl,url,alpha)
#\u6267\u884c,\u5982\u679c\u6210\u529f,n\u7684\u503c\u4e3a1
n=cursor.execute(sql,param)<\/p>\n
cursor.execute("select * from cdinfo")
#\u6211\u4eec\u4f7f\u7528\u4e86fetchall\u8fd9\u4e2a\u65b9\u6cd5.\u8fd9\u6837,cds\u91cc\u4fdd\u5b58\u7684\u5c06\u4f1a\u662f\u67e5\u8be2\u8fd4\u56de\u7684\u5168\u90e8\u7ed3\u679c.\u6bcf\u6761\u7ed3\u679c\u90fd\u662f\u4e00\u4e2atuple\u7c7b\u578b\u7684\u6570\u636e,\u8fd9\u4e9btuple\u7ec4\u6210\u4e86\u4e00\u4e2atuple
cds=cursor.fetchall()
#\u56e0\u4e3a\u662ftuple,\u6240\u4ee5\u53ef\u4ee5\u8fd9\u6837\u4f7f\u7528\u7ed3\u679c\u96c6
print cds[0][3]
#\u6216\u8005\u76f4\u63a5\u663e\u793a\u51fa\u6765,\u770b\u770b\u7ed3\u679c\u96c6\u7684\u771f\u5b9e\u6837\u5b50
print cds<\/p>\n
sql="insert into cdinfo values(0,%s,%s,%s,%s,%s)"
#\u6bcf\u4e2a\u503c\u7684\u96c6\u5408\u4e3a\u4e00\u4e2atuple,\u6574\u4e2a\u53c2\u6570\u96c6\u7ec4\u6210\u4e00\u4e2atuple,\u6216\u8005list
param=((title,singer,imgurl,url,alpha),(title2,singer2,imgurl2,url2,alpha2))
#\u4f7f\u7528executemany\u65b9\u6cd5\u6765\u6279\u91cf\u7684\u63d2\u5165\u6570\u636e.\u8fd9\u771f\u662f\u4e00\u4e2a\u5f88\u9177\u7684\u65b9\u6cd5!
n=cursor.executemany(sql,param)<\/p>\n
\u9700\u8981\u5206\u522b\u7684\u5173\u95ed\u6307\u9488\u5bf9\u8c61\u548c\u8fde\u63a5\u5bf9\u8c61.\u4ed6\u4eec\u6709\u540d\u5b57\u76f8\u540c\u7684\u65b9\u6cd5
cursor.close()
conn.close()<\/p>\n
MySQLdb\u7528\u6237\u6307\u5357: http:\/\/mysql-python.sourceforge.net\/MySQLdb.html<\/a>
MySQLdb\u6587\u6863: http:\/\/mysql-python.sourceforge.net\/MySQLdb-1.2.2\/public\/MySQLdb-module.html<\/a><\/p>\n