简体   繁体   English

使用python将数据从文件插入mysql时出错

[英]error while doing insert data from a file to mysql using python

there is a file containing data that I want to insert into table in mysql using python.I retrieved the data from file using "with open" and converted it to a list.so now in the list the elements are string and i want to change string to tuple so that i can retrieve the data from this to mysql. 有一个文件包含我要使用python插入到mysql表中的数据。我使用“with open”从文件中检索数据并将其转换为list.so现在在列表中元素是字符串,我想要更改字符串到元组,以便我可以从这里检索数据到mysql。

    import mysql.connector


    with open("/home/ninky/com.csv", "r") as fp:
        content = fp.read()
    lines = content.split("\n")
    print(lines)

        myconn = mysql.connector.connect(host="localhost", user="root", database="EMPSALARY" 
        cur = myconn.cursor()
        db = "insert into PERSON(name,age,year)values(%s,%s,%s)"
        myconn = cur.execute(db, lines)
        myconn.close()
result:
-----------
['deepak,29,2019', 'ninky,29,2010', 'suraj,29,2020', 'pratap,30,2018', '']
deepak,29,2019
ninky,29,2010
suraj,29,2020
pratap,30,2018
 Traceback (most recent call last): File "/home/ninky/PycharmProjects/new/csv_db.py", line 23, in <module> cur.execute(db, lines) File "/usr/local/lib/python3.6/dist-packages/mysql/connector/cursor.py", line 543, in execute "Not all parameters were used in the SQL statement") mysql.connector.errors. ProgrammingError: Not all parameters were used in the SQL statement 
Process finished with exit code 1

In lines what you have is a list containing each line of your CSV file. 在行中,您的列表包含CSV文件的每一行。 Then, you need to iterate through all lines and do a split again on your separator (','): 然后,您需要遍历所有行并在分隔符(',')上再次进行拆分:

import mysql.connector

with open("/home/ninky/com.csv", "r") as fp:
    content = fp.read()
lines = content.split("\n")
print(lines)


myconn = mysql.connector.connect(host="localhost", user="root", database="EMPSALARY")
cur = myconn.cursor()

db = "insert into PERSON (name,age,year) values (%s,%s,%s)"
for l in lines:
    data=l.split(',')
    cur.execute(db, data)
myconn.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用python在neo4j中创建节点,同时从.data文件进行批量插入时仅具有唯一的边缘/关系? - how to create nodes in neo4j using python, only with unique edges/relations while doing bulk insert from .data file? python脚本中的INSERT QUERY错误,无法将数据插入MySQL数据库 - Error with INSERT QUERY from python script to insert data into MySQL database 尝试使用 Python 将数据插入 MySQL 表时出现编程错误 - ProgrammingError while trying to insert data into a MySQL table using Python 使用python在mysql中插入数据 - Insert Data in mysql using python 如何有效地使用 Python 将 CSV 文件数据插入到 MYSQL 中? - How to insert a CSV file data into MYSQL using Python efficiently? 使用 Python 在 mysql 中进行插入时,出现“字段列表中的未知列”错误 - While doing insertion in mysql using Python , getting an error as “Unknown Column in field list” 从 sql 文件或使用 csv 文件将数据插入 mysql - Insert data into mysql from sql file or using csv file 如何使用 Python 将列表中的多维数据插入到 MYSQL - How to insert multi dimensional data from a list to MYSQL using Python 使用 MySQL 数据库中的 Python 选择数据时使用“%s”附近错误的正确语法 - Correct syntax to use near '%s' error while selecting data using Python from MySQL database 在python中从MySql数据库中获取数据时出错 - Error while fetching data from MySql Database in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM