Databse上的变量存在语法错误(MySQL / Python)

时间:2019-06-24 15:24:16

标签: python mysql database scrapy

首先,我声明了一个变量“ nom_of_game_db”,但是此后,我在MySQL级别上找不到正确的语法来将该变量回叫:

    # -*- coding: utf-8 -*-

name_of_game_db = input("Name of game ?")

class QuotetutorialPipeline(object):

    def __init__(self):
        self.create_connection()
        self.create_table()

    def create_connection(self):
        self.conn = mysql.connector.connect(
                host = 'localhost',
                user = 'root',
                passwd = 'jozefleonel',
                database = 'prices'
            )
        self.curr = self.conn.cursor()

    def create_table(self):
        self.curr.execute("""DROP TABLE IF EXISTS nam_of_game_db""")
        self.curr.execute("""create table name_of_game_db(
                        name text,
                        platform text,
                        edition text
                        )""")

    def process_item(self, item, spider):
        self.store_db(item)
        return item

    def store_db(self,item):
        self.curr.execute("""insert into name_of_game_db values (%s,%s,%s)""", (
            item['name'][0],
            item['platform'][0],
            item['edition'][0]
            ))
        self.conn.commit()

我使用Python和MySQL

1 个答案:

答案 0 :(得分:1)

执行完表创建语句后,使用self.conn.commit()提交数据库更改,并重命名drop table exist sql命令。