Python flask-sqlalchemy: Do I have to commit session after a query?

I am writing an app in python flask-sqlalchemy with MySQL DB (https://flask-sqlalchemy.palletsprojects.com/en/2.x/) and I am wondering if I have to make “db.session.commit() or db.session.rollback()” after GET call, which only query DB . For example: Answer orders = Order.query.all() is a SELECT query that could be extended to include additional filters (WHERE etc.). It doesn’t alter the database, it simply reads values from it. You don’t need to commit on a read for precisely that reason – what would you store other than “I just read this data”? Databases are concerned about this in other ways i.e. access permissions and logs.

Python – Connect MySQL through localhost not working but 127.0.0.1 is working?

I just setup a VPS with a Mysql Database on it. I’m able to log into the database in the SSH terminal with mysql -uroot -p however, the python file shown below is unable to form a connection, and errors with Access denied for root@localhost (using password: YES). I’m confused as to why this is happening. I’m 100% sure my password is correct. I’m using mysql.connector Python file: I’m a bit new to this DB and VPS stuff, so please bear with me if I don’t understand on the first go. Thanks! Answer In addition to @don’s answer I assume

Python Database that stays with the program

So mysql works great when you log into your mysql database program on that device but what if you want to move the program to another device, the connected databases wont work… Is there anyway to have the database somehow within the python file, so that when I move it to other devices it will act the same…Thank you! Answer Assuming you want to store some simple information, Sqlite3 does exact what you’re looking for. This is a sql database that is entirely run on a file on the hardrive that the application is running from. But be careful what

Django filter on OneToOne field appending “_id” and failing

In my Django application I have the following model: Note user is a OneToOneField to my CustomUser table and also the primary key of this table. In views.py, I attempt to query this table with Provider.objects.filter(user=request.user) and get django.db.utils.OperationalError: (1054, “Unknown column ‘appname_provider.user_id’ in ‘field list'”). Checking my MySql database, I see the columns are (user, company_id). So why is Django adding “_id” to user when I try to filter? My CustomUser model: Answer Here’s how I solved this issue: Undo all migrations: rm -rf appname/migrations/0* appname/__pycache__ and in mysql: DELETE FROM django_migrations; Delete the Provider table in mysql: DROP

Use python to turn mysql into json

I want to use python to turn mysql into json, but when I use the following code, the result is json line by line, not a whole set of json Answer You need to pull out the json.dumps() line from the for loop. This is resulting in conversion of into json with each iteration and printing in each iteration. Your code should look like this :

How to store column value from mysql to python variable? [SOLVED]

actually i’m new to learning python and mysql database i have table named TABEL_NILAI in database like this: So i want to Retrieve value NILAI_TRUST from TABEL_NILAI and i want to do some math. here’s code i was made: And giving me error like: so how to retrieve value into python variable in right ways. Please help:( Answer The query will return a iterable of tuples, for example like this: So this line has two problems: x is a tuple, so it can’t be called like x() x is not a string, so it doesn’t have a split method It

How to show more objects in my html with django (queries)

I want to show more queries within my html page but it only shows a single object. I do not know how to add more This is in mysql, html, django1.11 and python 2.7.15 views.py i need add object mapa and competencias. The object alumno i dont have problem Answer User filter instead of get. Because, get returns a single object, but filter returns all matching objects. In your template For Alumnao, For mapa, For competencias,

Python and MySQL: is there an alternative to MySQLdb?

Is there a module written purely in Python that will allow a script to communicate with a MySQL database? I’ve already tried MySQLdb without success. It requires too much: GCC, zlib, and openssl. I do not have access to these tools; even if I did, I don’t want to waste time getting them to work together. I’m looking for tools that will make my work easier. Can someone point me in the direction of a MySQL Python module written in Python? If not, tips on writing my own code for communicating with MySQL would be appreciated. Thanks, Tony Thanks everyone