2016-10-07 8 views
0

私はPythonの初心者です。私はcsvファイルからOracleテーブルにレコードを挿入しようとしています。 csvファイル形式:Artist_name、Artist_type、Country。私は以下のエラーが発生しています:Python csvファイルをOracleテーブルにロード

Error: File "artist_dim.py", line 42, in <module> 
    cur.execute(sqlquery) 
cx_Oracle.DatabaseError: ORA-00917: missing comma 


    import cx_Oracle as cx 
    import csv 
    import sys 

    ##### Step 1 : Connect to Oracle Database######### 
    conn_str=u'hr/[email protected]:1521/PDBORCL' 
    conn= cx.connect(conn_str) 
    cur=conn.cursor() 
    ####################################### 
    #### Step 2: FETCH LATEST ROW ID FROM ARTIST_DIM### 
    query="SELECT nvl(max(row_id)+1,1) from artist_dim" 
    cur.execute(query) 
    rownum=cur.fetchone() 
    x=rownum[0] 

    with open('D:\python\Artist.csv') as f: 
     reader=csv.DictReader(f,delimiter=',') 
     for row in reader: 
      sqlquery="INSERT INTO ARTIST_DIM VALUES (%d,%s,%s,%s)" %(x,row['Artist_name'],row['Artist_type'],row['Country']) 
        cur.execute(sqlquery) 
      x=x+1 

    conn.commit() 

ファイルを読み込もうとすると正しく動作しています。値の前後

##### Just to Read CSV File############################ 
    with open('D:\python\Artist.csv') as f: 
     reader = csv.DictReader(f, delimiter=',') 
     for row in reader: 
      a="row_id %d Artist : %s type : %s Country : %s " %(x,row['Artist_name'],row['Artist_type'],row['Country']) 
      print(a) 
      x=x+1 
    print(row['Artist_name'],",",row['Artist_type'],",",row['Country']) 

Also, when I try to insert using hard coded values it is working 

    sqlquery1="INSERT INTO ARTIST_DIM VALUES (%d,'Bob','Bob','Bob')" %x 
    cur.execute(sqlquery1) 
+0

あなたが作業しているprint文からの出力を含めることはできますか? –

+0

私はテーブルArtist_dimをチェックし、レコードは挿入されます: - 18、\tボブ\tボブ\tボブ – Leo

答えて

1

入れの引用:引用符なし

それが翻訳:

sqlquery="INSERT INTO ARTIST_DIM VALUES (1, Bob, Bob, Bob)" 
+0

ありがとうRichardson。出来た。 – Leo

+0

素晴らしい!あなたは答えを受け入れることができますか? :) –

+0

オッケー..それは目盛りですか?私はこのフォーラムにはとても新しいです。 – Leo

関連する問題