Postgresql python

Postgresql python

Postgresql python
Page content

Code

#!/usr/bin/env python

from psycopg2 import connect
from random import randrange
from sys import argv

insertNu = argv[1]

# create a connection
conn = connect(database="testdb", user="postgres", password="", host="localhost", port="5432")
print "Connection is OK."

# data to insert
nameList = ["Paul", "Allen", "Teddy", "Alice", "bob", "jack", "leslie", "cheung", "jay"]
cityList = ["California", "Beijing", "Chendu", "Silicon Valley", "Lisbon", "Moby", "Heihe", "Neimeng", "Heilj"]

cur = conn.cursor()
for i in range(5001, int(insertNu)):
    cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES ("+ str(i + 1) +", \'"+ str(nameList[randrange(len(nameList))]) +"\', "+ str(randrange(20, 40)) +", \'"+ str(cityList[randrange(len(nameList))]) +"\', "+ str(float(randrange(20000,40000,3000))) +" )")

conn.commit()
print "Inserted."
conn.close()