2013-10-10 4 views
0

스크립트를 만드는 가장 좋은 방법은 CSV (n 열 포함)를 가져 와서 sqlite db에 추가하는 것입니다. sqlite - python에 여러 변수 삽입

내가 지금까지 무엇을 가지고 - (? 그는이 아닌, 발신자가 알고 가정된다) 확실한 솔루션을 구성 할 열 수를 확인하는 것입니다

def export_to_db(): 
    conn = sqlite3.connect(config.DB_NAME) 
    cur = conn.cursor() 
    table_name = config.TABLE_NAME 
    with open(config.IMPORT_FILENAME, 'rb') as csvfile: 
     tablerows = csv.reader(csvfile, delimiter=',') 
     header = True # make this configurable 
     for row in tablerows: 
      if header: 
       header = False 
      else: 
       cur.execute("INSERT INTO <tablename> VALUES(?, ?, ?, ?)", tuple(row)) 
       # I want the number of columns to be same as teh number of columns in CSV.    
    conn.commit() 
    cur.execute("SELECT COUNT(*) from people") 
    print str(cur.fetchone()[0]) + " rows added!" 
    conn.close() 

답변

0

다음 삽입 문을 작성 이 정보를 바탕으로 이를 수행하는 방법을 모르는 경우 문자열 형식에 대한 FineManual의 부분을 읽으십시오.