B
B
Black back2020-09-18 12:26:37
Python
Black back, 2020-09-18 12:26:37

Can't insert object into %s how to do it in python?

self.cursor.execute("insert into %s ( NameTeam, Score, Data) values (%s,%s,%s); ",
psycopg2.errors.SyntaxError: ОШИБКА:  ошибка синтаксиса


# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
from .spiders import sql
import psycopg2
from .items import MarafonItem

    def db(self, item):
        b = 0;
        a = 0;
        table_name = len(item['Category_label'])
        total_name = len(item['Names'])
        while b <= table_name:
            while a <= total_name:
                self.cursor.execute("insert into %s ( NameTeam, Score, Data) values (%s,%s,%s); ",
                                    (item['Category_label'][b],
                                        item['Names'][a],
                                        item['Score'][a],
                                        item['Date'][a]
                                    ))
                self.connect.commit()
                a += 1;
            b += 1;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2020-09-18
@bekss

The table name cannot be substituted.

S
Stanislav Pugachev, 2020-09-18
@Stqs

self.cursor.execute(
    "insert into %s ( NameTeam, Score, Data) values (%%s,%%s,%%s); " % item['Category_label'][b],
    (item['Names'][a], item['Score'][a], item['Date'][a])
)

so in theory it should work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question