Hello pythonites:
I am having a problem with pythonanywhere with sqlite3 databases. I have a FLASK app that I have set as a web app on PA. I have read the other posts about databases and relative/absolute paths, etc. In my code, the database is created, but the tables are never created. It is wierd. I get a 0 byte file (account_2026.db). When I upload my version of the database, sqlite3 on PA reads and writes the database fine. But if the database does not exist, the first conn creates a 0 byte database, but it does not create any of the tables. Also, where do the output log entries go? This code runs fine in visual studio code, but on PA, I can't see the good messages or the error messages. I see some messages in the error logs, but they are not related to any of these log entries in the code. Where can i find the messages in PA? I am logging on success as well as failure, but I can't see any of the messages anywhere. Pls help!
#Database path
ACCOUNT = '/home/gopalnn/account_2026.db'
def init_db():
try:
# Attempt to connect to the local SQLite database
with sqlite3.connect(ACCOUNT) as conn:
cursor = conn.cursor()
# Create the account table if it does not exist
try:
cursor.execute('''CREATE TABLE IF NOT EXISTS account (
id INTEGER PRIMARY KEY AUTOINCREMENT,
userid TEXT,
password TEXT,
guid TEXT,
timestamp TEXT
)''')
conn.commit()
logging.info("Account table created or verified successfully.")
except sqlite3.Error as e:
logging.error(f"Error creating 'account' table: {e}")
# Create the inventory table if it does not exist
try:
cursor.execute('''CREATE TABLE IF NOT EXISTS inventory (
id INTEGER PRIMARY KEY AUTOINCREMENT,
guid TEXT,
originalPrompt TEXT,
category TEXT,
item TEXT,
count TEXT,
timestamp TEXT
)''')
conn.commit()
logging.info("Inventory table created or verified successfully.")
except sqlite3.Error as e:
logging.error(f"Error creating 'inventory' table: {e}")
# Create the preference table if it does not exist
try:
cursor.execute('''CREATE TABLE IF NOT EXISTS preference (
id INTEGER PRIMARY KEY AUTOINCREMENT,
guid TEXT,
originalPrompt TEXT,
category TEXT,
type TEXT,
preference TEXT,
timestamp TEXT
)''')
conn.commit()
logging.info("Preference table created or verified successfully.")
except sqlite3.Error as e:
logging.error(f"Error creating 'preference' table: {e}")
except sqlite3.Error as e:
logging.error(f"Database connection error: {e}")