Nov-11-2018, 09:15 AM
Hello,
I'm following the SQLite tutorial to create a database and tables and I'm pulling my hair out on this error. I don't see it. My linter is showing the 'conn' variable as the error, which means it may be that or the code line bofore that variable creation.
Do you see anything obvious?
I think the way I am calling for the db is the fault, but I'm unsure why.
Win10-64 bit
Python 3.7.0
Dell XPS-13
Thanks,
phil
I'm following the SQLite tutorial to create a database and tables and I'm pulling my hair out on this error. I don't see it. My linter is showing the 'conn' variable as the error, which means it may be that or the code line bofore that variable creation.
Do you see anything obvious?
I think the way I am calling for the db is the fault, but I'm unsure why.
Win10-64 bit
Python 3.7.0
Dell XPS-13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
''' [SqliteTutorial.net](http://www.sqlitetutorial.net/sqlite-python/creating-database/) ''' import sqlite3 from sqlite3 import Error # Create database def create_connection(db_example): ''' create a db connection to a sqlite db ''' try : conn = sqlite3.connect(db_example) # write to hard disk # conn = sqlite3.connect(':memory:') # write to RAM only print (sqlite3.version) except Error as err: print ( 'Error: ' , err) # finally: # conn.close() if __name__ = = '__main__' : create_connection( 'D:\\Software\db\sqlite\example.db' ) # create_connection() # use when writing to RAM only # create tables def create_table(conn, create_table_sql): """ create a table from the create_table_sql statement :param conn: Connection object :param create_table_sql: a CREATE TABLE statement :return: """ try : c = conn.cursor() c.execute(create_table_sql) print ( 'connection to \'example.db\' established' ) except Error as err: print ( 'Error: ' , err) # create main() function to create the named tables. def main(): database = 'D:\Software\db\sqlite\example.db' sql_create_csv01_table = '''CREATE TABLE IF NOT EXISTS csv01 ( id integer PRIMARY KEY, name text NOT NULL, begin_date text, end-date text ); ''' sql_create_csv02_table = ''' CREATE TABLE IF NOT EXISTS csv02 ( id integer PRIMARY KEY, name text NOT NULL, priority integer, status_id integer NOT NULL, project_id integer NOT NULL, begin_date text NOT NULL end_date text NOT NULL FOREIGN KEY (project_id) REFERENCES csv01 (id) ); ''' # create a db connection conn = create_connection(database) # HERE IS THE LINTER ERROR if conn is not None : # create csv01 table create_table(conn, sql_create_csv01_table) # create csv02 table create_table(conn, sql_create_csv02_table) else : print ( 'Error: Can not create the database connection' ) if __name__ = = '__main__' : main() |
phil