Mysql Database Insert with Python via MySQLdb - python

This is super basic, but I cannot seem to get it to work correctly, most of the querying I've done in python has been with the django orm.
This time I'm just looking to do a simple insert of data with python MySQLdb, I currently have:
phone_number = toasted_tree.xpath('//b/text()')
try:
#the query to execute.
connector.execute("""INSERT INTO mydbtable(phone_number) VALUES(%s) """,(phone_number))
conn.commit()
print 'success!'
except:
conn.rollback()
print 'failure'
conn.close()
The issue is, it keeps hitting the except block. I've triple-checked my connection settings to mysql and did a fake query directly against mysql like: INSERT INTO mydbtable(phone_number) VALUES(1112223333); and it works fine.
Is my syntax above wrong?
Thank you

We can't tell what the problem is, because your except block is catching and swallowing all exceptions. Don't do that.
Remove the try/except and let Python report what the problem is. Then, if it's something you can deal with, catch that specific exception and add code to do so.

Related

pypyodbc does not commit

I have a SQLServer TSQL query that has multiple INSERT statements that range from pretty basic to somewhat complex.
This query works in SQLServer Management Studio.
When I use Python pypyodbc package and run the script, the script runs but does not commit. I have tried with and without the commit() function.
BUT if I specify a SELECT statement at the end, the script commits the inserts.
So it's all good because it works, but I am putting an inapplicable SELECT statement at the end of all of my scripts.
Does anyone have any ideas how I can get these to commit without the SELECT statement at the end? I do not want to split the queries up into multiple queries.
Thank you!
def execute_query(self,
query,
tuple_of_query_parameters,
commit=False,
return_insert_id=False,
return_results=True):
self.open_cursor()
try:
self.connection_cursor.execute(query,
tuple_of_query_parameters)
result_set = None
if return_results:
if return_insert_id:
result_set = self.connection_cursor.fetchone()[0]
else:
result_set = self.connection_cursor.fetchall()
if commit:
self.connection_cursor.commit()
except pypyodbc.Error as e:
print('Check for "USE" in script!!!')
raise
finally:
self.close_cursor()
return result_set
Try this:
self.connection_cursor.execute(query,
tuple_of_query_parameters)
if commit:
self.connection_cursor.commit() #put commit here, immediately after execute
I think that will do the trick.

Python cx_oracle insert error

I am trying to insert records from csv into oracle 11g database using python script.
Primarily the application successfully insert some record, but later throws this exception Error <class 'cx_Oracle.DatabaseError'> .
def orcl_proc(sql):
# Open database connection
db = cx_Oracle.connect('username/password#localhost/XE')
# prepare a cursor object using cursor() method
cursor = db.cursor()
try:
# Execute the SQL command
cursor = cursor.execute(sql)
# Commit your changes in the database
db.commit()
except cx_Oracle.DatabaseError as e:
# Log error as appropriate
error, = e.args
print('Error.code =', error.code)
print('Error.message =', error.message)
print('Error.offset =', error.offset)
# Rollback in case there is any error
db.rollback()
# disconnect from server
db.close()
#print('Closed')
Error:
<class 'cx_Oracle.DatabaseError'>
Out of 56,367 records the python application was only able to insert 180 records. Can any body help me please, thanks in advance.
Remove exception handler from your Python script. Let Oracle database propagate it and you'll know what exactly caused the error, seeing its Oracle error code and message text.
Then, if you aren't sure what these mean, come back here and post how Oracle responded.
An intermittent "ORA-12516: TNS:listener could not find available handler with matching protocol stack" is likely to when the rate of connections to the DB is higher than the DB is configured to cope with. Try bumping the 'processes' parameter of the DB. If you need help with that, see the section "Configuring the Database For Testing" in the free PDF http://www.oracle.com/technetwork/topics/php/underground-php-oracle-manual-098250.html
I actually found the answer. Thanks to LittleFoot and Christopher Jones.
After removing the exception handler, I got ORA-12516: TNS:listener could not find available handler with matching protocol stack, I have to increase the database process and session.
alter system set processes=1000 scope=spfile;
alter system set sessions=2248 scope=spfile;
startup
and it worked.
Thanks

Python problems with error handling (try, except)

