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
#2
If you want to use the DB, why are you closing it in get_db? What's the purpose of the db.close() there?

If you want to close it on an exception, put that in the exception block, not a finally block.
Reply
#3
(Jun-01-2023, 03:23 PM)bowlofred Wrote: If you want to use the DB, why are you closing it in get_db? What's the purpose of the db.close() there?

If you want to close it on an exception, put that in the exception block, not a finally block.

When I get a successful connection, it will return from the function, so never do the finally block. The db shouldn't close.
Reply
#4
FYI: SQLalchemy tutorial on this forum.
Reply
#5
(Jun-01-2023, 05:12 PM)Larz60+ Wrote: FYI: SQLalchemy tutorial on this forum.

Thanks!

I'm looking it over but I don't understand any of it. Wall

I'm learning SQLAlchemy because I need to create/access a database with a FastAPI API, which I'm also learning.
Reply
#6
from command line. please start sqlite3
on command line, type schema catalog
check all column names and lengths, are they correct?
Reply
#7
(Jun-02-2023, 11:06 AM)Larz60+ Wrote: from command line. please start sqlite3
on command line, type schema catalog
check all column names and lengths, are they correct?
Is this what you mean?

sqlite> sqlite> .schema catalog
CREATE TABLE catalog (
        id VARCHAR NOT NULL,
        parent_id VARCHAR,
        request_id VARCHAR,
        test_type VARCHAR,
        target VARCHAR,
        user VARCHAR,
        submitted DATE,
        started DATE,
        ended DATE,
        progress INTEGER,
        state VARCHAR,
        result VARCHAR,
        PRIMARY KEY (id)
);
sqlite>
The table looks good to me.

sqlite> pragma table_info(catalog);
0|id|VARCHAR|1||1
1|parent_id|VARCHAR|0||0
2|request_id|VARCHAR|0||0
3|test_type|VARCHAR|0||0
4|target|VARCHAR|0||0
5|user|VARCHAR|0||0
6|submitted|DATE|0||0
7|started|DATE|0||0
8|ended|DATE|0||0
9|progress|INTEGER|0||0
10|state|VARCHAR|0||0
11|result|VARCHAR|0||0
sqlite>
Reply
#8
So the table is being created properly, just not loaded.
I'll get back.
Reply
#9
So, I found one error that I made, but it still does not resolve the issue...

I was using commit instead of commit() .

I am fixing the sample above now.
Reply
#10
What commands should I be using to query the all rows of the table "catalog"?

I want to stop having to jump between my script and the SQLite prompt to check if the database has been updated.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create SQLite3 database with peewee Jim53_1980 2 688 Dec-20-2023, 02:38 PM
Last Post: buran
  Regex replace in SQLite3 database WJSwan 1 814 Dec-04-2023, 05:55 PM
Last Post: Larz60+
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 765 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,393 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,637 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Python Variables and Sqlite3 Database Staples200 1 3,173 May-25-2021, 02:40 AM
Last Post: Staples200
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,125 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,162 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  sqlite3 database problem Maryan 2 2,494 Oct-05-2020, 05:21 AM
Last Post: buran
  Adding data to a table in SQLite3 djwilson0495 2 3,086 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