MySQLdb에 화를 냈습니다! 나는 정확히 공식 문서를 다음과 같은거야하지만 난 코드에 오류가 있습니다 MySQLdb : 큰 따옴표가 쿼리에 전달되었습니다.
Traceback (most recent call last):
File "dbscript.py", line 49, in <module>
cursor.execute("SELECT MAX(id) AS maxid FROM %s" , (DB_TABLE,))
File "build/bdist.macosx-10.7-intel/egg/MySQLdb/cursors.py", line 174, in execute
File "build/bdist.macosx-10.7-intel/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''test'' at line 1")
는 물론 일부 인용에 문제가있는 것입니다 :
DB_TABLE = "test"
cursor.execute("SELECT MAX(id) AS maxid FROM " + DB_TABLE)
print "***"
cursor.execute("SELECT MAX(id) AS maxid FROM %s" , (DB_TABLE,))
가 나는 오류가 발생합니다. 내 명령은 다음과 같은 라인을 MySQL에 보냈다.
SELECT MAX(id) AS maxid FROM ''test''
어떻게하면 큰 따옴표를 하나씩 바꿀 수 있습니까?
I'v은 다음과 시도 :.
DB_TABLE = 'test'
cursor.execute("SELECT MAX(id) AS maxid FROM %s" , [DB_TABLE])
cursor.execute("SELECT MAX(id) AS maxid FROM %s" , ("test",))
을하지만 아무것도 :(
전체 SQL 문자열을 작성하고 실행하기 전에 인쇄하십시오. 또한 - 실제로'test'입니까, 아니면 테이블 이름을 숨기려면 그것을 변경 했습니까? 테이블 이름이'desc'와 같은 경우 예약 된 키워드이며 오류가 발생합니다. – Mikhail
'test'가 실제 테이블이고 연결 문자열 옆에있는 좋은 방법이 있습니다 (첫 번째 줄 참조) – user777466
@ user777466 연결은 다음과 같습니다. 이 경우에는 괜찮습니다. –