我想使用这个 python 命令将 mysql 数据库导出到 .csv 文件中。
cursor.execute("""SELECT * INTO OUTFILE 'NullServids.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\\n' FROM accounts WHERE srvid is NULL""")
但它返回以下语法错误。
_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 '\\n' FROM accounts WHERE srvid is NULL' at line 1")
所以我尝试不转义换行符,当我将它复制并粘贴到 MySQL 时,这显然有效。
cursor.execute("""SELECT * INTO OUTFILE 'NullServids.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n' FROM accounts WHERE srvid is NULL""")
但我得到了这个错误:
_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 '' FROM accounts WHERE srvid is NULL' at line 2")
所以我的问题是,转义换行符以便在cursor.execute()
调用中正确解释的正确方法是什么?我用谷歌搜索了这个,找不到答案,这让我很尴尬。