0

i want to insert it into database, please help me to solve it.

    import numpy as np
    import sys
    import mysql.connector
    from mysql.connector import Error

    id = 'PEL0005,PEL0006,PEL0007,PEL0008,PEL0009,PEL0010,PEL0011,PEL0012,PEL0013,PEL0014'
    p = [[9,1,7,3,14,3,18,2,22,2],[2,2,6,4,4,9,3,3,3,7],[13.9769,12.1656,40.047,28.287,25.1415,47.6875,34.269,38.3822,47.6875,19.575]]
    result = ['C2', 'C2', 'C1', 'C2', 'C2', 'C1', 'C3', 'C1', 'C3', 'C2']
    def insertData(id, recency, frequency, monetary, result):
        try:
            connection = mysql.connector.connect(host='localhost', database='myDB', user='root', password='')
            cursor = connection.cursor()
            query = """INSERT INTO clus_result (id, recency, frequency, monetary, result) 
                                    VALUES (%s, %s, %s, %s, %s) """

            recordTuple = (id, recency, frequency, monetary, result)
            cursor.execute(query, recordTuple)
            connection.commit()
            print("Inserted successfully")

        except mysql.connector.Error as error:
            print("Failed to insert into MySQL table {}".format(error))

        finally:
            if (connection.is_connected()):
                cursor.close()
                connection.close()
                print("MySQL connection is closed")

    insertData(id,p[0],p[1],p[2],result)

Error : Failed to insert into MySQL table Failed processing format-parameters; Python 'list' cannot be converted to a MySQL type

i expect the result : id | recency | frequency | monetary | result

PEL0005 | 2 | 13.9789 | C2

PEL0006 | 2 | 12.1656 | C2

etc..

2
  • Possible duplicate of - stackoverflow.com/questions/8316176/… Commented Nov 11, 2019 at 10:57
  • Out of id,p[0],p[1],p[2],result p[0],p[1],p[2],result are lists. You should zip all of these and then use executemany function of cursor. Commented Nov 11, 2019 at 11:00

1 Answer 1

0

You should do this:-

import numpy as np
import sys
import mysql.connector
from mysql.connector import Error

id = 'PEL0005,PEL0006,PEL0007,PEL0008,PEL0009,PEL0010,PEL0011,PEL0012,PEL0013,PEL0014'
p = [[9,1,7,3,14,3,18,2,22,2],[2,2,6,4,4,9,3,3,3,7],[13.9769,12.1656,40.047,28.287,25.1415,47.6875,34.269,38.3822,47.6875,19.575]]
result = ['C2', 'C2', 'C1', 'C2', 'C2', 'C1', 'C3', 'C1', 'C3', 'C2']
def insertData(id, recency, frequency, monetary, result):
    try:
        connection = mysql.connector.connect(host='localhost', database='myDB', user='root', password='')
        cursor = connection.cursor()
        query = """INSERT INTO clus_result (id, recency, frequency, monetary, result) 
                                VALUES (%s, %s, %s, %s, %s) """

        records = zip(id.split(','), p[0], p[1], p[2], result)
        cursor.executemany(query, records)
        connection.commit()
        print("Inserted successfully")

    except mysql.connector.Error as error:
        print("Failed to insert into MySQL table {}".format(error))

    finally:
        if (connection.is_connected()):
            cursor.close()
            connection.close()
            print("MySQL connection is closed")

insertData(id,p[0],p[1],p[2],result)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.