MySQL -force Attribute from PostgreSQL

I am doing bulk inserts of 10,000 records in Postgres:

INSERT INTO TABLE VALUES (v1),...,(v10000)

      

I am doing this via the Python Psycopg2 library. Currently, if one of the tuples is, say, 5000 bombs (for example, if the field is of the wrong type), my entire package insert fails because an exception is thrown. I'm looking for an equivalent to the MySQLsforfor flag that solves exactly this problem; which tells MySQL that it keeps fixing bugs ( http://dev.mysql.com/doc/refman/5.0/en/mysql-command-options.html#option_mysql_force )

I don't see anything here, and this is for an even newer PG, which is quite disturbing: http://www.postgresql.org/docs/9.3/static/sql-select.html

+3


source to share


2 answers


Tell psycopg that you want to use the "autocommit" transaction isolation level, ie. it will not open a transaction for you and each statement will be automatically committed. Note that you will have to catch exceptions and ignore (or better write them). Example:



import psycopg2
import psycopg2.extensions

conn = psycopg2.connect("...")
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
curs = conn.cursor()

for row in your_data:
    try:
        curs.execute("INSERT INTO table VALUES (%s, %s, ...)", row)
    except Exception, err:
        print err

 # no need to commit!

      

+1


source


do it in psql \set ON_ERROR_ROLLBACK on

and try again



0


source







All Articles