I'm using python's builtin sqlite3 DB module.
While inserting objects to my DB tables, following sqlite exception raised:
"PRIMARY KEY must be unique"
As there are different insert methods for each object, I can't say for sure in which table does it failed:
import sqlite3
...
class SomeObject1:
....
def _insert_some_object1(self, db_object):
self._cursor.execute('insert into %s values (?,?,?)' % TABLE_NAME,
(db_oject.v1, db_object.v2, db_object_v3,))
Exception got caught in main() by except Exception as e:, so it's only info I've got.
I would want to know in which table insertion failed, value that failed, etc...
What's the right way to get the most info from sqlite exceptions?
Thanks
I think this really all depends on what you are using to connect to the database. Each module will display different errors.
I personally use sqlalchemy, and it gives you detailed errors. Here is an example to show what I mean (note: this is just an example, I personally do not support inline sql):
import sqlalchemy
connection = sqlalchemy.create_engine('sqlite:///mydb.db')
cursor = connection.connect()
query = "INSERT INTO my_table (id, name) values(1, 'test');"
cursor.execute(query)
And the error that is returned:
sqlalchemy.exc.IntegrityError: (IntegrityError) PRIMARY KEY must be unique "INSERT INTO my_table (id, name) values(1, 'test');" ()
As far as core sqlite3 module, I don't believe it will show the query that was executed. If you don't use a module such as sqlalchemy, then you will need to handle and show the error yourself. Take a look at this as example:
import sqlite3
def execute(query):
try:
conn = sqlite3.connect('mydb.db')
c = conn.cursor()
c.execute(query)
conn.commit()
except Exception as err:
print('Query Failed: %s\nError: %s' % (query, str(err)))
finally:
conn.close()
execute("INSERT INTO my_table (id, name) values(1, 'test');")
And the output on error:
Query Failed: INSERT INTO weapon (id, name) values(1, 'test');
Error: PRIMARY KEY must be unique
I have seen some code like (sqlite3 in python)
try:
conn = sqlite3.connect('mydb.db')
c = conn.cursor()
c.execute(query)
conn.commit()
except sqlite3.Error as err:
print('Sql error: %s' % (' '.join(err.args)))
print("Exception class is: ", err.__class__)
Related
I am attempting to INSERT INTO using a correct SQL query and Python's PyMySQL package. I know it is correct since the same query, when doing a Copy/Paste into mySQL workbench, inserts it all right!
However, using Python's pymysql package and cursor.executemany() it fails silently, and returns as rowcount minus 1 (-1).
Here is the piece of code:
ins_query = "REPLACE INTO `household`.`expenses`(`ID`,`Date`,`Sum`,`Class`,`Description`,`Necessity`) VALUES (NULL,'2021-12-23', 800, 'Groceries','Test-3',NULL )"
# ------------------------------
cursor = self.conn.cursor()
try:
cursor.executemany(ins_query)
except pymysql.Error as e:
print("Error %d: %s" % (e.args[0], e.args[1]))
return False
self.conn.commit()
return cursor.rowcount
When I run this function some error occurs, but I have no idea what's wrong.
def insertVariblesIntoTable(newepc, newtimestamp):
try:
connection = mysql.connector.connect(host='localhost',
database='myDB',
user='root',
password='root')
cursor = connection.cursor()
mySql_insert_query = """INSERT INTO myTB(epc,time_stamp)
SELECT newepc,newtimestamp
FROM dual
WHERE NOT EXISTS (SELECT * FROM myTB
WHERE epc=newepc
AND time_stamp>NOW()-INTERVAL 1 MINUTE )"""
cursor.execute(mySql_insert_query)
connection.commit()
print("Record inserted successfully into myTB table")
except mysql.connector.Error as error:
print("Failed to insert into MySQL table {}".format(error))
finally:
if (connection.is_connected()):
cursor.close()
connection.close()
# print("MySQL connection is closed")
error:
Failed to insert into MySQL table 1054 (42S22): Unknown column 'newepc' in 'field list'
As far as I can tell, you're trying to access a column named "newepc" which doesn't exist.
This happens, because you're not escaping the variable "newepc" in your select query, mySql doesn't know it should be a variable.. you can try f-Strings or just normal concatination.
try:
conn = psycopg2.connect("dbname='test1' user='postgres' host='localhost' password='123'")
cur = conn.cursor()
cur.execute("""INSERT INTO product_info (product_name) VALUES (%s)""", 'xxx')
except:
print "error happens"
The above is my code snippet, I have no trouble connecting to the database, but I have some problem inserting the value into it.
I execute the same query in postgres and it works, so i think it's a syntax problem.
Can someone show me what is the right way to do insertion?
cur.execute("""
insert into product_info (product_name) VALUES (%s)
""", ('xxx',))
conn.commit()
Notice that the value is passed to the method wrapped in an iterable.
def fillblast(sequentie, titel_lijst, score_lijst, e_lijst, iden_lijst, pos_lijst, gaps_lijst):
conn = mysql.connector.connect(host = "ithurtswhenip.nl", user = "pg2", password = "pg2", database= "pg2", port= "3307")
cursor = conn.cursor()
Blast = 1000
for i in range(0,len(titel_lijst)):
Blast =+ 2
cursor.execute("INSERT INTO `pg2`.`Blast` (`Blast_id`, `Blast_seq`, `Blast_titel`, `Blast_score`, `Blast_E`, `Blast_gaps`, `Blast_pos`, `Blast_iden`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s);", (Blast, sequentie[i] ,titel_lijst[i], score_lijst[i], e_lijst[i], iden_lijst[i], pos_lijst[i], gaps_lijst[i]))
print("1 record toegevoegd")
cursor.commit()
cursor.close()
conn.close()
I get the following error:
AttributeError: 'MySQLCursor' object has no attribute 'commit'
How does it come, and where does it go wrong?
I try to connect with MySQLWorkbench.
EDIT:
Now I get the following error:
mysql.connector.errors.DatabaseError: 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
Because you can not commit a cursor! you must commit the connection.
# cursor.commit() --> This is wrong!
conn.commit() # This is right
Check the docs...
While fixing some legacy code (that apparently hasn't been working for a couple of years, so users stopped trying to use it), we ran into the same error, using the MySQL-python package in Django. Using the suggestions on this and other answers however resulted in a different error, on account of it occurring within the Django ORM:
django.db.transaction.TransactionManagementError: This code isn't
under transaction management
So for those who run into this error after using conn.commit() instead of cursor.commit(), you may be able to use enter_transaction_management and leave_transaction_management (note that this was for Django 1.4.6 and MySQL-python 1.2.5; I may have to update this once we get the Django upgrade completed):
try:
conn.enter_transaction_management()
cursor = conn.cursor()
cursor.execute(sql)
conn.commit()
except DatabaseError as e:
cursor.rollback()
log.warning('log warning here')
# Handle other exceptions here.
finally:
if cursor:
cursor.close()
conn.leave_transaction_management()
I've got psycopg2 running and I can successfully query tables of my database. This is a working example of querying the table my_table:
import psycopg2
try:
conn_string="dbname='my_dbname' user='user' host='localhost' password='password'"
print "Connecting to database\n->%s" % (conn_string)
conn = psycopg2.connect(conn_string)
print "connection succeeded"
except:
print "no connection to db"
cur = conn.cursor()
try:
cur.execute(""" SELECT * from my_table; """)
records = cur.fetchall()
cur.close()
except:
print "Query not possible"
Question: How can I query a view, let it be called my_view, within the same database my_dbname?
The same way you'd query a table. From a SELECT point of view, a VIEW is the exact same thing as a TABLE:
cur.execute("SELECT * from my_view")
Note that you generally do not want to use a black except:. Catch a specific exception if you have to, but you are usually better off not catching the exception at all rather than block all feedback on errors as you've done here.