python sqlite3 update not updating - python

Question: Why is this sqlite3 statement not updating the record?
Info:
cur.execute('UPDATE workunits SET Completed=1 AND Returns=(?) WHERE PID=(?) AND Args=(?)',(pickle.dumps(Ret),PID,Args))
I'm using python and sqlite3. this statement does not throw an error, it just seems like it is out right ignored. for testing reasons I included below it:
cur.execute('SELECT * FROM workunits WHERE PID=(?) AND Args=(?)',(PID,Args))
Which returns a record just fine. but the record doesn't up date with the new value of the pickled ret. it remains u''. I can't figure out why. my where statement seems to work. my syntax seems to be correct because there is no error being thrown. I'm clueless as to why exactly it doesn't work.

If the problem persists after you fixed your syntax. Please make sure you're using:
conn.commit()
After cur.execute, UPDATES and INSERTS require COMMIT.

don't use 'AND', use a ','.
cur.execute('UPDATE workunits SET Completed=1, Returns=? WHERE PID=? AND Args=?',
(pickle.dumps(Ret), PID, Args)
)

Related

Python mysql.connector insert does not work

I work with the Python mysql.connector for the first time and I am not able to create a working insert statement.
This is the table:
'CREATE TABLE IF NOT EXISTS products (id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255));'
I am trying to insert a variable as title while the id should be auto incremented. I have tried multiple solutions but it simply won't work.
def insert_product(title: str):
insert_product_query = 'INSERT INTO products (title) VALUES (%s);'
cursor.execute(insert_product_query, (title,))
This runs without any error, but the insert is not working. It does nothing. I tried multiple versions of this, with '?' instead of '%s' and without a tuple but it won't work.
Another solution I tried is this:
def insert_product(title: str):
insert_product_query = f'INSERT INTO products (title) VALUES (\'{title}\')'
print(insert_product_query)
cursor.execute(insert_product_query)
I printed the insert statement and when I copy paste it directly into the database it works perfectly, so I don't have any idea why it is not working out of the python code as it is not producing any errors.
I found many similar problems but none of the solution worked for me.
I hope someone can help me as I might overlook something obvious.
Thanks in advance!
Python's connector disables autocommit by default (as a reasonable library would do!). You need to explicitly commit after you perform a DML statement:
con.commit() # Assuming con is the name of the connection variable

INSERTING and UPDATE SET issues when handling strings

I've been having this issue for some time now and here is the short story:
I want to edit a date in my Sqlite database with a Python script so I do this using:
import sqlite3
db = sqlite3.connect('test.db')
cursor = db.cursor()
cursor.execute("UPDATE info SET date = '2003-03-14 12:34:20' WHERE id = 10")
but when I execute this I get an exception:
sqlite3.OperationalError: no such module: fts4
with some troubleshooting I've managed to narrow it down to having some issues with the quote marks. Since even if I just reduce it to a single year it still produces an exception.
The same exception appear if I try to insert a string using quote marks.
How can I fix this? Most solutions I find is solved though enabling fts3/fts4 with the SQLITE_ENABLE_FTS3 flag when building, but I have no idea of how to do that.
Besides, the exact same code runs smoothly both in Python 3.6.5 and 2.7.12. what happened in Python3?, or more specifically 3.5.2.
I should add, that it works in linux but not in windows.

Unable to see table after creation in Flask-sqlalchemy [duplicate]

When I make a MySQL table order, it is created successfully but, when I execute any query against it, it says "error 1064 , syntax error".
When I change the name to orders, it works fine.
But I don't want to change the name. How can I execute our query against the order table?
can you use something like?
select * from `order`
The word order is actually an SQL keyword. You would have the same problem if you tried to use a table called group or select. You can fix it is MySQL by using quotes around it, along the lines of:
select f1, f2 from `order` where blah blah blah ...
However, unless your table will only ever hold a single order (in which case it won't do so for long since the underlying business will soon be bankrupt), you should probably call your table orders.
That solves both your problems, the one you found and the one you didn't :-)
I got here because I was searching for similar solution for SQL CE. There using
order
'order'
"order"
doesn't work.
What worked was:
[order]
Maybe it'll help someone else also.
This should fix the problem:
e.g
mysql>
Create table order(
ID char(5),
QTY(3)
)
;

SQL Update with Variable as parameter error

I'v searched throughout this forum and couldn't find an answer.
cur.execute("UPDATE lastran SET job_date=newtimedate WHERE job_name=usrjobname")
Why is my code now working?
The two variables are newtimedate and usrjobname
I don't know Python, but it seems you're passing your variables as literals... a little Googling tells me this should be it?
cur.execute("UPDATE lastran SET job_date=? WHERE job_name=?", (newtimedate, usrjobname))
See how this person does it: How to see the real SQL query in Python cursor.execute

pysqlite and python : wont insert data into table but code executes fine

I am trying to insert some data into a pysqlite database but even tho the code runs fine with no errors nothing shows up in the database and i have made sure that the variable does contain a value
cur = self.con.execute("insert into urllist(url) values('%s')" % seed)
i have double checked the table and column name and they are also correct
Are you calling con.commit() ?
Apparently changes are lost unless this method is used before closing the connection.
http://readthedocs.org/docs/pysqlite/en/latest/sqlite3.html

Categories