There seem to be similar but not quite the same issues discussed in some forums including StackOverflow, so they did not help with my problem below.
I can add a series of tables to a sqlite3 database without errors using either DB Browser for Sqlite or Python except in one case, in which case DB Browser works fine but python fails. My goal is to create a python program that sequentially runs sqlite3 scripts automatically.
The place where python hangs up is in the following script (One of several sequential "create table" scripts in the same file) near "AS" according to the error message:
DROP TABLE if EXISTS SheetGoods;
CREATE TABLE SheetGoods (
SheetId INTEGER PRIMARY KEY,
ItemNo INTEGER,
ItemCategory TEXT,
ItemName TEXT,
SheetName TEXT,
Width REAL,
Height REAL,
Depth REAL,
Thickness REAL,
Area REAL GENERATED ALWAYS AS ((Width * Height)/144) STORED,
/* SYNTAX for line above similar to: column INT GENERATED ALWAYS AS (a*abs(b)) VIRTUAL or STORED
Consider putting this formula into a Python script and update table with python output*/
for_Pricing INTEGER,
for_Order_List INTEGER,
Comments TEXT,
FOREIGN KEY(ItemNo) REFERENCES Items(ItemId)
);
The Python script that calls the sqlite3 query above works fine for all of my tables except for this one instance. If I comment out the end of the generated column after Column: "Area REAL" (i.e.: the following is left out - "GENERATED ALWAYS AS ((Width * Height)/144) STORED )" then python does not hang. I haven't been able to find a way to sort out the issue that Python has with this syntax.
The python code is:
# runNewProjectScript.py
import sqlite3
newDbName = "Johnston.db"
try:
sqliteConnection = sqlite3.connect(newDbName)
cursor = sqliteConnection.cursor()
print("Successfully Connected to SQLite")
# the following was advice from sqlite3 website. But it seems there is issues.
cursor.execute('pragma foreign_keys=ON')
with open('C:\myPy38\sqlite\sqLiteCabTest\cabPricingScripts\cabPricing.sql', 'r') as sqlite_file:
sql_script = sqlite_file.read()
cursor.executescript(sql_script)
print("SQLite script executed successfully")
cursor.close()
except sqlite3.Error as error:
print("Error while executing sqlite script", error)
finally:
if (sqliteConnection):
sqliteConnection.close()
print("sqlite connection is closed")
The error message is as follows:
$ python runNewProjectScript.py
Successfully Connected to SQLite
Error while executing sqlite script near "AS": syntax error
sqlite connection is closed