Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
MySQL INSERT Problem
#1
I am trying to insert a row into a table with this code:
query="INSERT INTO Members VALUE ('"+args["first"].value+"','"+args["last"].value+"','"+mac+\
     "','"+args["email"].value+"',false,'','');"
try:
     rows=cur.execute(query)
     print("Success - ",rows)
except (MySQLdb.Error,MySQLdb.Warning) as e:
     print("Error:  [%d] - %s" % (e.args[0], e.args[1]))
I am getting the "Success" output with 1 row. However, when I manually do a "select * from Members" the empty set is returned. Obviously in spite of the row count being 1, nothing was added to the table. I have no clue how to debug this since it looks like a success from Python. Can someone explain how this is possible or how to debug it? TIA.
Reply
#2
do you execute connection commit() after the insert?

https://dev.mysql.com/doc/connector-pyth...ction.html
Quote:Since by default Connector/Python turns autocommit off, and MySQL 5.5 and higher uses transactional InnoDB tables by default, it is necessary to commit your changes using the connection's commit() method.

Note also how to properly construct the INSERT statement
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks. I sure missed that one. It is working now. However, as far as your comment about constructing an insert, it sure looks right to me and it works. What are you seeing that I am not?
Reply
#4
you should use parameterized statements in order to prevent sql injections.

you may also take advantage of using prepared statements (https://dev.mysql.com/doc/connector-pyth...pared.html)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Prepared statements is my normal practice (old PHP programmer). However, everything I searched indicated there was no prepared statements in python mysql and were unnecessary. Based on your comment I now see why the threads I found, indicating prepared statements do not exist in python, inexplicably switched the topic to parameterized queries. Odd that no one mentioned your link before. Thanks again.
Reply
#6
well, I also posted a link to prepared statements in python too :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 680 Oct-03-2023, 10:25 PM
Last Post: lostintime
  openpyxl insert picture problem cools0607 2 1,545 May-03-2023, 06:48 AM
Last Post: cools0607
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 822 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  Mysql error message: Lost connection to MySQL server during query tomtom 6 16,055 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  Problem Using SQL Placeholder In MySQL Query AdeS 11 6,115 Jul-31-2021, 12:19 AM
Last Post: Pedroski55
  Problem updating value in MySQL database dangermaus33 1 1,632 Nov-24-2020, 08:32 PM
Last Post: dangermaus33
  Insert into mysql through python LaKhWaN 0 1,945 Aug-26-2020, 04:54 AM
Last Post: LaKhWaN
  How to insert data if not exists in mysql? farah97 0 2,824 Dec-29-2019, 08:32 AM
Last Post: farah97
  Failed to insert record into MySQL table.Python type tuple cannot be converted farah97 3 21,587 Dec-26-2019, 02:01 PM
Last Post: buran
  How to insert record into MySQL using Phython abhay_kala 1 2,268 Dec-06-2019, 04:34 PM
Last Post: abhay_kala

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020