RDB/MySQL

mysql용 python 코드 예제

세모데 2017. 9. 7. 11:41

1. 설치

 

    yum install mysqldb



2. fetchone


   import MySQLdb

   mydb = MySQLdb.connect(host = 'localhost', user = 'test', passwd = 'test', db = 'test')

   cur = mydb.cursor()

   statement = "SELECT * FROM test1 WHERE name='a1'"

   cur.execute(statement)

   result = cur.fetchone()

   print result



3. fetchmany

   import MySQLdb

   mydb = MySQLdb.connect(host = 'localhost', user = 'test', passwd = 'test', db = 'test')

   cur = mydb.cursor()

   statement = "SELECT * FROM test1 WHERE name='a1'"

   cur.execute(statement)

   results = cur.fetchmany(10)

   for result in results:

      print result


   cur.execute("SHOW TABLES")

   time.sleep(20)

   print cur.fetchmany(10)

   time.sleep(20)

   print cur.fetchmany(10)