Python Forum
Rows not adding to sqlite3 database using SQLAlchemy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rows not adding to sqlite3 database using SQLAlchemy
#1
I'm trying to understand how to use SQLAlchemy. I've written a piece of code that should simply add one record to an existing database. When I run the code I don't get an error, but nothing writes to the database. I'm obviously doing something wrong, but I can't spot it.

I'm using Python 3.7.11. SQLAlchemy is 2.0.12.

#!/cmts/fastapi/sv/venv/bin/python3

from datetime import datetime

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from sqlalchemy import Column, Integer, String, Date

# Location of the test catalog database
SQLALCHEMY_DATABASE_URL = "sqlite:///./test_catalog.db"

engine = create_engine(SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False})

Base = declarative_base()

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)


class Test(Base):
    __tablename__ = "catalog"

    id = Column(String, primary_key=True)  # Identifying ID for this test
    parent_id = Column(String)  # ID of test that this was generated from, if not a parent test
    request_id = Column(String)  # UUID ID used to group multiple tests that are submitted at the same time
    test_type = Column(String)  # Type of test: Online, Impaired, Network, etc...
    target = Column(String)  # Target for this test
    user = Column(String)  # Username of who initiated the test
    submitted = Column(Date)  # When was this test submitted
    started = Column(Date)  # When was this test started
    ended = Column(Date)  # When was this test completed
    progress = Column(Integer, default=0)  # Progress indicator for this test, from 0 to 100
    state = Column(String, default="New")  # State of the test: New, Running, Done, Error
    result = Column(String)  # JSON results of the test. Compressed?


def get_db():
    try:
        db: Session = SessionLocal()
        return db
    except Exception as e:
        print(e)
#    finally:
#        db.close()


Base.metadata.create_all(bind=engine)

db = get_db()

test_model = Test()
test_model.id = "1234567890"
test_model.parent_id = None
test_model.request_id = "0987654321"
test_model.target = "target"
test_model.user = "user"
test_model.test_type = "test"
test_model.submitted = datetime.now()
test_model.started = None
test_model.ended = None
test_model.progress = 0
test_model.state = "New"
test_model.result = None

db.add(test_model)
db.commit()

print("Added")

print(db.query(Test).all())

db.close()
I check my database, and there are no rows:
$ sqlite3 test_catalog.db
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from catalog;
sqlite>
Reply


Messages In This Thread
Rows not adding to sqlite3 database using SQLAlchemy - by Calab - Jun-01-2023, 02:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Create SQLite3 database with peewee Jim53_1980 2 775 Dec-20-2023, 02:38 PM
Last Post: buran
  Regex replace in SQLite3 database WJSwan 1 869 Dec-04-2023, 05:55 PM
Last Post: Larz60+
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 851 Aug-09-2023, 05:51 PM
Last Post: Calab
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 1,489 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,678 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Python Variables and Sqlite3 Database Staples200 1 3,264 May-25-2021, 02:40 AM
Last Post: Staples200
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,175 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,440 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  sqlite3 database problem Maryan 2 2,574 Oct-05-2020, 05:21 AM
Last Post: buran
  Adding data to a table in SQLite3 djwilson0495 2 3,137 Aug-15-2020, 02:48 PM
Last Post: djwilson0495

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020