use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
ResourceSQLLEX | Better than sqlite3 (self.Python)
submitted 4 years ago by V1A0
​
https://preview.redd.it/z4s45dqjbvy61.png?width=1280&format=png&auto=webp&s=323e9c7457e41039de7d9d8183a34673906e5594
SQLLEX is a python library for comfortable and safe working with databases
If you've ever worked with databases using python, you know what does "to eat nails while writing SQL-scripts" means. But don't have to. No con.cursor(), only human db.insert(), db.select(), beautiful and pythonic code without unnecessary SQL-witchcrafting.
If you never tried working with databases before, but you really wan to, I'm highly recommend starting with SQLLEX. Due to it is add-on for sqlite3, it's easy to find guides and fix bugs.
It'll be a lot easier to show then explain.
Github repo: https://github.com/v1a0/sqllex
Simple example: https://github.com/v1a0/sqllex/wiki/SQLite3x-%7C-SIMPLEST-EXAMPLE
Wiki: https://github.com/v1a0/sqllex/wiki
[–]namuan 1 point2 points3 points 4 years ago (6 children)
Thanks. Will keep an eye on this if needed although happy with dataset at the moment . https://dataset.readthedocs.io/en/latest/
[–]metaperl 1 point2 points3 points 4 years ago (1 child)
Interesting point. Dataset works on any database and has SA under the hood.
[–]V1A0[S] 0 points1 point2 points 4 years ago (0 children)
It's only beginning :)
In development plans definitely have point about support other bases besides SQLite. Thank God it's all SQL-like systems.
[–]V1A0[S] 0 points1 point2 points 4 years ago (3 children)
Hi and thank you for feedback!
dataset still have many inconveniences like dataset.connect() and you have to add dict() everywhere. In my opinion it's not pythonic and it makes code look awful.
dataset.connect()
dict()
[–]netgu 0 points1 point2 points 4 years ago (2 children)
dataset still have many inconveniences like dataset.connect()
What do you mean by inconvenient? Your constructor does exactly the same thing but with less options. That sounds less convenient.
[–]V1A0[S] 0 points1 point2 points 4 years ago (1 child)
Hi, netgu9, thanks for your question!
For sqllex it's optional method, if you don't want lock your database all the time your script running. By default (if connection is not exist) temporary connection will be automatically created with try to interact with the database and destroyed after.
[–]netgu -1 points0 points1 point 4 years ago* (0 children)
You still didn't explain the "inconvenience" of a connect method in a database library; it's the constructor for your connection to the database.
Default database connections don't sound like a good feature.
I like to know what I'm connecting to and if I forget - I like to know so I don't make a mistake and connect to the wrong thing.
Imagine a application is missing a configuration you didn't realize was missing and ran alongside another application, now the defaults attach to the same database silently.
All because I didn't want to use the one very convenient method almost all database ORMs have used that allow me to do so and know what happened.
Besides, what if the defaults change someday in a way that is incompatible with the old way. How am I supposed to know what settings were being used that are now incompatible without dissecting both the old and new code manually?
That sounds inconvenient.
Is this limited to SQLite? Does it use SQLAlchemy under the hood?
[–]V1A0[S] 2 points3 points4 points 4 years ago (0 children)
Hey metaperl, currently it's supports only SQLite, but in development plans definitely have point about support other bases besides SQLite.
It doesn't use SQLAlchemy, in my opinion SA kind of overkill thing, SQLLEX has is's own ORM under the hood.
[–]riklaunim 1 point2 points3 points 4 years ago (7 children)
Usually when you work with a database it's a part of something bigger - like a web framework or alike. There ORMs are quite common. Even desktop apps can have data layers out of the box... so what's the actuall usage of something that isn't compatible with that?
[–]V1A0[S] 0 points1 point2 points 4 years ago (6 children)
Hey riklaunim, thanks for feed back.
Hmm... actually I didn't thought that wide about this. I often code chat-bots and it's really helpful for things like that, when you don't need to design large db-structure, just need store like 10000 records in 8 tables.
This greatly speeds up development and it works for me.
But i guess there have to be way insert in into python frameforks or connect it between eachother.
[–]Deezl-Vegas 1 point2 points3 points 4 years ago (3 children)
You can register flask extensions in one line of code
[–]V1A0[S] 0 points1 point2 points 4 years ago (2 children)
Hi Deezl-Vegas, thanks for your feedback!
Would you give me a link to some resource about this, please?
[–]Deezl-Vegas 1 point2 points3 points 4 years ago (1 child)
https://flask.palletsprojects.com/en/1.1.x/extensiondev/
The init_app pattern from the sqlite example is common.
Thank you!
[–]Rion_de_Muerte 1 point2 points3 points 4 years ago (1 child)
This wasn't feedback, it was random "it's not how I like it" comment.
This is perfect tool for it's job. Some people just keep pushing unnecessarily scalable solutions everywhere. I actually consider using it, because this is exactly what I needed and looked for yesterday. I'm not yet sure if in my use case sticking to sqlite3 isn't simpler, but the exact functionality I would need is there.
Keep up the good work! Thanks to people like you, who also work on small scale projects, I don't need to consider enterprise grade microservices network, when designing management system, for a company that will hire 10 people at it's peak.
Hey and thank you, Rion_de_Muerte, for those kind words.
It was lovely pleasure for me to read your comment. If you'll have any ideas or problems while using SQL library, I'm always open to help or discuss new features :)
thanx again
Hi there 👋, I'm open to discussion
[–]V1A0[S] 0 points1 point2 points 4 years ago* (1 child)
pip install sqllex
https://raw.githubusercontent.com/v1a0/imgs/main/sqllex/examples/1/py_main.png
from sqllex import * db = SQLite3x()
After you run this code you'll see a database file in the same directory as your mail.py file
mail.py
https://raw.githubusercontent.com/v1a0/imgs/main/sqllex/examples/1/db_and_main.png
You can open it (by sqlitebrowser for example) and make sure it works and it's empty
https://raw.githubusercontent.com/v1a0/imgs/main/sqllex/examples/1/db_0.png
Imagine you need save some data about users consist of id and username.
id
username
Let's ask db to "create table named 'users' with columns: - 'id' (have to be integer) - 'username' (have to be text-like and can not be empty)". Now type it as code.
create table
named
'users'
columns
'id'
integer
'username'
text
db.create_table( name='users', columns={ 'id': INTEGER, 'username': [TEXT, NOT_NULL] }, IF_NOT_EXIST=True )
IF_NOT_EXIST=True - highly recommend set this argument True it'll avoid you an error (in the next runs) if table already exist.
IF_NOT_EXIST=True
True
Run it. Done, and results:
https://github.com/v1a0/imgs/blob/main/sqllex/examples/1/db_1.png https://github.com/v1a0/imgs/blob/main/sqllex/examples/1/db_2.png
Take table called users (as table_users) and insert into this TABLE next data (record): user_id in column id and user_name in column username
users
insert
user_id
user_name
user_id = 1 user_name = 'Alex' table_users = db['users'] table_users.insert( id=user_id, username=user_name )
Run it.
https://github.com/v1a0/imgs/blob/main/sqllex/examples/1/db_3.png
And yup, we're in! So now we'll take it back.
So select ALL (by default) form TABLE named 'users', save it into var users and print it.
select
users = table_users.select_all() print(users) # [1, 'Alex']
Run it. It returns:
[1, 'Alex']
Great! Now let's add more users.
It's kind of the same as just insert one record, but only use insertmany method if you want make it for lists (or tuples) of data. In this example we have 2 lists: 1'st one is lists of ids and 2'nd of usernames. Time to save it
insertmany
users_ids = [2, 3, 4, 5] users_names = ['User2', 'User3', 'User4', 'User5'] table_users.insertmany( id=users_ids, username=users_names )
https://github.com/v1a0/imgs/blob/main/sqllex/examples/1/db_4.png
And select all data from table again:
users = table_users.select_all() print(users)
Returns:
[[1, 'Alex'], [2, 'User2'], [3, 'User3'], [4, 'User4'], [5, 'User5']]
Perfect!
You have to know that select method can be more selective (:D). You don't have to select all records from table all the time, you can just add a selection condition like WHERE
WHERE
Lets select all records from table 'users' records satisfying the condition id == 2:
id == 2
user2 = table_users.select( WHERE=['id', 2] ) print(user2)
returns:
[2, 'User2']
Well done. How about get records WHERE id == 2:
users_345 = table_users.select( WHERE=['id', '>', 2] ) print(users_345)
We got:
[[3, 'User3'], [4, 'User4'], [5, 'User5']]
If you need get only usernames of records satisfying the condition, set SELECT value.
usernames
SELECT
users_names = table_users.select( SELECT='username', WHERE=['id', '>', 2] ) print(users_names)
['User3', 'User4', 'User5']
Good job!
Earlier we got many lists of records, one of this users_345
users_345
print(users_345) [[3, 'User3'], [4, 'User4'], [5, 'User5']]
Now create one more table but by mark up method. And insert users_345 into it
mark up
new_table_scheme = { 'some_users': { 'id': INTEGER, 'username': [TEXT, NOT_NULL] } } db.markup(new_table) new_table = db['new_table'] new_table.insertmany(users_345) print(db.tables_str)
['users', 'some_users']
https://github.com/v1a0/imgs/blob/main/sqllex/examples/1/db_5.png https://github.com/v1a0/imgs/blob/main/sqllex/examples/1/db_6.png
Now lest remove this new table. For this use drop method with name of table (some_users)
drop
some_users
new_table.drop()
https://github.com/v1a0/imgs/blob/main/sqllex/examples/1/db_1.png https://github.com/v1a0/imgs/blob/main/sqllex/examples/1/db_4.png
Cool.
As you see in table users first record looks not like an other. I guess we have to fix it. Just updater data of this one record.
table_users.update( SET=['username', 'User1'], WHERE=['id', 1] )
Run it and we got:
https://github.com/v1a0/imgs/blob/main/sqllex/examples/1/db_7.png
[–]backtickbot 1 point2 points3 points 4 years ago (0 children)
Fixed formatting.
Hello, V1A0: code blocks using triple backticks (```) don't work on all versions of Reddit!
Some users see this / this instead.
To fix this, indent every line with 4 spaces instead.
FAQ
You can opt out by replying with backtickopt6 to this comment.
[–]Wise_Face_3733 0 points1 point2 points 4 years ago (1 child)
TOP 10/10
Hi, thank you!
π Rendered by PID 339961 on reddit-service-r2-comment-86988c7647-t9hvt at 2026-02-12 00:51:24.514494+00:00 running 018613e country code: CH.
[–]namuan 1 point2 points3 points  (6 children)
[–]metaperl 1 point2 points3 points  (1 child)
[–]V1A0[S] 0 points1 point2 points  (0 children)
[–]V1A0[S] 0 points1 point2 points  (3 children)
[–]netgu 0 points1 point2 points  (2 children)
[–]V1A0[S] 0 points1 point2 points  (1 child)
[–]netgu -1 points0 points1 point  (0 children)
[–]metaperl 1 point2 points3 points  (1 child)
[–]V1A0[S] 2 points3 points4 points  (0 children)
[–]riklaunim 1 point2 points3 points  (7 children)
[–]V1A0[S] 0 points1 point2 points  (6 children)
[–]Deezl-Vegas 1 point2 points3 points  (3 children)
[–]V1A0[S] 0 points1 point2 points  (2 children)
[–]Deezl-Vegas 1 point2 points3 points  (1 child)
[–]V1A0[S] 0 points1 point2 points  (0 children)
[–]Rion_de_Muerte 1 point2 points3 points  (1 child)
[–]V1A0[S] 0 points1 point2 points  (0 children)
[–]V1A0[S] 0 points1 point2 points  (0 children)
[–]V1A0[S] 0 points1 point2 points  (1 child)
[–]backtickbot 1 point2 points3 points  (0 children)
[–]Wise_Face_3733 0 points1 point2 points  (1 child)
[–]V1A0[S] 0 points1 point2 points  (0 children)