0

I have a program that migrates data from a Microsoft Access database to a local sqlite database. It runs on a 32-bit Miniconda installation rather than the usual 64-bit version I use because the Access version is 32-bit. I reinstalled that 32-bit miniconda installation this morning after something happened with my IT department that moved some of anaconda's program files. The clean install seems to be working fine, but I'm getting a bizarre error message when I try to backup the sqlite database.

Calling sqlite3.Connection.backup raises the following error: 'sqlite3.Connection' object has no attribute 'backup'.

Except, of course, a sqlite3.Connection object does have an attribute called backup.

And, to make things even stranger, the file is being backed up. I can see a new file being created, but it's still throwing this error.

Here's a simplified version of the code that's causing the error:

I have a module called db that contains classes for connecting to both databases I have:

class SQL:
    def __init__(self, alt_path=None):
        if not alt_path:
            pdir = Path("C:\\path\to\default\project\directory")
            dbdir = pdir / "Data" / "db" / "SQL"
            sql_path = dbdir / "jjp.db"
            self.conn = sqlite3.connect(sql_path.as_posix())
        else:
            self.conn = sqlite3.connect(alt_path.as_posix())
        self.cursor = self.conn.cursor()
        self.table_names = [
            table[0]
            for table in self.cursor.execute(
                "SELECT name FROM sqlite_master WHERE type = 'table'"
            )
        ]

    def backup(self, backup_con):
        self.conn.backup(backup_con, pages=0)
        backup_con.close()

    def close(self):
        self.conn.close()

I'm importing those classes in a module called backup:

from jjp import db


def create_backup_con(db_type):
    today = datetime.today().strftime("%Y%m%d%H%M%S")
    if db_type == "sql":
        db_file = sql_backup_dir / f"jjp_backup_{today}.db"
        conn = sqlite3.connect(db_file.as_posix())
        return conn
    elif db_type == "access":
        db_file = acc_backup_dir / f"jjp_backup_{today}.accdb"
        return db_file


def backup_sql():
    sql = db.SQL()
    bck = create_backup_con("sql")
    sql.backup(backup_con=bck)
    bck.close()
    sql.close()

Here's the full error message output:

backup.backup_sql()

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
 in 
----> 1 backup.backup_sql()

c:\users\path\to\backup.py in backup_sql()
     38     sql = db.SQL()
     39     bck = create_backup_con("sql")
---> 40     sql.backup(backup_con=bck)
     41     bck.close()
     42     sql.close()

c:\users\path\to\db.py in backup(self, backup_con)
    159         """
    160 
--> 161         self.conn.backup(backup_con, pages=0)
    162         backup_con.close()
    163 

AttributeError: 'sqlite3.Connection' object has no attribute 'backup'

Running python 3.6.10 32-bit miniconda distribution on a 64-bit machine running Windows 10.

1 Answer 1

2

Running python 3.6.10

According to the documentation, support for the sqlite backup functionality was added in Python 3.7. Indeed, if you look at the 3.6 version of the docs, you'll see the description of the method is missing.

You'll have to use a newer Python release or find a different approach. Maybe VACUUM INTO if using sqlite 3.27 or newer.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I think when I created a new env after installing I just accidentally typed python=3.6 rather than python=3.7 so I didn't even realize it.

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.