Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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,我正在尝试将一个作为txt文件的数据库转换为MySQL数据库。我找到了教程并尝试按照它进行操作,但每次运行时都会出现错误: mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'Pdf' in 'field list' 这是我写的代码 import shutil import os import mysql.connector mydb = mysql.connector.connect( hos

我正在尝试将一个作为txt文件的数据库转换为MySQL数据库。我找到了教程并尝试按照它进行操作,但每次运行时都会出现错误:

mysql.connector.errors.ProgrammingError: 1054 (42S22): Unknown column 'Pdf' in 'field list'
这是我写的代码

import shutil
import os

import mysql.connector 

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  database="Book"
)

mycursor = mydb.cursor()

mycursor.execute("CREATE TABLE IF NOT EXISTS Books (name VARCHAR(255), address VARCHAR(255))")


if os.path.exists("/home/Me/Desktop/Books/INFO.txt"): 
  #Extract the data from the txt file
  f= open("/home/Me/Desktop/Books/INFO.txt","r")
  Lines = f.readlines()
  Items=[]
  for line in Lines:
    line=line.replace("\n","")
    Items_not_s=line.split(",")
    Items_not_s=tuple(Items_not_s)
    Items.append(Items_not_s)
 #Extracted the data from the txt file

  sql = "INSERT INTO Books (Name, Pdf, Image, DegradedImage, Tags, Extra) VALUES (%s, %s, %s, %s, %s, %s)"


  val = Items
  print(val[20])
  mycursor.executemany(sql, val)
  mydb.commit()

  print(mycursor.rowcount, "was inserted.") 

else:
    print("no file")
我想在表中输入的值(val)示例如下:

[('geekychefcookbook', '/home/Me/Desktop/Books/Food/Quarto Cookbooks/geekychefcookbook/PDF/PDF.pdf', 'None', '/home/Me/Desktop/Books/Food/Quarto Cookbooks/geekychefcookbook/IMAGE/IMAGE_RE.jpg', 'None', 'None'),('wickedgoodburgers', '/home/piotr/Desktop/Books/Food/Quarto Cookbooks/wickedgoodburgers/PDF/PDF.pdf', 'None', '/home/piotr/Desktop/Books/Food/Quarto Cookbooks/wickedgoodburgers/IMAGE/IMAGE_RE.jpg', 'None', 'None')]

提前谢谢

当您试图插入时,MYSQL表没有名为
Pdf、Image、DegradedImage、Tags、Extra的列

您仅有的列是
名称
地址
,定义如下:

mycursor.execute(“创建表,如果不存在图书(名称VARCHAR(255),地址VARCHAR(255))”


添加此行中的所有列(在
address VARCHAR(255)
之后用逗号分隔,并使用它们的特定数据类型,然后尝试从文本文件中插入行。

当然!完全忽略了这一点。谢谢