Importing a csv into sql using python using mac

Question:

I cant seem to get this code to work. It is a relatively small csv file. The terminal responds with an error stating there is an issue with the sql syntax. I think one issue may be macs default use of numbers. I would just like to load this file into sql with two columns and all of the respective data. The database and table has already been created in SQL.

import pymysql 
import csv
import time

with open('code1.csv') as file:
    reader = csv.reader(file, delimiter=',')
    conn = pymysql.connect(host='', port=, user='', passwd='', db='', autocommit=True) 
    cur = conn.cursor(pymysql.cursors.DictCursor)

    line = 0
    for row in reader:
        if line == 0:
            line +=1
        else:
            sql = "INSERT INTO name_project('Code', 'Definition')VALUES(%s,%s);"
            cur.execute(sql, (row[0], (str(row[1]))))
cur.close()
conn.close()
Asked By: Brandon

||

Answers:

If you want to do it this way, there are 1 or 2 syntax errors:

  1. Missing a empty space before ‘VALUES’
  2. Unless your are using dialect such as Postgres, 'Code',
    'Definition' may ought to be
    [Code], [Definition]or just plain
    Code, Definition`

Additionally, you may want to consider using pandas to read the whole csv file as a DataFrame using .read_csv, then write to your db with .to_sql.

Answered By: Indominus

First off, general advice in such situations is to check what the query that you’re passing is, i.e. print the query, check whether it’s syntax matches an sql query(you’d be surprised how much we overlook things).

Next run this sql query in the relevant sql server, this might also help in filtering your errors. Later look for syntactical errors.

Answered By: Sumukha Pk

This solution from chatgpt:

import pymysql
import csv
import time

with open(‘code1.csv’) as file:
reader = csv.reader(file, delimiter=’,’)

# Move database connection and cursor initialization inside the loop
for row in reader:
    conn = pymysql.connect(host='', port=, user='', passwd='', db='', autocommit=True) 
    cur = conn.cursor(pymysql.cursors.DictCursor)

    if row[0] != 'Code':  # Skip header row
        sql = "INSERT INTO `name_project` (`Code`, `Definition`) VALUES (%s, %s);"
        cur.execute(sql, (row[0], row[1]))

    cur.close()
    conn.close()
Answered By: Jpj
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.