Im doing:
try:
conn = sqlite3.connect('bags_of_coins.db')
print('Db Creation Successful')
except:
print('Db Creation NOT Successful')
try:
conn.execute('''CREATE TABLE bags
(ID INTEGER PRIMARY KEY,
bag TEXT NOT NULL,
);''')
print('Table Creation Successful')
except:
print('Table Creation NOT Successful')
try:
conn.execute("INSERT INTO bags (bag) \
VALUES ('test')");
conn.commit()
except:
print('Insert NOT Successful')
#finally.
conn.close()
But it keeps outputting:
Db Creation Successful
Table Creation NOT Successful
Insert NOT Successful
Does anyone see anything i'm doing wrong? I was following this guide but I cant see to spot the issue. Thanks.
You have comma before ) in this place:
CREATE TABLE bags
(ID INTEGER PRIMARY KEY,
bag TEXT NOT NULL, <- here
);
delete it.
Try this:
import sqlite3
conn = None
try:
conn = sqlite3.connect('bags_of_coins.db')
print('Db Creation Successful')
except:
print('Db Creation NOT Successful')
try:
with conn:
conn.execute("CREATE TABLE bags (ID INTEGER PRIMARY KEY, bag TEXT NOT NULL);")
print('Table Creation Successful')
cursor = conn.execute("INSERT INTO bags (bag) VALUES ('test')")
conn.commit()
print("Insert Successful" if cursor.rowcount > 0 else "Insert NOT Successful")
except:
print('Table Creation NOT Successful')
Your error was creating the table.
The 'with' is a plus I added, it takes care of closing the connection when the block finish by any reason so you don't need the 'finally' block.
Don't hesitate to ask if you have any doubt.
Blindly banging out try..except without showing the error from the exception is just going to cause you grief and heartache, use the exception available to list the specific error.
import sqlite3
try:
conn = sqlite3.connect('bags_of_coins.db')
except sqlite3.Error as e:
print('Db Creation NOT Successful', str(e))
mycursor = conn.cursor()
try:
mycursor.execute("CREATE TABLE bags (ID INTEGER PRIMARY KEY,bag TEXT NOT NULL)")
except sqlite3.Error as e:
print("Table creation failed", str(e))
try:
mycursor.execute("insert into bags (bag) values (?)", (['test']))
conn.commit()
except sqlite3.Error as e:
print("table insert failed", str(e))
#finally.
conn.close()
Related
I am trying to insert data into my database using psycopg2 and I get this weird error. I tried some things but nothing works. This is my code:
def insert_transaction():
global username
now = datetime.now()
date_checkout = datetime.today().strftime('%d-%m-%Y')
time_checkout = now.strftime("%H:%M:%S")
username = "Peter1"
connection_string = "host='localhost' dbname='Los Pollos Hermanos' user='postgres' password='******'"
conn = psycopg2.connect(connection_string)
cursor = conn.cursor()
try:
query_check_1 = """(SELECT employeeid FROM employee WHERE username = %s);"""
cursor.execute(query_check_1, (username,))
employeeid = cursor.fetchone()[0]
conn.commit()
except:
print("Employee error")
try:
query_check_2 = """SELECT MAX(transactionnumber) FROM Transaction"""
cursor.execute(query_check_2)
transactionnumber = cursor.fetchone()[0] + 1
conn.commit()
except:
transactionnumber = 1
""""---------INSERT INTO TRANSACTION------------"""
query_insert_transaction = """INSERT INTO transactie (transactionnumber, date, time, employeeemployeeid)
VALUES (%s, %s, %s, %s);"""
data = (transactionnumber, date_checkout, time_checkout, employeeid)
cursor.execute(query_insert_transaction, data)
conn.commit()
conn.close()
this is the error:
", line 140, in insert_transaction
cursor.execute(query_insert_transaction, data) psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, commands ignored until end of transaction block
The error message means that one of the preceding SQL statements has resulted in an error. If an exception occurs while executing an SQL statement you need to call the connection's rollback method (conn.rollback()) to reset the transaction's state. PostgreSQL will not permit further statement execution otherwise.
Ideally you want to record the actual error for later analysis, so your code ought to be structured like this:
try:
cursor.execute(sql, values)
conn.commit()
except Exception as e:
print(f'Error {e}')
print('Anything else that you feel is useful')
conn.rollback()
I have a query that I intend to insert with a try-except block, so that I can ignore all the queries that fail a certain constraint i.e. IntegrityError.
with sql_engine.begin() as cn:
sql = """INSERT INTO table1 (m_id, pick_on)
SELECT t.m_id, t.pick_on
FROM temp_table t
WHERE NOT EXISTS
(SELECT 1 FROM table1 f
WHERE (t.m_id = f.m_id
OR (t.m_id IS NULL AND f.m_id IS NULL))
AND t.pick_on = f.pick_on)"""
try:
cn.execute(sql)
except:
print("Unexpected error:", sys.exc_info()[0])
print(sql)
pass
but this does not insert all the records to table1. why?
I'm pretty new to python but I have created a small script to insert rows in postgres, and a table that stores stock prices.
The table have a unique constraint on date and ticker, to ensure only one price per ticker per day is inserted. I want the script to skip inserts and continue on with the rest, but I cannot figure out how to make the loop continue when the exception block is triggered.
The Python script is as follows:
def createConnection(db="db", user="john", password='doe', host='host', port=5432):
conn = psycopg2.connect(
database=db, user=user, password=password, host=host, port=port)
return conn
def insertNewPrices(df):
conn = createConnection()
cur = conn.cursor()
for row in df.head().itertuples(index=False):
try:
print(row)
cur.execute(
"INSERT INTO daily_price (price_date, ticker, close_price) VALUES (%s, %s,
%s)", row)
except psycopg2.IntegrityError as e:
print("something went wrong")
print(e)
continue
conn.commit()
conn.close()
error raised:
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "daily_price_ticker_price_date_key"
DETAIL: Key (ticker, price_date)=(EQUINOR, 1990-02-28) already exists.
You have the insert statement outside the try statement. Can you remove the insert from outside and you should be OK.
for row in df.head().itertuples(index=False):
#cur.execute(
# "INSERT INTO daily_price (price_date, ticker, close_price) VALUES (%s, %s, %s)",
row)
try:
print(row)
cur.execute(
"INSERT INTO daily_price (price_date, ticker, close_price) VALUES (%s, %s,
%s)", row)
except psycopg2.IntegrityError, psycopg2.errors.UniqueViolation) as e:
print("something went wrong")
print(e)
Also you don't need continue at the end of the except statement as its the last line.
Instead of checking for specific errors, you can also catch all errors and warnings using the below:
except (psycopg2.Error, psycopg2.Warning) as e:
My code functions. When running, it displays "Error %s % e" but when I go to postgresql the data doesn't upload there in anyway whatsoever. What should I do?
#!/usr/bin/python
# -*- coding: utf-8 -*-
import psycopg2
import sys
con = None
try:
con = psycopg2.connect("host='localhost' dbname='test123' user='postgres' password='XXX'")
cur = con.cursor()
cur.execute("CREATE TABLE Products(Id INTEGER PRIMARY KEY, Name VARCHAR(20), Price INT)")
cur.execute("INSERT INTO Products VALUES(1,'Milk',5)")
cur.execute("INSERT INTO Products VALUES(2,'Sugar',7)")
cur.execute("INSERT INTO Products VALUES(3,'Coffee',3)")
cur.execute("INSERT INTO Products VALUES(4,'Bread',5)")
cur.execute("INSERT INTO Products VALUES(5,'Oranges',3)")
con.commit()
except psycopg2.DatabaseError as e:
if con:
con.rollback()
print ("Error %s % e")
sys.exit(1)
finally:
if con:
con.close()
To start, your code should be formatted like this:
#!/usr/bin/python
-- coding: utf-8 --
import psycopg2
import sys
con = None
try:
con = psycopg2.connect("host='localhost' dbname='test123' user='postgres' password='XXX'")
cur = con.cursor()
cur.execute("CREATE TABLE Products(Id INTEGER PRIMARY KEY, Name
VARCHAR(20), Price INT)")
cur.execute("INSERT INTO Products VALUES(1,'Milk',5)")
cur.execute("INSERT INTO Products VALUES(2,'Sugar',7)")
cur.execute("INSERT INTO Products VALUES(3,'Coffee',3)")
cur.execute("INSERT INTO Products VALUES(4,'Bread',5)")
cur.execute("INSERT INTO Products VALUES(5,'Oranges',3)")
con.commit()
except psycopg2.DatabaseError as e:
if con:
con.rollback()
print ("Error %s % e")
sys.exit(1)
finally:
if con:
con.close()
if you're receiving the message "Error ..." then your code is not working as you expect it to. You should break it up into different sections or else manually run these commands in the Python terminal to debug where the issue is. - This is not really an answer, but hopefully we can work this one out since your question has not got enough information for an answer.. try running the following and see which error you hit:
#!/usr/bin/python
-- coding: utf-8 --
import psycopg2
import sys
try:
con = psycopg2.connect("host='localhost' dbname='test123' user='postgres' password='XXX'")
cur = con.cursor()
except Exception:
print("Failed to open a connection")
sys.exit(1)
try:
cur.execute("CREATE TABLE Products(Id INTEGER PRIMARY KEY, Name
VARCHAR(20), Price INT)")
cur.execute("INSERT INTO Products VALUES(1,'Milk',5)")
cur.execute("INSERT INTO Products VALUES(2,'Sugar',7)")
cur.execute("INSERT INTO Products VALUES(3,'Coffee',3)")
cur.execute("INSERT INTO Products VALUES(4,'Bread',5)")
cur.execute("INSERT INTO Products VALUES(5,'Oranges',3)")
con.commit()
except Exception as e:
print("Failed to insert data into the database")
sys.exit(1)
I insert items using psycopg2 in the following way:
cursor = connection.cursor()
for item in items:
try:
cursor.execute(
"INSERT INTO items (name, description) VALUES (%s, %s) RETURNING id",
(item[0], item[1])
)
id = cursor.fetchone[0]
if id is not None:
cursor.execute(
"INSERT INTO item_tags (item, tag) VALUES (%s, %s) RETURNING id",
(id, 'some_tag')
)
except psycopg2.Error:
connection.rollback()
print("PostgreSQL Error: " + e.diag.message_primary)
continue
print(item[0])
connection.commit()
Obviously, when an item is already in the database, the duplicate key exception is being thrown. Is there a way to ignore the exception? Is the whole transaction is going to be aborted when the exception is thrown? If yes, then what is the best option to rewrite the query, maybe using batch inserting?
from Graceful Primary Key Error handling in Python/psycopg2:
You should rollback transaction on error.
I've added one more try..except..else construction in the code bellow
to show the exact place where exception will occur.
try:
cur = conn.cursor()
try:
cur.execute( """INSERT INTO items (name, description)
VALUES (%s, %s) RETURNING id""", (item[0], item[1]))
except psycopg2.IntegrityError:
conn.rollback()
else:
conn.commit()
cur.close()
except Exception , e:
print 'ERROR:', e[0]