Python中的MySQL连接器不允许加载数据填充语法
我正在尝试向MySQL数据库发送一个文本文件。我正试图用Python3.2中的mysql连接器来实现这一点。问题在于加载数据填充语法。你可以在上面找到我的代码。我的第一个问题是,到底有没有办法解决这个问题。请注意,我尝试了localinfle=1选项,Python不允许使用此选项。第二,是否有其他方法将此数据作为块发送到mysql数据库中Python中的MySQL连接器不允许加载数据填充语法,python,mysql-python,Python,Mysql Python,我正在尝试向MySQL数据库发送一个文本文件。我正试图用Python3.2中的mysql连接器来实现这一点。问题在于加载数据填充语法。你可以在上面找到我的代码。我的第一个问题是,到底有没有办法解决这个问题。请注意,我尝试了localinfle=1选项,Python不允许使用此选项。第二,是否有其他方法将此数据作为块发送到mysql数据库中 from __future__ import print_function import os import mysql.connector from mys
from __future__ import print_function
import os
import mysql.connector
from mysql.connector import errorcode
config = {
'user':'root',
'password':'3778',
## 'host':'localhost',
# 'database':'microstructure',
# 'local-infile':'1',
}
DB_NAME = 'EURUSD'
TABLES ={}
TABLES['microstructure']=(
"CREATE TABLE `microstructure` ("
# " `p_id` int NOT NULL AUTO_INCREMENT,"
" `ticker` varchar(255),"
" `time` date,"
" `last_price` decimal(6,3)"
") ENGINE=InnoDB")
TABLES['cumulative']=(
"CREATE TABLE `cumulative` ("
" `p_id` int NOT NULL AUTO_INCREMENT,"
" `ticker` varchar(255),"
" `time` date,"
" `last_price` decimal(6,3),"
" PRIMARY KEY(`p_id`)"
") ENGINE=InnoDB")
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
path_txt = 'C:/Users/ibrahim/Desktop/testfile.txt'
def create_database(cursor):
try:
cursor.execute(
"CREATE DATABASE IF NOT EXISTS {} DEFAULT CHARACTER SET 'utf8'".format(DB_NAME))
except mysql.connector.Error as err:
print("Failed creating database: {}".format(err))
exit(1)
try:
cnx.database = DB_NAME
except mysql.connector.Error as err:
if err.errno == errorcode.ER_BAD_DB_ERROR:
create_database(cursor)
cnx.database=DB_NAME
else:
print(err)
exit(1)
for name, ddl in TABLES.items():
try:
print("Creating table {}: ".format(name), end ='')
cursor.execute(ddl)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
print("Already exists")
else:
print(err)
else:
print("OK")
cursor.execute("SET @@global.local_infile = 1")
cursor.execute("LOAD DATA LOCAL INFILE 'testfile.txt' into table microstructure")
os.system("start")
cursor.close()
在MySQLdb中,我使用此选项启用加载数据本地填充功能:
MySQLdb.connect(..., local_infile=True)
您使用的是哪个版本的mysql.connector?我使用的是1.0.11,我的功能正常工作。我所做的是用三行(1、2、3)创建data.txt,将cd放入其中,启动python 3.3.1,然后运行:
import mysql.connector
conn = mysql.connector.connect(database='test', user='test', password='xx')
curs = conn.cursor()
curs.execute('''CREATE TABLE foo (id text)''')
curs.execute('''LOAD DATA INFILE '/<full path to>/data.txt' INTO TABLE foo''')
curs.execute('''SELECT * FROM FOO''')
print(curs.fetchall())
curs.execute('''DROP TABLE foo''')
我也有类似的问题。我不熟悉python和mysqldb。我添加了一个conn.commit,它成功了。
它似乎通过要求提交来保护数据库。我在这里回答了同样的问题:
简而言之,您需要使用client_flags参数在连接中添加一个client标志我刚刚看到这篇旧文章,但是这些答案都没有解决我的问题 我看到有一个专门用于本地数据填充的论点:
allow\u local\u infle=True
因此,有可能做到:
mysql.connector.connect(user='[username]', password='[pass]', host='[host]', allow_local_infile=True)
也。不要忘记在db上执行commit()。这应该不是必需的:同一个链接清楚地表明
allow\u local\u infle
参数的默认值已经是True
mysql.connector.connect(user='[username]', password='[pass]', host='[host]', allow_local_infile=True)