Python Load csv file to Oracle table
Question: I'm a python beginner. I'm trying to insert records into a Oracle table from a
csv file. csv file format : Artist_name, Artist_type, Country . I'm getting
below error:

    
    
    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/hr@localhost: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()
    

When I try to read the file it is working correctly.

    
    
        ##### 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)
    


Answer: Put quotes around the values:

    
    
    sqlquery="INSERT INTO ARTIST_DIM VALUES (%d,'%s','%s','%s')" %(x,row['Artist_name'],row['Artist_type'],row['Country'])
    

Without the quotes it translates to:

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

