大佬教程收集整理的这篇文章主要介绍了LOAD DATA LOCAL INFILE上的Python2.7 MySQL Connector错误,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
通过在连接中添加适当的客户端标志,可以轻松解决此问题,如下所示:
import Mysql.connector
from Mysql.connector.constants import ClIEntFlag
cnx = Mysql.connector.connect(user='[username]', password='[pass]', host='[host]', clIEnt_flags=[ClIEntFlag.LOCAL_fileS])
cursor = cnx.cursor()
@H_675_6@
这将授予MysqL访问您计算机上本地文件的权限,然后以下LOAD将起作用:
Loadsql = """LOAD DATA LOCAL INfile '%s'
INTO table %s
FIELDS TERMINATED BY '\t'
lines TERMINATED BY '\n'
IGnorE 1 lines
(fIEld1, fIEld2, fIEld3, fIEld4)""" % (csvfile, tabl_Name)
cursor.execute(Loadsql)
cnx.commit()
cursor.close()
cnx.close()
@H_675_6@
我正在尝试使用Python和MySQL Connector将普查数据动态加载到mysql数据库(从.csv文件)中。
我不知道为什么我得到错误:
TraceBACk (most recent call last):
File "miner.py",line 125,in <module>
cursor.execute(add_csv_file,csv_info)
File "/Library/Python/2.7/site-packages/mysql/connector/cursor.py",line 393,in execute
self._handle_result(self._connection.cmd_query(stmt))
File "/Library/Python/2.7/site-packages/mysql/connector/connection.py",line 586,in cmd_query
statement))
File "/Library/Python/2.7/site-packages/mysql/connector/connection.py",line 508,in _handle_result
raise errors.get_exception(packet)
mysql.connector.errors.programR_843_11845@ingError: 1064 (42000): 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 '/Users/afink/Documents/Research/Current Papers & Books/Youth Assessm' at line 1
@H_675_6@
执行前输出的字符串在同一用户下的MySQL命令行界面中运行良好。
似乎应该是一个简单的问题,但是我被卡住了!
def db_connect():
config = {
'user': 'username','password': 'pass','host': 'localhost','database': 'uscensus','raise_on_warnings': True,}
from mysql.connector import errorcode
try:
cnx = mysql.connector.connect(**config)
return cnx
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
cnx.close()
cursor = cnx.cursor()
os.chdir("./")
for files in glob.glob("*.csv"):
add_csv_file = ('''load data local infile '%(cwd)s/%(file_name)s' into table %(table_Name)s
columns terminated by ','
optionally enclosed by '\"'
escaped by '\"'
lines terminated by '\\n';''')
# Insert CSV file information
csv_info = {
'cwd': os.getcwd(),'file_name': files,'table_name': 'SF1_' + files[2:-8],}
print add_csv_file % csv_info # Temporary Debug
cursor.execute(add_csv_file,csv_info)
# Make sure data is committed to the database
cnx.commit()
cursor.close()
cnx.close()
@H_675_6@
提前谢谢你的帮助!
以上是大佬教程为你收集整理的LOAD DATA LOCAL INFILE上的Python2.7 MySQL Connector错误全部内容,希望文章能够帮你解决LOAD DATA LOCAL INFILE上的Python2.7 MySQL Connector错误所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。