Python: executes without errors or warnings, but data is not inserted in SQLite database
Hello, people.
I am learning Python. Right now, I am trying to make a backend using Flask library. I manually created an empty database, defining just the types for each field it has. After that, I manually created some data, which will be used during tests. The execution shows no error, but the database is still empty! Code:
import sqlite3 # Biblioteca de SQLite The browser shows "Inserções no banco feitas e confirmadas.", which is what this function returns. It is also printed in the terminal. So, the functions bdBicicletas.executemany(), conBicicletas.commit() and conBicicletas.close() were successful. But when I examine the database with sqlitebrowser or sqlitestudio, there is not data there. What am I doing wrong? |
Well, in the case of the code you actually posted, the problem is that you commented out the import of constantes and you're still using stuff from it.
I installed flask and Flask-Cors, created the .bancos directory, took out the try...except wrapper, and ran it. This is what I got: Code:
❯ python3 filename.py |
The imported file was pasted, but i forgot to remove the link to use them
My local code has 2 files: insertData.py and constantes.py, with the lines i inserted in the first thread:
Code:
# Para bicicletas: |
The fact the database existed before the code in #1 was executed is important? It will just insert more data, if i refresh the address in the browser, right?
Now, the code in the first thread works to give a running backend. I tested it. |
I was about to ask if maybe you needed a CREATE TABLE statement.
|
Quote:
At first I would recommend to use a linter (like pylint) which will catch a lot of coding issues. From the other hand a missing table or similar error must be reported, so if you can't see any error it is suppressed. |
For this project, I created the database files and the tables inside them, manually. I just did not want to populate them in the same way. But other students created the tables just with sqlite3.connect() function. It is not called "create", and i find this very strange. But it seems to do it, anyway, when the file path passed to it does not exist.
https://docs.python.org/3/library/sqlite3.html is not clear about creating or not the database file. I have that file, and all the columns and types are defined. This is why i only connect to it, insert data, commit and then close. |
As i said, the database is already present when i execute this script the first time, and it has the table inside it, with all defined data types for each column. Maybe I should have given you this file, so I am making this post to give it: https://tmpfile.us/f/trPQYVaw Save this file in a folder named ".bancos" in the folder where the Python code of #1 post is saved.
|
Too much code.
Start by cutting it down to the absolute minimum - e.g. a single simple insert statement to a single-column table - and check the behaviour. http://www.sscce.org/ |
Quote:
Code:
import sqlite3 # Biblioteca de SQLite |
That's still not a minimal example.
The behaviour you're concerned about is sqlite3 related, right? So remove EVERYTHING that is not sqlite3 related, then further minimize the result until you have something that's approximately 6-7 lines long. When you've confirmed a regular minimal un-parameterised insert works then switch it for a parameterised executemany and check if it still works, if it does, progressively restore complexity until you re-encounter the unexpected behaviour. |
Quote:
|
Quote:
I can only tell some general rules: 1. you need to check every command/function if that was successfully executed or failed. As an example: Code:
except: # you must catch the exception and print it. This code will just hide the real error message 2. you need to check all the table/column and other names if they are not misspelled somehow (for example you write into a table and check another one if the data is available). 3. sometimes pylint or other checker can catch strange errors, you may try to use it. |
Quote:
|
An example without Flask
I wrote another reduced example. This one has no Flask squeleton. The call to SQLite inserting data finishes well. But when the database closes, in the "try" codeblock, i call sys.exit() function, and that is what generates an exception (a print() after it is not seen). No output, though.
The new reduced code: Code:
import sqlite3 # Biblioteca de SQLite Code:
$ python min.py |
All times are GMT -5. The time now is 06:15 PM. |