2017-11-21 1 views
1

다음 Python Mysql 연결을 테스트하는 간단한 코드가 있습니다. 나는이 코드를 실행하면PyMySQL 커서 유형 오류

connection = pymysql.connect(host = "localhost", 
        user = "root", 
        passwd = "", 
        db = "bugs", 
        charset = "utf8mb4", 
        cursorclass = "pymysql.cursors.Dictcursor") 

try: 
    with connection.cursor() as cur: 
     cur.execute('''SELECT COUNT(*) FROM tbl_firefox''') 

     for row in cur.fetchall(): 
      print("Number of rows in firefox table:", row) 
    connection.commit() 
finally: 
    connection.close() 

는 내가 다른 파이썬 MySQL의 코드에서 유사한 오류를 볼 수있다

File "test2.py", line 31, in <module> 
    with connection.cursor() as cur: 
    File "C:\Program Files\Anaconda3\lib\site-packages\pymysql\connections.py", line 833, in cursor 
    return self.cursorclass(self) 
TypeError: 'str' object is not callable 

유형의 오류를 얻을. 그러나 여전히 운이 없다. 누군가 나에게 버그를 지적 할 수 있다면, 많이 감사하겠습니다.

답변

0

이리저리 PyMySQL 인용 :

import pymysql.cursors 

# Connect to the database 
connection = pymysql.connect(host='localhost', 
          user='user', 
          password='passwd', 
          db='db', 
          charset='utf8mb4', 
          cursorclass=pymysql.cursors.DictCursor) 

try: 
    with connection.cursor() as cursor: 
     # Create a new record 
     sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)" 
     cursor.execute(sql, ('[email protected]', 'very-secret')) 

    # connection is not autocommit by default. So you must commit to save 
    # your changes. 
    connection.commit() 

    with connection.cursor() as cursor: 
     # Read a single record 
     sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s" 
     cursor.execute(sql, ('[email protected]',)) 
     result = cursor.fetchone() 
     print(result) 
finally: 
    connection.close() 

그것은 나의 끝에서

+0

신인 실수 따옴표로 둘러싸여되지 passwd 및 cursorclass 이외의 암호를해야한다. 고마워. – akalanka