Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将文本文件数据导出到mysql数据库_Python_Mysql - Fatal编程技术网

Python 如何将文本文件数据导出到mysql数据库

Python 如何将文本文件数据导出到mysql数据库,python,mysql,Python,Mysql,我想将文本文件数据导出到MySQL数据库中 import MySQLdb import re conn = MySQLdb.connect (host = "127.0.0.1", user = "root", passwd = "123456") mycursor =conn.cursor() mycursor.execute("CREATE DATABASE IF NOT EXISTS EMP") mycursor.execute("USE EMP") mycursor.execute

我想将文本文件数据导出到MySQL数据库中

import MySQLdb
import re

conn = MySQLdb.connect (host = "127.0.0.1", user = "root", passwd = "123456")
mycursor =conn.cursor()
mycursor.execute("CREATE DATABASE IF NOT EXISTS  EMP")
mycursor.execute("USE  EMP")
mycursor.execute("CREATE TABLE IF NOT EXISTS emp_details  (Id VARCHAR(255) , Firstname VARCHAR(255),Lastname VARCHAR(255),department VARCHAR(255),salary VARCHAR(255)) ")

f = open("new.txt", "rb")
print (f.next())

for x in f:

  mycursor.execute("INSERT INTO emp_details VALUES (%s,%s,%s,%s,%s)",x)
  conn.commit()

print(mycursor.rowcount, "record inserted.")
我犯了一个错误

query = query % args
TypeError: not all arguments converted during string formatting

以逗号分隔行,因为必须为该执行字符串提供5个参数。此外,还应明确命名这些列:

mycursor.execute("INSERT INTO emp_details (Id, Firstname, Lastname, department, salary) VALUES (%s,%s,%s,%s,%s)", x.split(","))

以逗号分隔行,因为必须为该执行字符串提供5个参数。此外,还应明确命名这些列:

mycursor.execute("INSERT INTO emp_details (Id, Firstname, Lastname, department, salary) VALUES (%s,%s,%s,%s,%s)", x.split(","))

query=query%args TypeError:格式化字符串的参数不够
现在出现此错误可能是文件中的某行格式不好。请检查您的数据文件或仅将该文件截断为几行,如果代码对该示例有效,请尝试,然后检查您的文件。
query=query%args TypeError:格式化字符串的参数不够。
现在出现此错误可能是文件中的某行格式不好。检查数据文件或仅将该文件截断为几行,如果代码适用于该示例,请尝试,然后检查文件。