This question already has answers here:
Sqlite insert query not working with python?
(2 answers)
Closed 6 years ago.
I'm new to python and SQLite, so I apologize if this is a dumb question. I've written the code below to open up a database and delete the data in the STAGING_LIDs table. The script runs, but when I check the DB, the data is still there. Am I doing something wrong?
import sqlite3
import csv
conn = sqlite3.connect('C:\\SQLite\\Budget_Dev.db')
cur = conn.cursor()
#delete all table data
cur.execute("DELETE FROM STAGING_LIDs;")
I'm using bernie's answer in this question (the accepted answer) as a template.
I figured it out. I needed to add a line:
conn.commit()
Related
This question already has an answer here:
SQLite not saving data between uses
(1 answer)
Closed 6 months ago.
I'm exploring sqlcipher3 with python, and I find that after inserting some rows, then closing and re-opening the database, my table is empty. This problem doesn't appear when I use the sqlcipher command-line utility itself.
from sqlcipher3 import dbapi2 as sqlcipher
db = sqlcipher.connect('testing.db')
db.execute('pragma key="testing"')
db.execute("create table people (name text primary key)")
db.execute("insert into people (name) values ('charlie'), ('huey')")
print(db.execute('select * from people').fetchall())
# => [('charlie',), ('huey',)]
db.close()
db = sqlcipher.connect('testing.db')
db.execute('pragma key="testing"')
print(db.execute('select * from people').fetchall())
db.close()
# => []
What have I missed here?
You are supposed to commit your transaction before closing the connection otherwise your transaction is lost : https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.close
This question already has an answer here:
Python sqlite3 parameterized drop table
(1 answer)
Closed 10 months ago.
def refreshDatabase(table):
c.execute("DROP TABLE ?", (table,))
conn.commit()
createNewTable(table)
Hey, how can I drop the Table that is declared as a parameter when calling the function? It doesn't seem to work with this syntax. thanks in advance!
Take a look here
You can't substitute table name at SQL side, so change it from Python (which is not safe of course)
def refreshDatabase(table):
c.execute(f"DROP TABLE {table}")
conn.commit()
createNewTable(table)
This question already has answers here:
Variable table name in sqlite
(9 answers)
Closed 2 years ago.
So, I am new to Stackoverflow and I hope I'm writing this question well. So I'm trying to choose a table from my database (that contains 5 tables) based on user input in python. However I'm not quite sure how to do it. Here is the code:
user_input = "table1"
db.execute("SELECT number FROM (?) WHERE person = 1;")
I'm searching for a way if it is possible. Anyway any help would be appreciated.
Well, after some verifications in order to forbid SQL injections, the easiest way is to format the query string with the user input.
db.execute ("SELECT * FROM MyTable WHERE person = {};".format(user_input))
And the content of user_input would be placed on the curly brackets.
It's not very clear on how you're getting user input, though.
This question already has answers here:
Python: use mysqldb to import a MySQL table as a dictionary?
(5 answers)
Closed 9 years ago.
After executing a query statement on a MySQL database connection, I perform:
rows = cursor.fetchall()
This gives an array of arrays. I'd like to have an array of dictionaries, where each dictionary takes its keys from the requested column names of my table and associates the values from the table.
How do I do this?
Well, you forgot to mention which mysql library you're using.
If using oursql (which I recommend, it is certainly the best one), use oursql's DictCursor. Example:
conn = oursql.connect(...)
curs = conn.cursor(oursql.DictCursor)
If using MySQLdb (why?) Use MySQLdb's DictCursor. Example:
conn = MySQLdb.connect(..., cursorclass=MySQLdb.cursors.DictCursor)
curs = conn.cursor()
Doing that will give you a cursor that returns dicts for each row. Remember to not have duplicate rownames in your query.
This question already has answers here:
Python: use mysqldb to import a MySQL table as a dictionary?
(5 answers)
Closed 9 years ago.
After executing a query statement on a MySQL database connection, I perform:
rows = cursor.fetchall()
This gives an array of arrays. I'd like to have an array of dictionaries, where each dictionary takes its keys from the requested column names of my table and associates the values from the table.
How do I do this?
Well, you forgot to mention which mysql library you're using.
If using oursql (which I recommend, it is certainly the best one), use oursql's DictCursor. Example:
conn = oursql.connect(...)
curs = conn.cursor(oursql.DictCursor)
If using MySQLdb (why?) Use MySQLdb's DictCursor. Example:
conn = MySQLdb.connect(..., cursorclass=MySQLdb.cursors.DictCursor)
curs = conn.cursor()
Doing that will give you a cursor that returns dicts for each row. Remember to not have duplicate rownames in your query.