Error message in python-mysql course: 1054 unknown column "x" in the "list of fields"

This is my first post! I started programming too, so please carry me!

I am trying to load a bunch of CSV files into a database so that I can later run various reports on the data. I started by creating several tables in mysql with the appropriate field names and data types for what will be loaded into the tables. I manipulate the file name (to parse the date to use as a field in my table) and clear the data using python.

So my problem right now (haha ...) is that I get this error message when I try to query "Insert Into" in mysql.

Traceback (most recent call last):  
File "C:\Program Files\Python\load_domains2.py", line 80, in <module>  
cur.execute(sql)
File "C:\Program Files\Python\lib\site-packages\MySQLdb\cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "C:\Program Files\Python\lib\site-packages\MySQLdb\connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
OperationalError: (1054, "Unknown column 'a1200e.com' in 'field list'")

'a1200e.com' refers to a specific domain name that I insert in this column. My request is as follows:

sql="""INSERT INTO temporary_load
    (domain_name, session_count, search_count, click_count,
    revenue, revenue_per_min, cost_per_click, traffic_date)
    VALUES (%s, %d, %d, %d, %d, %d, %d, %s)""" %(cell[0],
                                                int(cell[1]),
                                                int(cell[2].replace (",","")),
                                                int(cell[3].replace(",","")),
                                                float(cell[4].replace("$","")),
                                                float(cell[5].replace("$","")),
                                                float(cell[6].replace("$","")),
                                                parsed_date)

    cur.execute(sql)

I am completely new to this, so I am sure that my code is not at all effective, but I just wanted to lay out everything so that it was clear to me. I do not understand that I guaranteed that my data types are correctly defined in my table (corresponding to those specified in my query). Is there something I'm missing? I tried to do this for a while and don’t know what might be wrong: /

Thank you very much! Val

+5
source share
2 answers

Thomas , as usual, is absolutely correct: feel free to have MySQLdb handle quotation marks.

:

  • csv - .
  • MySQLdb , PEP 249.
    ? , type, MySQLdb (, %s). MySQLdb , SQL-.
    , MySQLdb .
  • . .

MySQL .csv:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import csv
import MySQLdb
import os

def main():
    db = MySQLdb.connect(db="mydb",passwd="mypasswd",) # connection string

    filename = 'data.csv'
    f = open(filename, "rb") # open your csv file
    reader = csv.reader(f)
    # assuming the first line of your csv file has column names
    col_names = reader.next() # first line of .csv file
    reader = csv.DictReader(f, col_names) # apply column names to row values

    to_db = [] # this list holds values you will insert to db
    for row in reader: # loop over remaining lines in .csv file
        to_db.append((row['col1'],row['col2']))
    # or if you prefer one-liners
    #to_db = [(row['col1'],row['col2']) for row in reader]
    f.close() # we're done with the file now

    cursor = db.cursor()
    cursor.executemany('''INSERT INTO mytable (col1,col2) 
                    VALUES (%s, %s)''', to_db) # note the two arguments
    cursor.close()
    db.close()

if __name__ == "__main__":
    main()
+2

DB-API SQL-:

sql = """INSERT INTO temporary_load
    (domain_name, session_count, search_count, click_count,
    revenue, revenue_per_min, cost_per_click, traffic_date)
    VALUES (%s, %d, %d, %d, %d, %d, %d, %s)"""
args = (cell[0],
        int(cell[1]),
        int(cell[2].replace (",","")),
        int(cell[3].replace(",","")),
        float(cell[4].replace("$","")),
        float(cell[5].replace("$","")),
        float(cell[6].replace("$","")),
        parsed_date)
cur.execute(sql, args)

DB-API , ( ).

+1

All Articles