Skip to main content
13 votes
Accepted

Python class to manage a table in SQLite

I exposed the database connection and database cur as instance variables. Is that a good practice? Having a cursor defined as an instance variable is a common practice. The problem though here is that ...
alecxe's user avatar
  • 17.5k
10 votes
Accepted

Searching for a word in a list of tuple extracted from a db via SQL

You could try doing the filtering at the database level. Something like: ...
defy's user avatar
  • 306
10 votes
Accepted

Repository with SQLite in Python

We're using black, isort, ruff, and type checkers -- terrific! Lots of best practices in ...
J_H's user avatar
  • 42.2k
8 votes

Flagging employees whose spending is close to the budgeted amount

There's two ways you can go about this. Stick with your current way, or use SQL more. Current way I'd change your for loop to assign to one value. I'd make a function that tells you if the values ...
Peilonrayz's user avatar
  • 44.6k
8 votes
Accepted

Python update SQLite DB Values

SQL (and thus SQLite) support a variety of operators suitable (amongst others) in UPDATE statements. This means that you can simplify your whole loop into a single statement: ...
301_Moved_Permanently's user avatar
8 votes

School House Points (Python + SQLite)

DRY This repetitive code is an anti-pattern: blueTotal = 0 greenTotal = 0 redTotal = 0 yellowTotal = 0 Please define ...
J_H's user avatar
  • 42.2k
7 votes
Accepted

Python simple Class with sqlite and unit testing

This is an interesting question. Thanks for posting it! IMO, you don't actually have a User class here. A User would be a system ...
aghast's user avatar
  • 12.6k
7 votes
Accepted

Optimizing the insertion of 400 000 records in sqlite

Other than @PavloSlavynskyy's correct recommendation to add indices: Whereas autocommit is off by default for the Python library, I doubt your code is doing what you think it's doing because as soon ...
Reinderien's user avatar
  • 71.1k
7 votes
Accepted

Database schema for an upcoming comment hosting system

deps, topo sort order The FK DAG induces a topological order across various DB objects, such as tables. Consider writing these in a different order: ...
J_H's user avatar
  • 42.2k
7 votes

Repository with SQLite in Python

Your code can benefit from a TypedDict or two. Consider this assignment: ...
InSync's user avatar
  • 499
6 votes
Accepted

Flagging employees whose spending is close to the budgeted amount

I would say being a small code, you can change a couple things to make it a bit more readable Specially the part with the conditionals, is a bit hard to follow ...
A. Romeu's user avatar
  • 1,123
6 votes

Searching for a word in a list of tuple extracted from a db via SQL

You should remove the re.compile from the second for loop, this will save some time. You only have to compile once per word in ...
Ludisposed's user avatar
  • 11.8k
6 votes

School House Points (Python + SQLite)

Here are some hints on how to decrease the size of your code and make it more Pythonic. Stack Ifs Anytime you see stacked ifs, that are all basically the same, you should consider using a ...
Stephen Rauch's user avatar
6 votes
Accepted

Mapping named parameters to indices for PreparedStatement

Name of the class doesn't indicate its purpose. The class is called "ParameterMap" but its use-case (filling in values in a query) doesn't need to know about it having a map somewhere inside ...
QuasiStellar's user avatar
  • 2,327
6 votes

Repository with SQLite in Python

Since I use SqlAlchemy with SQLite, here is my personal fix using SqlAlchemy events, basically these are hooks. My models.py looks like this: ...
Kate's user avatar
  • 8,313
5 votes

Python update SQLite DB Values

Mathias’ answer is the correct way of solving your problem but since this is a code review site, let’s look at the rest of your code. First off, your code style is fundamentally tidy and clear, so ...
Konrad Rudolph's user avatar
5 votes

Optimizing the insertion of 400 000 records in sqlite

Indexes! Tables basically has array-like access: to find something, an engine should check all the values in a table. If you have a big table, this can be slow. But there's a solution: indexes. ...
Pavlo Slavynskyy's user avatar
5 votes
Accepted

Searchable database with Java and SQL

Block form over statement form if (!dBfile.exists()) createNewTable(); This is a risky pattern. Say you (or someone else, perhaps a Python ...
mdfst13's user avatar
  • 22.4k
4 votes

Python class to manage a table in SQLite

To allow for more flexibility, I would give the user the ability to set the database location: ...
Graipher's user avatar
  • 41.7k
4 votes

Unwinding shortened URLs with Python, SQLite, and multithreading

__ There is liitle need for the __. A single _ should suffice to make people clear it's a ...
Maarten Fabré's user avatar
4 votes
Accepted

Simple one time pad cipher

The hard problems for one-time pads are randomly generating the pad, and securely sharing the pad between the participants (and nobody else). The first problem is not solved, because the ...
Toby Speight's user avatar
  • 88.3k
4 votes
Accepted

Uploading songs from website to database and then to Spotify

Some simple suggestions after a first look at scraper.py: class Song defines a method called ...
aghast's user avatar
  • 12.6k
4 votes
Accepted

OOP bank account program in Python 3

Some suggestions: Running this code through a linter such as flake8 will give you some hints towards producing more idiomatic code. You can also use Black to automatically format your code to be more ...
l0b0's user avatar
  • 9,117
4 votes

RAII wrapper for SQLite transactions

There's not a lot of code here to be reviewed, but I'll have a go. A small efficiency gain is possible, by moving the db_ argument in the initializer list, rather ...
Toby Speight's user avatar
  • 88.3k
4 votes
Accepted

Add as newest ID to SQLite Database

Really you don't need DR to be stored as it's a user convenience. That is you can add DR when extracting the value. You also don't need to check calculate the next number as SQlite can do this on your ...
MikeT's user avatar
  • 211
4 votes

Gold Price Tracker Web Scraper using Python

General There are so, so many examples of people scraping stock tracker sites. For beginners it's an understandable urge: you can see the data on the web, and you want to be able to translate those ...
Reinderien's user avatar
  • 71.1k
4 votes
Accepted

decorator to execute sqlite statement

It's cool that you are experimenting, but from my point of view this is just trying to be fancy for no good reason. How is this better than just executing the code safely at end of your ...
K.H.'s user avatar
  • 2,728
4 votes

Mapping named parameters to indices for PreparedStatement

commenting var index = 1 // <-- JDBC starts counting with 1 Thank you for the one-origin reminder, that's a helpful comment. ...
J_H's user avatar
  • 42.2k
4 votes

Database schema for an upcoming comment hosting system

In table users, there is a role field which I suspect may not really be used at present. But it could make sense to add one more ...
Kate's user avatar
  • 8,313

Only top scored, non community-wiki answers of a minimum length are eligible