0

Using Scrapy to get some stuff and I want to store it in a Database. Never done anything with MySQL or Python before so looking for some help as to why this wont work.

Here is my code:

from __future__ import print_function
from metacritic.items import MetacriticItem
from mysql.connector import errorcode
import mysql.connector
import json

class MetacriticPipeline(object):

DB_NAME = 'metacritic'

TABLES = {}
TABLES['titles'] = (
    "CREATE TABLE `titles` ("
    "   `name` varchar (14) NOT NULL,"
    "   PRIMARY KEY (`emp_no`)"
    ")  ENGINE=InnoDB")

cnx = mysql.connector.connect(user='root', password = 'andy')
cursor = cnx.cursor()

def process_item(self, item, spider):
    if item['title']:
        return item

class JsonWriterPipeline(object):

def __init__(self):
    self.file = open('items.jl', 'wb')

def process_item(self, item, spider):
    line = json.dumps(dict(item)) + "\n"
    self.file.write(line)
    return item

class WriteToDatabasePipeline(object):

def create_database(cursor):
    try:
        cursor.execute(
            "CREATE DATABASE {} DEFAULT CHARACTER SET 'utf8'".format(DB_NAME))
    except mysql.connector.Error as err:
        print("Failed creating database: {}".format(err))
        exit(1)

try:
    cnx.database = DB_NAME
except mysql.connector.Error as err:
    if err.errno == errorcode.ER_BAD_DB_ERROR:
        create_database(cursor)
        cnx.database = DB_NAME
    else:
        print(err)
        exit(1)

I'm getting this error in CMD when I try run it:

File "metacritic\pipelines.py", line 46, in WriteToDatabasePipeline
cnx.database = DB_NAME
NameError: name 'DB_NAME' is not defined

Any idea why this is? It looks to me like DB_NAME is defined okay? I just want to make the database for now and try add in tables afterwards. Thanks for any help

2 Answers 2

1

You are defining DB_NAME inside the MetacriticPipeline class, move it at the top of the script (or better move to scrapy settings):

from __future__ import print_function
from metacritic.items import MetacriticItem
from mysql.connector import errorcode
import mysql.connector
import json

DB_NAME = 'metacritic'

class MetacriticPipeline(object):
   ...
Sign up to request clarification or add additional context in comments.

Comments

1

DB_NAME is defined in class MetacriticPipeline, make it global or do this:

cnx.database = MetacriticPipeline.DB_NAME

1 Comment

thanks for this. alecxe's answer worked so I will have to accept his but thanks for the reply, I upvoted you anyway :)

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.