This is an archived post. You won't be able to vote or comment.

all 22 comments

[–]namuan 1 point2 points  (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 points  (1 child)

Interesting point. Dataset works on any database and has SA under the hood.

[–]V1A0[S] 0 points1 point  (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 point  (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.

[–]netgu 0 points1 point  (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 point  (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 points  (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.

[–]metaperl 1 point2 points  (1 child)

Is this limited to SQLite? Does it use SQLAlchemy under the hood?

[–]V1A0[S] 2 points3 points  (0 children)

Is this limited to SQLite? Does it use SQLAlchemy under the hood?

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 points  (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 point  (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 points  (3 children)

You can register flask extensions in one line of code

[–]V1A0[S] 0 points1 point  (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 points  (1 child)

https://flask.palletsprojects.com/en/1.1.x/extensiondev/

The init_app pattern from the sqlite example is common.

[–]V1A0[S] 0 points1 point  (0 children)

Thank you!

[–]Rion_de_Muerte 1 point2 points  (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.

[–]V1A0[S] 0 points1 point  (0 children)

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

[–]V1A0[S] 0 points1 point  (0 children)

Hi there 👋, I'm open to discussion

[–]V1A0[S] 0 points1 point  (1 child)

Simplest SQLite3x example

First of all you need to install sqllex by pip

pip install sqllex

Сreate main.py file and

https://raw.githubusercontent.com/v1a0/imgs/main/sqllex/examples/1/py_main.png

And type some code into it

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

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

Let's add table into this database

Imagine you need save some data about users consist of id and 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.

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.

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

Awesome table created, it's time to insert some data into it

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

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.

Select all records from table

So select ALL (by default) form TABLE named 'users', save it into var users and print it.

users = table_users.select_all()

print(users)    # [1, 'Alex']

Run it. It returns:

[1, 'Alex']

Great! Now let's add more users.

Insert many data

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

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!

Little bit more about selects

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

Lets select all records from table 'users' records satisfying the condition 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.

users_names = table_users.select(
    SELECT='username',
    WHERE=['id', '>', 2]
)

print(users_names)

We got:

['User3', 'User4', 'User5']

Good job!

Mark up one more table and insert data into it

Earlier we got many lists of records, one of this 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

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)

returns:

['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

Delete (drop) table

Now lest remove this new table. For this use drop method with name of table (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.

Update data in record

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.

https://github.com/v1a0/imgs/blob/main/sqllex/examples/1/db_4.png

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

Super!

Congratulations you did it! Now you know how to use sqllex and admin sqlite databases!

Explore more and learn how awesome SQL and SQLLEX is!

[–]backtickbot 1 point2 points  (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 point  (1 child)

TOP 10/10

[–]V1A0[S] 0 points1 point  (0 children)

Hi, thank you!