0

I'm trying to take recipe hyperlinks from a pre-existing file and add them to a table in a database. I have already created the database and given the file name to set up a connection and insert the data into the table but whenever I do so I get the error sqlite3.OperationalError: unable to open database file. Here's my code:

import bs4, os, requests, time
import sqlite3
from flask import current_app, g

def create_connection(db_file):
    db = sqlite3.connect(db_file)
    c = db.cursor()

    return db


def get_html():
    '''
    Get the BBC Food sitemap and save it to a local file.
    '''
    page = None
    db = create_connection("pantry/instance/flaskr.sqlite")
    for attempt in range(1, 4):
        print("line 40")
        page = requests.get('http://www.bbc.co.uk/food/sitemap.xml')
        try:
            page.raise_for_status()
            break
        except requests.RequestException:
            time.sleep(attempt * 10)

    if not page:
        raise Exception('Failed to get sitemap.xml')

    sitemap = bs4.BeautifulSoup(page.text, 'html.parser')
    # Write the recipe urls to a text file
    print("line 53")
    for line in sitemap.find_all('loc'):
        for string in line.stripped_strings:
            if string.startswith('https://www.bbc.co.uk/food/recipes/'):
                print("line 57")
                recipeUrl = string
                if (
                        db.execute("SELECT recipeID FROM recipe WHERE weblink = ?", (recipeUrl,)).fetchone()
                        is not None
                ):
                    error = "recipe weblink {0} is already inputted.".format(recipeUrl)
                if error is None:
                    db.execute(
                        'INSERT INTO recipe (weblink) VALUES (?)',
                        recipeUrl
                    )
                    db.commit()
    db.close()

And the error message:

Traceback (most recent call last):
File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm 2019.2.2\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2019.2.2\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Eva Morris/PycharmProjects/pantry/flaskr/BBCscraper/scraperecipes.py", line 161, in <module>
    get_html()
  File "C:/Users/Eva Morris/PycharmProjects/pantry/flaskr/BBCscraper/scraperecipes.py", line 34, in get_html
    db = create_connection("pantry/instance/flaskr.sqlite")
  File "C:/Users/Eva Morris/PycharmProjects/pantry/flaskr/BBCscraper/scraperecipes.py", line 23, in create_connection
    db = sqlite3.connect(db_file)
sqlite3.OperationalError: unable to open database file
1

1 Answer 1

0

Based on the consoles error report, your .py file is located in: C:/Users/Eva Morris/PycharmProjects/pantry/flaskr/BBCscraper/scraperecipes.py".

In your code you set your databases directory using the relative path as:pantry/instance/flaskr.sqlite.

This means python is looking for the directory: C:/Users/Eva Morris/PycharmProjects/pantry/flaskr/BBCscraper/pantry/instance/ to create/link your database file in.

If that is not a preexisting directory, sqlite3.connect will not be able to create your database flaskr.sqlite.

You may need to update the code db = create_connection("pantry/instance/flaskr.sqlite") to something like db = create_connection("flaskr.sqlite") which should work without any problem since it will just create the database in your working directory.

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.