【问题标题】:Syntaxe Error with variable on Databse (MySQL / Python)数据库变量的语法错误(MySQL / Python)
【发布时间】:2019-11-06 10:41:08
【问题描述】:

一开始我声明了一个变量“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

【问题讨论】:

  • 向我们展示错误。

标签: python mysql database scrapy


【解决方案1】:

在执行创建表语句后,使用 self.conn.commit() 提交数据库更改。并重命名删除表存在 sql 命令。

【讨论】:

  • 我只想找到好的语法来调用变量的值
  • 将变量定义为全局变量
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-31
  • 1970-01-01
  • 2016-03-26
  • 2021-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多