Unable to use Cursor in separate function

I’m currently tryign to learn how to use SQLite3 and am trying to seperate setting up the DB through functions. My second function is where I’m having the error : AttributeError: 'NoneType' object has no attribute 'cursor'. After looking at the SQLite3 doucmentation, I got the impression that it’s best to seperate diffrent methods for the DB into diffrent functions.

import sqlite3
from sqlite3 import Error


def create_connection(CRM):
    """
    This Function will check to see if the Database is already created, if not will be created.
    :param CRM: Location of the Database
    :return: None
    """
    db = None
    try:
        db = sqlite3.connect(CRM)
        print(sqlite3.version)
    except Error as e:
        print(e)
    finally:
        if db:
            db.close()
    return db

def create_table(db):
    cur = db.cursor()
    G8Personnel = ''' CREATE TABLE [IF NOT EXISTS] G8Personnel(  
                DoD INTEGER NOT NULL PRIMARY KEY,
                Rank varchar NOT NULL,
                FirstName varchar NOT NULL,
                LastName varchar NOT NULL,
                Role varchar NOT NULL
                )'''
    cur.execute(G8Personnel)

    Company = '''CREATE TABLE [IF NOT EXISTS] Company(
            CompanyName varchar PRIMARY KEY,
    )'''
    cur.execute(Company)

    Employee = '''CREATE TABLE [IF NOT EXISTS] Employee(
               AutoID INTEGER  PRIMARY KEY AUTOINCREMENT,
               FirstName varchar NOT NULL,
               LastName varchar NOT NULL,
               Email varchar NOT NULL,
               JobTitle varchar,
               WebPage varchar,
               Notes varchar,
               Company varchar
               FOREIGN KEY (Company) REFERENCES  Company(CompanyName)
       )'''
    cur.execute(Employee)

    Meeting = '''CREATE TABLE [IF NOT EXISTS] Meeting(
            AutoID INTEGER PRIMARY KEY AUTOINCREMENT,
            Date1 real NOT NULL, 
            DoD INTEGER NOT NULL,
            Employee INTEGER NOT NULL,
            MeetingNotes varchar NOT NULL
            FOREIGN KEY (DoD) REFERENCES G8Personnel (DoD)
            FOREIGN KEY (Employee) REFERENCES Employee (AutoID)
             )'''
    cur.execute(Meeting)




if __name__ == '__main__':
    db = None
    create_connection(r'C:Usersc94reDocumentsGit-RepoCRMCRM.db')
    create_table(db)

Answer

You are not capturing the return value of create_connection. Try to assign the return type to a variable and try again.

if __name__ == '__main__':
    db = create_connection(r'C:Usersc94reDocumentsGit-RepoCRMCRM.db')
    create_table(db)

I think this should do it, if you have other problems, please edit your question.