Skip to content

Instantly share code, notes, and snippets.

@vubon
Last active January 1, 2018 14:51
Show Gist options
  • Save vubon/ce6e243cc084c4618437b6d7dcd10964 to your computer and use it in GitHub Desktop.
Save vubon/ce6e243cc084c4618437b6d7dcd10964 to your computer and use it in GitHub Desktop.
Insert csv data PostgreSQL database by Python
Firstly import below modules in Python file or Django file .
1. csv
2. psycopg2
Now Firstly create a connection with your database below way
try:
conn_string="dbname='user database name' user='database user name' host='localhost' password='your user password'"
print ("Connecting to database\n->%s" % (conn_string))
conn = psycopg2.connect(conn_string)
print ("connection succeeded")
except:
print ("no connection to db")
Hope if you insert correctly your database information. It should work.
Now go next step.
Now collect your data from csv file below way . I gave an example below
My CSV data Sctucture was below
Call 43,234234223,2017/08/08 06:47:21
Call 44,234234234,2017/08/08 06:47:26
Call 45,987654443,2017/08/08 06:47:30
Call 46,Ext.301,2017/08/08 07:01:34
with open("your file location", newline='') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
s = []
for row in spamreader:
s.append(row)
s2 = filter(None, s)
ids = []
numbers = []
dateDtime = []
for i in s2:
if i[1] == 'Ext.301':
continue
else:
numbers.append(i[1])
ids.append(i[0])
dateDtime.append(i[2])
idnumber = [i.split(' ')[1] for i in ids]
date = [i.split(' ')[0] for i in dateDtime]
time = [i.split(' ')[1]for i in dateDtime]
t = tuple(zip(idnumber,numbers,date,time))
# check connection
cur = conn.cursor()
cur.executemany("INSERT INTO csv_data (id, call_numbers, call_date,call_time) VALUES (%s,%s, %s, %s)", t);
# Make the changes to the database persistent
conn.commit()
# Close communication with the database
cur.close()
conn.close()
My tuple was look like below. If you try to above data , you must get tuple look like this.
(('43', '234234223', '2017/08/08', '06:47:21'), ('44', '234234234', '2017/08/08', '06:47:26'),('45', '987654443', '2017/08/08', '06:47:30'))
executemany means in a time it will execute many tuple data.
That's all . Thanks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment