python连接MySQL数据库实例分析 python连接MySQL数据库问题? cursor( ) ...

python\u600e\u4e48\u8fde\u63a5mysql\u6570\u636e\u5e93

1\u3001\u548c\u6570\u636e\u5e93\u5efa\u7acb\u8fde\u63a5
2\u3001\u6267\u884csql\u8bed\u53e5,\u63a5\u6536\u8fd4\u56de\u503c
3\u3001\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5

1\u3001MySQL\u6570\u636e\u5e93\u8981\u7528MySQLdb\u6a21\u5757,\u4f46Python\u7528\u6765\u94fe\u63a5MySQL\u7684\u7b2c\u4e09\u65b9\u5e93MySQLdb\u4e0d\u652f\u6301Python3.x
\u7279\u522b\u8bf4\u660e\uff1a\u6211\u5728\u6211\u7684\u7535\u8111\u4e0a\u5b9e\u9a8c\u65f6\uff0c\u6211\u7684python\u662f2.7.2\u7248\u672c\uff0c\u5b89\u88c5\u5bf9\u5e94\u7248\u672c\u7684MySQLdb\u4e4b\u540e\u76f4\u63a5\u53ef\u4ee5\u8fd0\u884c\uff0c\u5e76\u4e0e\u6570\u636e\u5e93\u8fde\u63a5\u6210\u529f\uff0c\u6240\u4ee5\u5982\u679c\u5927\u5bb6\u4e5f\u50cf\u6211\u4e00\u6837\u987a\u5229\u7684\u8bdd\uff0c\u4e0b\u9762\u7684\u5c31\u4e0d\u9700\u8981\u770b\u4e86\uff0c\u76f4\u63a5\u8df3\u8fc7\uff0c\u770b\u7b2c2\u70b9\u5982\u4f55\u6267\u884csql\u8bed\u53e5\u5373\u53ef\uff01\u5982\u679c\u5b89\u88c5\u4e4b\u540e\u51fa\u73b0\u5f02\u5e38\uff0c\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u4e0b\u9762\u7684\u89e3\u51b3\u529e\u6cd5\u3002

\u8fde\u63a5\u7684\u5173\u952e\u662f\u5b89\u88c5MySQLdb\u6a21\u5757\u8981\u4e0b\u8f7d\u4e0ePython\u76f8\u5bf9\u5e94\u7684\u7248\u672c\uff1a
\u4e0b\u8f7d\u597d\u540e\u5b89\u88c5\uff0c\u5b83\u4f1a\u81ea\u52a8\u68c0\u6d4b\u5230\u8ba1\u7b97\u673aPython\u7684\u5b89\u88c5\u8def\u5f84\uff0c\u5e76\u81ea\u52a8\u586b\u5199\u6a21\u5757\u89e3\u538b\u8def\u5f84\uff08\u6211\u7684\u662f\uff1aD:\ProgramFiles\ActivePython 2.6.6.17\Lib\site-packages\\uff09\u3002
\u4f46\u89e3\u538b\u5b8c\u6210\u540e\u5e76\u4e0d\u80fd\u4f7f\u7528\uff0c\u8fd8\u8981\u4fee\u6539MySQLdb\u6a21\u5757\u4e0b\u7684\u4e00\u4e9b\u6587\u4ef6\uff1a
\u2460.\u5728MySQLdb\u76ee\u5f55\u4e0b\uff08\u6211\u7684\u662f\uff1aD:\ProgramFiles\ActivePython 2.6.6.17\Lib\site-packages\MySQLdb\uff09\u627e\u5230__init__.py\uff1a
\u6ce8\u91ca\u7b2c34\u300135\u884c\u7684from setsimport ImmutableSet\u3001class DBAPISet(ImmutableSet):\uff0c\u5728\u540e\u9762\u6dfb\u52a0class DBAPISet(frozenset):

# from sets import ImmutableSet
# class DBAPISet(ImmutableSet):
class DBAPISet(frozenset):

