Paul Smith wrote: > Early stages messing with module sqlite3 in python3.4. I am successful in > creating sqlite tables of my own and interacting with other sqlite tables, > however in refining the code from a purely "it can do it" stage to a more > stable working piece of code I run into this problem. > > I call the correct sqlite3 items and create table D to which I add or > 'amend' specific information from tables A and Z. > > However since the table is already created the line of code that created > the table subsequently kicks out this error when the program runs again. > > Traceback (most recent call last): > File "C:\Users\Thechives\Desktop\pyweather\AlphaItems\ > weathercodetrial1.2.py", line 206, in <module> > cur.execute('''CREATE TABLE "D" AS SELECT weather_current FROM > "todays_weather" WHERE weather_current IS NOT NULL''') > sqlite3.OperationalError: table "D" already exists > > So how does one handle the error or ignore the creation portion of the > program once the table is created? > > Python exception handling has not helped, Try - Except - although I am > mashing it I am sure. I believe since we are actually engaging sqlite via > python at this point in the code it should be a sqlite error handler or > some such. So simple and yet it eludes my noobile mind.
While you could wrap the cur.execute() in a try except try: cur.execute('''CREATE TABLE "D" ...''') except sqlite3.OperationalError: pass # assume that the table already exists and ignore the error there is a also way to handle this in SQL: cur.execute('''CREATE TABLE IF NOT EXISTS "D" ...''') See also the sqlite3 documentation at <http://www.sqlite.org/lang_createtable.html>. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor