1

I have list of tuples with data something like this:

list1 = [(1100, 'abc', '{"1209": "Y", "1210": "Y"}'), (1100, 'abc', None)]

def insert_sample_data(col_val):
    cur = self.con.cursor()
    sql = """insert into sampletable values {}""".format(col_val)
    cur.execute(sql)
    self.con.commit()
    cur.close()

values1 = ', '.join(map(str, list1))  #bulk insert
insert_sample_data(values1)

Table Structure: ssid int, name varchar, rules jsonb

When I am trying to insert the data but it throws me an error saying "insert column "none" does not exist". How can we load the data into table with 'Null' or 'None'?

I looked at this solution but it does not help in this case How to insert 'NULL' values into PostgreSQL database using Python?

1
  • 1
    Why does the solution from the linked question not work in this case? Because of the bulk insert? For that you should use executemany or execute_batch if speed is important for you. Commented Sep 18, 2018 at 17:09

1 Answer 1

3

As @shmee states, you need to use something like executemany and parameterize your values instead of using format, which is vulnerable to SQL injection.

I would modify your code as follows:

def insert_sample_data(self, values): # added self since you are referencing it below
    with self.con.cursor() as cur:
        sql = "insert into sampletable values (%s, %s, %s)" # Use %s for parameters
        cur.executemany(sql, values) # Pass the list of tuples directly
        self.con.commit()

list1 = [(1100, 'abc', '{"1209": "Y", "1210": "Y"}'), (1100, 'abc', None)]
self.insert_sample_data(list1) # pass the list directly
Sign up to request clarification or add additional context in comments.

1 Comment

As @shmee said, speed is something really important for my process as there are atleast 100k to 1mil records to be inserted and using executemany takes a lot of time. I made few modifications to your code and it worked like a charm. Thanks @Nicarus

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.