Python MySQLdb ²éѯ·µ»Ø×Öµä½á¹¹
Python MySQLdb ²éѯ·µ»Ø×Öµä½á¹¹ smallfish
MySQLdbĬÈϲéѯ½á¹û¶¼ÊÇ·µ»Øtuple£¬Êä³öʱºò²»ÊǺܷ½±ã£¬±ØÐë°´ÕÕ0£¬1ÕâÑù¶ÁÈ¡£¬ÎÞÒâÖÐÔÚÍøÉÏÕÒµ½¼òµ¥µÄÐ޸ķ½·¨£¬¾ÍÊÇ´«µÝÒ»¸öcursors.DictCursor¾ÍÐС£
ĬÈϳÌÐò£º
import MySQLdb
db = MySQLdb.connect(host = 'localhost', user = 'root', passwd = '123456', db = 'test')
cursor = db.cursor()
cursor.execute('select * from user')
rs = cursor.fetchall()
print rs
# ·µ»ØÀàËÆÈçÏÂ
# ((1000L, 0L), (2000L, 0L), (3000L, 0L))
Ð޸ĺó£º
import MySQLdb
import MySQLdb.cursors
db = MySQLdb.connect(host = 'localhost', user = 'root', passwd = '123456', db = 'test',
cursorclass = MySQLdb.cursors.DictCursor)
cursor = db.cursor()
cursor.execute('select * from user')
rs = cursor.fetchall()
print rs
# ·µ»ØÀàËÆÈçÏÂ
# ({'age': 0L, 'num': 1000L}, {'age': 0L, 'num': 2000L}, {'age': 0L, 'num': 3000L})
»òÕßÒ²¿ÉÒÔÓÃÏÂÃæÌæ»»connectºÍcursor²¿·Ö
db = MySQLdb.connect(host = 'localhost', user = 'root', passwd = '123456', db = 'test')
cursor = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
Ïà¹ØÎĵµ£º
½ñÌìÓöµ½ÁËÂé·³£º
ÓÃEclipse±àÒëpythonÏòsqliteÊý¾Ý¿â²åÈëÊý¾Ý×ÜÊÇÌáʾ±àÂëµÄÎÊÌ⣬ÎÒÉèÖÃÁËEclipseµÄworkspaceÓÃutf-8±àÂ뻹ÊDz»ÐÐ
µ«ÊÇÓÃEclipseдµÄ³ÌÐò±£´æÔÙÓÃIDLE´ò¿ªÈ´ÄܱàÒ룬Ææ¹ÖÁË
ÎÒ¿¼Âǵ½¿ÉÄÜÊÇÅäÖõÄÔÒò£¬ÕÒÕÒ±éÁËËùÓеÄÅäÖ㬶¼Ã»ÓÐÎÊÌ⣬°üÀ¨¹¤³ÌÎļþ
×îºóÎÒÓÃxvi32´ò¿ ......
µ±Ö´ÐÐimport
moduleʱ£¬½âÊÍÆ÷»á¸ù¾ÝÏÂÃæµÄËÑË÷·¾¶£¬ËÑË÷module1.pyÎļþ¡£
1) µ±Ç°¹¤×÷Ŀ¼
2) PYTHONPATHÖеÄĿ¼
3) Python°²×°Ä¿Â¼
(/usr/local/lib/python)
ÊÂʵÉÏ£¬Ä£¿éËÑË÷ÊÇÔÚ±£´æÔÚsys.pathÕâ¸öÈ«¾Ö±äÁ¿ÖеÄĿ¼ÁбíÖнøÐÐËÑË÷¡£
sys.path»áÔÚ½âÊÍÆ÷¿ªÊ¼Ö´ÐÐʱ±»³õʼ»¯³É°üº¬£º
1)µ±Ç°¹¤×÷Ŀ¼
2) PYT ......
python¶Ô¶à¹úÓïÑԵĴ¦ÀíÊÇÖ§³ÖµÄºÜºÃµÄ£¬Ëü¿ÉÒÔ´¦ÀíÏÖÔÚÈÎÒâ±àÂëµÄ×Ö·û£¬ÕâÀïÉîÈëµÄÑо¿Ò»ÏÂpython¶Ô¶àÖÖ²»Í¬ÓïÑԵĴ¦Àí¡£
ÓÐÒ»µãÐèÒªÇå³þµÄÊÇ£¬µ±pythonÒª×ö±àÂëת»»µÄʱºò£¬»á½èÖúÓÚÄÚ²¿µÄ±àÂ룬ת»»¹ý³ÌÊÇÕâÑùµÄ£º
ÔÓбàÂë -> ÄÚ²¿±àÂë ->
Ä¿ ......
ºÜ³¤µÄÒ»¶Î´úÂ룬µ«ºÜÇå³þ¡£¹þ¹þ¡£
import os
from time import strftime
stamp=strftime("%Y-%m-%d %H:%M:%S")
logfile = 'F:\\test\\m-php-framework\\tmp\logs\\error_report.log'
path = 'F:\\test\\'
files = os.listdir(path)
bytes = 0
numfiles = 0
for f in files:
if f.startswith('t'): ......