\u2461.\u6253\u5f00converters.py\uff1a
\u6ce8\u91ca\u7b2c37\u884c\u7684from sets import BaseSet, Set\uff0c\u5c06\u7b2c45\u884c\u7684return Set([ i for i in s.split(',') ifi ])\u4e2d\u7684Set\u6539\u4e3aset\uff1b\u540c\u6837\u5c06\u7b2c129\u884c\u7684Set: Set2Str,\u4e2d\u7684Set\u6539\u4e3aset\uff08\u4e0d\u8981\u4fee\u6539Set2Str\uff09\uff0c\u5230\u8fd9\u91cc\u5c31\u4fee\u6539\u5b8c\u6bd5\u4e86
2.\u5efa\u7acb\u6570\u636e\u5e93\u8fde\u63a5
import MySQLdb
conn=MySQLdb.connect(host="localhost",user="root",passwd="sa",db="mytable")
\u3000
\u6bd4\u8f83\u5e38\u7528\u7684\u53c2\u6570\u5305\u62ec
host: \u8fde\u63a5\u7684\u6570\u636e\u5e93\u670d\u52a1\u5668\u4e3b\u673a\u540d\uff0c\u9ed8\u8ba4\u4e3a\u672c\u5730\u4e3b\u673a(localhost)\u3002
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.
conn\u8fde\u63a5\u6709\u4e24\u4e2a\u91cd\u8981\u7684\u65b9\u6cd5commit\u3010\u63d0\u4ea4\u65b0\u589e\u548c\u4fee\u6539\u3011,rollback\u3010\u64a4\u9500\u65b0\u589e\u6216\u4fee\u6539\u3011
3\u3001\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c
n=cursor.execute(sql,param)
\u6211\u4eec\u8981\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
cursor\u7528\u6765\u6267\u884c\u547d\u4ee4\u7684\u65b9\u6cd5:
\u3000 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
\u3000 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
\u3000 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
\u3000 nextset(self):\u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u7ed3\u679c\u96c6
\u3000 cursor\u7528\u6765\u63a5\u6536\u8fd4\u56de\u503c\u7684\u65b9\u6cd5:
\u3000 fetchall(self):\u63a5\u6536\u5168\u90e8\u7684\u8fd4\u56de\u7ed3\u679c\u884c.
\u3000 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.
\u3000 fetchone(self):\u8fd4\u56de\u4e00\u6761\u7ed3\u679c\u884c.
\u3000 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\u679cmode='absolute',\u5219\u8868\u793a\u4ece\u7ed3\u679c\u96c6\u7684\u7b2c\u4e00\u884c\u79fb\u52a8value\u6761.
\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50.
#\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)
#\u518d\u6765\u6267\u884c\u4e00\u4e2a\u67e5\u8be2\u7684\u64cd\u4f5c
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
#\u5982\u679c\u9700\u8981\u6279\u91cf\u7684\u63d2\u5165\u6570\u636e,\u5c31\u8fd9\u6837\u505a
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)
\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.
4\u3001\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5
\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()
5\u3001
5 \u7f16\u7801\uff08\u9632\u6b62\u4e71\u7801\uff09
\u9700\u8981\u6ce8\u610f\u7684\u70b9\uff1a
1 Python\u6587\u4ef6\u8bbe\u7f6e\u7f16\u7801 utf-8 \uff08\u6587\u4ef6\u524d\u9762\u52a0\u4e0a #encoding=utf-8)
2 MySQL\u6570\u636e\u5e93charset=utf-8
3 Python\u8fde\u63a5MySQL\u662f\u52a0\u4e0a\u53c2\u6570 charset=utf8
4 \u8bbe\u7f6ePython\u7684\u9ed8\u8ba4\u7f16\u7801\u4e3a utf-8 (sys.setdefaultencoding(utf-8)
#encoding=utf-8
import sys
import MySQLdb

reload(sys)
sys.setdefaultencoding('utf-8')

db=MySQLdb.connect(user='root',charset='utf8')
\u6ce8\uff1aMySQL\u7684\u914d\u7f6e\u6587\u4ef6\u8bbe\u7f6e\u4e5f\u5fc5\u987b\u914d\u7f6e\u6210utf8

6.\u6a21\u5757\u529f\u80fd\u6f14\u793a
#!/usr/bin/python
import MySQLdb
Con= MySQLdb.connect(host='localhost',user='root',passwd='root',db='abc')
cursor =con.cursor()
sql ="select * from myt"
cursor.execute(sql)
row=cursor.fetchone()
print row
cursor.close()
con.close()

\u6267\u884c\u4ee5\u4e0bSQL\u8bed\u53e5\u83b7\u53d6\u8fd4\u56de\u503c\uff1a
//\u83b7\u53d6\u8fde\u63a5\u7684\u6e38\u6807
cursor=conn.cursor()
//\u67e5\u8be2
sql = "select * from \u3010table\u3011"
//\u65b0\u589e
sql = "insert into \u3010table\u3011(\u5b57\u6bb5,\u5b57\u6bb5) values(\u503c\uff0c\u503c)"
//\u4fee\u6539
sql = "update \u3010table\u3011 set \u5b57\u6bb5 =\u2018\u503c\u2019where \u6761\u4ef6 "
//\u5220\u9664
sql = "delete from \u3010table\u3011where \u6761\u4ef6"
cursor.execute(sql)
\u8fd4\u56de\u503c
cur.execute('select * from tables')
\u5176\u8fd4\u56de\u503c\u4e3aSQL\u8bed\u53e5\u5f97\u5230\u7684\u884c\u6570\uff0c\u5982\uff1a2L\uff0c\u8868\u793a2\u884c\u3002
\u7136\u540e\uff0c\u53ef\u4ee5\u4ece\u8be5\u5bf9\u8c61\u7684fetchone\u6216fetchall\u65b9\u6cd5\u5f97\u5230\u884c\u4fe1\u606f\u3002
\u83b7\u53d6\u884c\u4fe1\u606f
\u6307\u9488\u5bf9\u8c61\u7684fetchone()\u65b9\u6cd5\uff0c\u662f\u6bcf\u6b21\u5f97\u5230\u4e00\u884c\u7684tuple\u8fd4\u56de\u503c\uff1a
\u5f15\u7528
>>> row=cur.fetchone()
>>> print row
('user1', '52c69e3a57331081823331c4e69d3f2e', 1000L, 1000L, '/home/FTP/user1','')
\u6307\u9488\u5bf9\u8c61\u7684fetchall()\u65b9\u6cd5\uff0c\u53ef\u53d6\u51fa\u6307\u9488\u7ed3\u679c\u96c6\u4e2d\u7684\u6240\u6709\u884c\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u96c6\u4e00\u4e2a\u5143\u7ec4(tuples)\uff1a
\u5f15\u7528
>>> cur.scroll(0,'absolute')
>>> row=cur.fetchall()
>>> print row
(('user1', '52c69e3a57331081823331c4e69d3f2e', 1000L, 1000L, '/home/FTP/user1',''), ('user2', '7e58d63b60197ceb55a1c487989a3720', 1000L, 1000L,'/home/FTP/user2', None))
\u79fb\u52a8\u6307\u9488
\u5f53\u4f7f\u7528fetchone()\u65b9\u6cd5\u662f\uff0c\u6307\u9488\u662f\u4f1a\u53d1\u751f\u79fb\u52a8\u7684\u3002\u6240\u4ee5\uff0c\u82e5\u4e0d\u91cd\u7f6e\u6307\u9488\uff0c\u90a3\u4e48\u4f7f\u7528fetchall\u7684\u4fe1\u606f\u5c06\u53ea\u4f1a\u5305\u542b\u6307\u9488\u540e\u9762\u7684\u884c\u5185\u5bb9\u3002
\u624b\u52a8\u79fb\u52a8\u6307\u9488\u4f7f\u7528\uff1a
cur.scroll(int,parm)
\u542b\u4e49\u4e3a\uff1a
\u5f15\u7528
int\uff1a\u79fb\u52a8\u7684\u884c\u6570\uff0c\u6574\u6570\uff1b\u5728\u76f8\u5bf9\u6a21\u5f0f\u4e0b\uff0c\u6b63\u6570\u5411\u4e0b\u79fb\u52a8\uff0c\u8d1f\u503c\u8868\u793a\u5411\u4e0a\u79fb\u52a8\u3002
parm\uff1a\u79fb\u52a8\u7684\u6a21\u5f0f\uff0c\u9ed8\u8ba4\u662frelative\uff0c\u76f8\u5bf9\u6a21\u5f0f\uff1b\u53ef\u63a5\u53d7absoulte\uff0c\u7edd\u5bf9\u6a21\u5f0f\u3002
\u4fee\u6539\u6570\u636e
\u4fee\u6539\u6570\u636e\uff0c\u5305\u62ec\u63d2\u5165\u3001\u66f4\u65b0\u3001\u5220\u9664\u3002\u5b83\u4eec\u90fd\u662f\u4f7f\u7528\u6307\u9488\u5bf9\u8c61\u7684execute()\u65b9\u6cd5\u6267\u884c\uff1a
cur.execute("insert into table (row1, row2) values ('111', '222')")
cur.execute("update table set row1 = 'test' where row2 = 'row2' ")
cur.execute("delete from table where row1 = 'row1' ")
\u56e0\u5355\u5f15\u53f7\u201c'\u201d\u7528\u4e8eSQL\u8bed\u53e5\u4e2d\u7684\u6807\u8bc6\uff0c\u6240\u4ee5\uff0cpython\u4e2d\u7684\u5b57\u7b26\u4e32\u9700\u4f7f\u7528\u53cc\u5f15\u53f7\u62ec\u4f4f\u3002
\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528python\u7684\u201c\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u201d\u5199\u6cd5\uff0c\u7b80\u5316\u547d\u4ee4\uff0c\u4f8b\u5982\uff1a
cur.execute("update table set row1 = '%s' where row2 = '%s' "%('value1','value2'))
\u203b\u8bf7\u6ce8\u610f\uff0c'%s'\u7684\u5355\u5f15\u53f7\u662fSQL\u8bed\u53e5\u7684\u95f4\u9694\u7b26\uff0c'value1'\u7684\u5355\u5f15\u53f7\u662fpython\u7684\u5b57\u7b26\u4e32\u95f4\u9694\u7b26\uff0c\u5176\u542b\u4e49\u662f\u4e0d\u540c\u7684\u3002\u662f\u5426\u9700\u8981\u95f4\u9694\u7b26\uff0c\u4ee5\u53ca\u4f7f\u7528\u53cc\u5f15\u53f7\u8fd8\u662f\u5355\u5f15\u53f7\u4f5c\u4e3a\u95f4\u9694\uff0c\u9700\u6839\u636e\u5176\u542b\u4e49\u51b3\u5b9a\u3002\u4f8b\u5982\uff0c\u8fd8\u6709\uff1a
cur.execute("update FTPUSERS set passwd=%s where userid='%s' "%("md5('123')",'user2'))
\u8fd9\u91cc\uff0cpaswd=%s\u662f\u56e0SQL\u7684md5()\u51fd\u6570\u662f\u4e0d\u9700\u8981\u5355\u5f15\u53f7\u95f4\u9694\u7684\uff1b"md5('123')"\u662fpython\u7684\u5b57\u7b26\u4e32\u4e2d\u542b\u6709\u5355\u5f15\u53f7\uff0c\u6240\u4ee5\u7528\u53cc\u5f15\u53f7\u62ec\u4f4f\u3002
\u63d0\u4ea4\u4fee\u6539
\u4e00\u822c\u60c5\u51b5\u4e0b\uff0cMySQLdb\u6a21\u5757\u4f1a\u81ea\u52a8\u63d0\u4ea4\u4fee\u6539\u3002\u4f46\u6211\u4eec\u5728\u66f4\u65b0\u6570\u636e\u540e\uff0c\u624b\u52a8\u8fd0\u884c\u4e00\u6b21\uff1a
conn.commit()
\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5
\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()

MySQLdb.connect\u662fpython \u8fde\u63a5MySQL\u6570\u636e\u5e93\u7684\u65b9\u6cd5\uff0c\u5728Python\u4e2d import MySQLdb\u5373\u53ef\u4f7f\u7528\uff0c\u81f3\u4e8econnect\u4e2d\u7684\u53c2\u6570\u5f88\u7b80\u5355\uff1a
host\uff1aMySQL\u670d\u52a1\u5668\u540d
user\uff1a\u6570\u636e\u5e93\u4f7f\u7528\u8005
password\uff1a\u7528\u6237\u767b\u5f55\u5bc6\u7801
db\uff1a\u64cd\u4f5c\u7684\u6570\u636e\u5e93\u540d
charset\uff1a\u4f7f\u7528\u7684\u5b57\u7b26\u96c6(\u4e00\u822c\u662fgb2312)
cursor = db.cursor() \u5176\u5b9e\u5c31\u662f\u7528\u6765\u83b7\u5f97python\u6267\u884cMysql\u547d\u4ee4\u7684\u65b9\u6cd5,\u4e5f\u5c31\u662f
\u6211\u4eec\u6240\u8bf4\u7684\u64cd\u4f5c\u6e38\u6807
\u4e0b\u9762cursor.execute\u5219\u662f\u771f\u6b63\u6267\u884cMySQL\u8bed\u53e5\uff0c\u5373\u67e5\u8be2TABLE_PARAMS\u8868\u7684\u6570\u636e\u3002
\u81f3\u4e8efetchall()\u5219\u662f\u63a5\u6536\u5168\u90e8\u7684\u8fd4\u56de\u7ed3\u679c\u884c row\u5c31\u662f\u5728python\u4e2d\u5b9a\u4e49\u7684\u4e00\u4e2a\u53d8\u91cf\uff0c\u7528\u6765\u63a5\u6536\u8fd4\u56de\u7ed3\u679c\u884c\u7684\u6bcf\u884c\u6570\u636e\u3002\u540c\u6837\u540e\u9762\u7684r\u4e5f\u662f\u4e00\u4e2a\u53d8\u91cf\uff0c\u7528\u6765\u63a5\u6536row\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5982\u679c\u5199\u6210C\u7684\u5f62\u5f0f\u5c31\u66f4\u597d\u7406\u89e3\u4e86
for(string row = ''; row<= cursor.fetchall(): row++)
for(char r = ''; r<= row; r++)
printf("%c", r);
\u5927\u81f4\u5c31\u662f\u8fd9\u4e48\u4e2a\u610f\u601d\uff01

python连接MySQL数据库实例分析
本文实例讲述了python连接MySQL数据库的方法。分享给大家供大家参考。具体实现方法如下:
import MySQLdb
conn = MySQLdb.connect(host="localhost",
user="root",
passwd="123456",
db="test")
cursor = conn.cursor()
cursor.execute("select * from hard")
res = cursor.fetchall()
for x in res:
print x
cursor.close()
conn.close()
运行结果如下:
希望本文所述对大家的python程序设计有所帮助。

  • Python濡備綍浣跨敤fetchone()鍜宖etchall()鏂规硶浠MySQL鏁版嵁搴撴煡璇㈡暟鎹...
    绛旓細鍦Python涓锛杩炴帴MySQL鏁版嵁搴撳苟鎵ц鏌ヨ鎿嶄綔鏄父瑙佺殑浠诲姟銆傛湁涓ょ鏂规硶鍙互鑾峰彇鏁版嵁锛歠etchone()鍜宖etchall()銆俧etchone()鐢ㄤ簬鑾峰彇鍗曟潯鏌ヨ缁撴灉锛岃宖etchall()鍒欑敤浜庤幏鍙栨墍鏈夌粨鏋溿俽owcount灞炴ф槸涓涓彧璇诲睘鎬э紝瀹冭繑鍥炴墽琛孲QL璇彞鍚庡奖鍝嶇殑琛屾暟锛岃繖瀵逛簬杩借釜鎿嶄綔褰卞搷鑼冨洿寰堟湁鐢ㄣ傝鎴戜滑閫氳繃涓涓疄渚嬫潵鐞嗚В杩欎簺姒...
  • Python杩炴帴mysql鏁版嵁搴撳強python浣跨敤mysqldb杩炴帴鏁版嵁搴撴暀绋
    绛旓細port,鎸囧畾鏁版嵁搴撴湇鍔″櫒鐨杩炴帴绔彛,榛樿鏄3306 澶у鍙兘浼氭敞鎰忓埌婧愮爜涓病鏈夌敤鍒扮鍙e彿,杩欐槸鍥犱负MySQLdb鐨刢onnect鍑芥暟鐨勮鍙傛暟鐨勯粯璁ゅ间究鏄3306,濡傛灉浣犲湪瀹夎mysql鐨勬椂鍊欎慨鏀逛簡鏁版嵁搴撶殑绔彛鍙,閭d箞浣犲氨闇瑕佸湪婧愮爜涓姞涓婅鍙傛暟鐨勪慨鏀瑰间簡銆 涓,瀹夎mysql 濡傛灉鏄痺indows 鐢ㄦ埛,mysql 鐨勫畨瑁呴潪甯哥畝鍗,鐩存帴涓嬭浇瀹夎鏂囦欢,鍙屽嚮...
  • 鐢Python杩炴帴MySQL鐨勫嚑绉嶆柟寮忚瑙python
    绛旓細1銆丮ySQL-pythonMySQL-python 鍙堝彨 MySQLdb锛屾槸 Python 杩炴帴 MySQL 鏈娴佽鐨勪竴涓┍鍔紝寰堝妗嗘灦閮戒篃鏄熀浜庢搴撹繘琛屽紑鍙戯紝閬楁喚鐨勬槸瀹冨彧鏀寔 Python2.x锛岃屼笖瀹夎鐨勬椂鍊欐湁寰堝鍓嶇疆鏉′欢锛屽洜涓哄畠鏄熀浜嶤寮鍙戠殑搴擄紝鍦 Windows 骞冲彴瀹夎闈炲父涓嶅弸濂斤紝缁忓父鍑虹幇澶辫触鐨勬儏鍐碉紝鐜板湪鍩烘湰涓嶆帹鑽愪娇鐢紝鍙栦唬鐨勬槸瀹冪殑琛嶇敓鐗...
  • Python鏁欑▼:[25]杩滅▼鎿嶄綔Mysql鏁版嵁搴
    绛旓細棣栧厛瑕佸紩鍏ユā鍧楋紝涓嬮潰杩欎釜妯″潡鏄繀椤昏寮曞叆鐨 鎺ョ潃锛屾垜瑕佸缓绔杩炴帴锛屾垜浠湅鍒板悇涓弬鏁板凡缁忓湪鍚庨潰瑙i噴锛屾垜瑕佽鐨勬槸锛屽ぇ閮ㄥ垎璇炬湰涓婂皢host閮界敤鐨刲ocalhost浣滀负鏈湴涓绘満鐨勫湴鍧锛岄偅涔堟垜浠湪鎿嶄綔杩滅▼鏁版嵁搴撶殑鏃跺欙紝鎴戜滑灏遍渶瑕佺敤鍒拌繙绋嬩富鏈虹殑Ip鍦板潃浜嗐傛垜浠氬父寤虹珛杩炴帴鐨勬椂鍊欙紝涓嶈鐩存帴浣跨敤connect鏂规硶锛岃屾槸浣跨敤try璇...
  • python鎬庝箞杩炴帴mysql鏁版嵁搴
    绛旓細鍦 Python 璇█鐜涓嬫垜浠繖鏍杩炴帴鏁版嵁搴撱侷n [1]: from mysql import connector In [2]: cnx = connector.connect(host="172.16.192.100",port=3306,user="appuser",password="xxxxxx")浣嗘槸杩炴帴鏁版嵁搴撶殑鑳屽悗鍙戠敓浜嗕粈涔堝憿锛熺瓟妗 褰撴垜浠氳繃椹卞姩绋嬪簭锛坢ysql-connector-python,pymysql锛杩炴帴 MySQL ...
  • python鎬庝箞杩炴帴mysql鏁版嵁搴
    绛旓細1 Python鏂囦欢璁剧疆缂栫爜 utf-8 (鏂囦欢鍓嶉潰鍔犱笂 #encoding=utf-8) 2 MySQL鏁版嵁搴揷harset=utf-8 3 Python杩炴帴MySQL鏄姞涓婂弬鏁 charset=utf8 4 璁剧疆Python鐨勯粯璁ょ紪鐮佷负 utf-8 (sys.setdefaultencoding(utf-8)#encoding=utf-8 import sys import MySQLdb reload(sys) sys.setdefaultencoding('utf-8') db=...
  • python3.4鎬庝箞杩炴帴mysql pymysql杩炴帴mysql鏁版嵁搴
    绛旓細锛1锛夊鍏ymysql: import pymysql 锛2锛杩炴帴鏁版嵁搴擄細conn=pymysql.connect(host='localhost',user='root',passwd='root',db='ere'锛宑harset='utf8')鍔″繀娉ㄦ剰鍚勭瓑鍙峰墠闈㈢殑鍐呭锛乧harset鍙傛暟鍙伩鍏嶄腑鏂囦贡鐮 锛3锛夎幏鍙栨搷浣滄父鏍囷細cur=conn.cursor()锛4锛夋墽琛宻ql璇彞,鎻掑叆璁板綍锛歴ta=cur.execute("...
  • python杩炴帴MySQL鏁版嵁搴撳疄渚嬪垎鏋
    绛旓細python杩炴帴MySQL鏁版嵁搴撳疄渚嬪垎鏋 鏈枃瀹炰緥璁茶堪浜唒ython杩炴帴MySQL鏁版嵁搴撶殑鏂规硶銆傚垎浜粰澶у渚涘ぇ瀹跺弬鑰冦傚叿浣撳疄鐜版柟娉曞涓嬶細import MySQLdb conn = MySQLdb.connect(host="localhost",user="root",passwd="123456",db="test")cursor = conn.cursor()cursor.execute("select * from hard")res = cursor.fetch...
  • windows涓python鎬庝箞閾炬帴鏁版嵁搴
    绛旓細涓锛屽畨瑁匨ySQL-python python 杩炴帴mysql鏁版嵁搴撻渶瑕 Python interface to Mysql鍖咃紝鍖呭悕涓 MySQL-python 锛孭yPI涓婄幇鍦ㄥ埌浜1.2.5鐗堟湰銆侻ySQL-python鍦╳indows涓嬫槸閫氳繃.exe鏂囦欢鐨刬nstaller瀹夎鐨勶紝鍓嶆彁鏄凡缁忓畨瑁呯殑python闇瑕佸啓鍏ユ敞鍐岃〃锛屽弬鑰冭繖绡囨枃绔狅細windows瀹夎python2.7鍚庣殑娉ㄥ唽(registry)闂銆傜劧鍚庡紑濮嬪畨瑁...
  • linux鐜涓python鎬庢牱鎿嶄綔mysql鏁版嵁搴
    绛旓細浣跨敤python鎿嶄綔MySQL 浣跨敤python杩炴帴MySQL锛屽垱寤鏁版嵁搴擄紝鍒涘缓琛ㄦ牸锛屾彃鍏/鏌ヨ鏁版嵁銆俻ython_mysql.py浠g爜濡備笅锛氭晥鏋滃鍥撅細鑻ュ嚭鐜扮被浼间簬姝ょ殑璀﹀憡锛/usr/local/lib/python2.6/dist-packages/MySQL_python-1.2.2-py2.6-linux-i686.egg/MySQLdb/__init__.py:34: DeprecationWarning: the sets module is ...
  • 扩展阅读:学python后到底能干什么 ... python连接mysql菜鸟 ... c++和python先学哪个 ... python编程经典例题 ... mysql连接不上navicat ... 学python有前途吗 ... python初学编程必背 ... sql和python哪个难学 ... python编程入门自学 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网