2015-07-22 56 views
0

我正在嘗試逐行遍歷csv並使用session.add函數將數據插入到特定的postgres數據庫和表中。如何將session.add()指向特定表格

import csv 
from sqlalchemy import create_engine 
from sqlalchemy.orm import sessionmaker 
from sqlalchemy.engine.url import URL 

# Define database 
DATABASE = { 
    'drivername': 'postgresql', 
    'host': 'localhost', 
    'port': '5432', 
    'username': 'postgres', 
    'password': 'password', 
    'database': 'postgres' 
    } 

# Connect to the database 
return engine = create_engine(URL(DATABASE)) 

# Sessionmaker returns a class 
session = sessionmaker(bind=engine)() 

# Open csv 
r = csv.reader(open("E:\Testcsv.csv")) 

# Skip headers 
r.next() 

end = False 
while end == False: 
    try: 
     next = r.next() 
      tableupdate = (
      var_oid = next[0], 
      var_trip_id = next[1], 
      var_route_id = next[2], 
      var_vehicle_id = next[3], 
      var_position_latitude = next[4], 
      var_position_longitude = next[5], 
      var_timestamp = next[6] 
      ) 

    except StopIteration: 
     end = True 

如何使用session.add()函數將數據添加到名爲「vehicle_positions」已定義的表完成這個代碼。

回答

相關問題