Posted
Filed under 프로그래밍/Python
PHP로 개발하다가 python 할라니 참 불편해...
pymysql 이 처리해줘야할것 같은걸 개발자가 다 해줘야해?

PyMySQL
https://github.com/PyMySQL/PyMySQL

샘플
https://github.com/PyMySQL/PyMySQL/blob/master/example.py



# dict 형태로 row를 반환
cursor = db.cursor(pymysql.cursors.DictCursor)
cursor.execute("SELECT id, name FROM `table`")
rows=cursor.fetchall()


cursor.execute("SELECT id, name FROM `table`")
for i in xrange(cursor.rowcount):
    id, name = cursor.fetchone()
    print id, name



cursor.execute("SELECT id, name FROM `table`")
result = cursor.fetchmany()
while result:
    for id, name in result:
        print id, name
    result = cursor.fetchmany()



cursor.execute("SELECT id, name FROM `table`")
for id, name in cursor.fetchall():
    print id, name



import MySQLdb

conn = MySQLdb.connect(user="user", passwd="password", db="mydb")
cur = conn.cursor()
print "Executing query"
cur.execute("SELECT * FROM bigtable");


print "Starting loop"
row = cur.fetchone()
while row is not None:
    print ", ".join([str(c) for c in row])
    row = cur.fetchone()


cur.close()
conn.close()
2014/09/23 10:21 2014/09/23 10:21