I'm new to programming and trying to work out the error handling at the moment.
But i keep running into the same problem. When I find an error i want to rerun the script again. The problem is, if you enter a good input after the first mistake, it still sees it as a bad input. please help me out.
def new_user_name()
print "Choose a Username"
username = input_str()
try:
data = lite.connect(database)
dat = data.cursor()
dat.execute("INSERT INTO Users('User_Name') VALUES(?)", username);
dat.rollback()
return username
except:
print "The username %s is already in use" % username
time.sleep(2)
new_user_name()
Can someone help me out, or link a nice tutorial about errorhandling?
It would me help out alot
dat.rollback() (if this is a valid syntax, didn't check but looks close) should be in the except section and not in the try section.
Notice that you should be better of if you initiliazed the database connection outside the function (so you won't have to do it every function call) or at least outside the try section.
dat.execute("INSERT INTO Users('User_Name') VALUES(?)", username);
This line is the problem. The second argument should be a tuple, not a string, so it's raising a ValueError. Because you're catching all errors instead of just sqlite3.IntegrityError (which would be raised if you tried to insert a duplicate primary key), you will always end up in the except block.
Never use catch-all except blocks if you can avoid it.
Python has a great official documentation, links are below:
v 2.7 http://docs.python.org/2/tutorial/errors.html
v 3.3 http://docs.python.org/3.3/tutorial/errors.html
Please notice you must never use except: without Exception class, it is a bad practice.

Why my sql doesn't execute?

I have a python script that needs to update a database information.
So, in my init() method, I start the connection. But when I call
the update method, the script does not give me any answer, it seems
like it is into an infinite loop.
def update(self,id,newDescription):
try:
sql="""UPDATE table SET table.new_string=:1 WHERE table.id=:2"""
con=self.connection.cursor()
con.execute(sql,(newDescription,id))
con.close()
except Exception,e:
self.errors+=[str(e)]
What I've tried so far:
Change the query, just to see if the connection is alright. When I did that (I used 'SELECT info from table'), the script worked.
I thought that my query was wrong, but when I execute it in SQLDeveloper
program, it goes right.
What can be happening?
Thanks
You are forgetting to call commit.
Not sure about how to do it in python script, but i think you need to call a "commit" before closing connection. Otherwise oracle rollsback your transaction.
Try adding con.commit() before close()

Trapping MySQL Warnings In Python

I would like to catch and log MySQL warnings in Python. For example, MySQL issues a warning to standard error if you submit 'DROP DATABASE IF EXISTS database_of_armaments' when no such database exists. I would like to catch this and log it, but even in the try/else syntax the warning message still appears.
The try/except syntax does catch MySQL errors (eg, submission of a typo like 'DRP DATABASE database_of_armaments').
I have experimented with <<except.MySQLdb.Warning>> -- no luck. I've looked at the warnings module, but don't understand how to incorporate it into the try/else syntax.
To be concrete, how do I get the following (or something like it) to work.
GIVEN: database 'database_of_armaments' does not exist.
try:
cursor.execute('DROP DATABASE IF EXISTS database_of_armaments')
except: <<WHAT DO I PUT HERE?>>
print 'There was a MySQL warning.'
<<AND what goes here if I want to get and manipulate information about the warning?>>
UPDATE:
Thanks for the comments. I had tried these and they didn't work -- but I had been using a DatabaseConnection class that I wrote for a connection, and its runQuery() method to execute. When I created a connection and cursor outside the class, the try/except Exception caught the "Programming Error", and except MySQLdb.ProgrammingError worked as advertised.
So now I have to figure out what is wrong with my class coding.
Thank you for your help.
Follow these steps.
Run it with except Exception, e: print repr(e).
See what exception you get.
Change the Exception to the exception you actually got.
Also, remember that the exception, e, is an object. You can print dir(e), e.__class__.__name__, etc.to see what attributes it has.
Also, you can do this interactively at the >>> prompt in Python. You can then manipulate the object directly -- no guessing.
Have you tried something like this?
try:
cursor.execute(some_statement)
except MySQLdb.IntegrityError, e:
# handle a specific error condition
except MySQLdb.Error, e:
# handle a generic error condition
except MySQLdb.Warning, e:
# handle warnings, if the cursor you're using raises them
I think the exception you want to catch is a MySQLdb.ProgrammingError, and to get information about it, just add a variable to store the error data (a tuple) in after that i.e:
try:
cursor.execute('DROP DATABASE IF EXISTS database_of_armaments')
except MySQLdb.ProgrammingError, e:
print 'There was a MySQL warning. This is the info we have about it: %s' %(e)
I managed to trap the mysql warning like this:
import _mysql_exceptions
try:
foo.bar()
except _mysql_exceptions.Warning, e:
pass
first you should turn on warnings to treat as exceptions, and only then you can catch them.
see "error" warnings filter - https://docs.python.org/3/library/warnings.html#the-warnings-filter
Plain and simple
def log_warnings(curs):
for msg in curs.messages:
if msg[0] == MySQLdb.Warning:
logging.warn(msg[1])
cursor.execute('DROP DATABASE IF EXISTS database_of_armaments')
log_warnings(cursor)
msg[1] example :- (u'Warning', 1366L, u"Incorrect integer value: '\xa3' for column 'in_userid' at row 1")

Categories