Inserting CSV data into MySQL database - "You have an error in your SQL syntax; near ' ' at line 1

StackOverflow https://stackoverflow.com/questions/23586157

Domanda

This is the exact error message I am getting:

(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1")

And here is my code.

def mysql_connect():
    conn = MySQLdb.connect(host = "localhost",
                           user = "root",
                           passwd = "********",
                           db = "seomonster")
    cursor = conn.cursor()

    filename = 'tdnam_all_listings.csv'
    filename2 = 'tdnam_all_listings2.csv'
    filename3 = 'tdnam_all_listings3.csv'

    try:
        table_create = """CREATE TABLE sites (domainName CHAR(40),
itemID INT,auctionType CHAR(40),
timeLeft CHAR(40),
price CHAR(20),
bids INT,
domainAge INT,
traffic INT,
valuationPrice CHAR(40))"""
        cursor.execute(table_create)

    except Exception as e:
        print e


    try:
        csv_data = csv.reader(file(filename))
        for row in csv_data:
            cursor.execute("""INSERT INTO sites(domainName,itemID,auctionType,timeLeft,price,bids,domainAge,traffic,valuationPrice)""")
    except Exception as e2:
        print e2

    conn.commit()
    cursor.close()
    conn.close()

I do not understand the error at all, although I assume it has something to do with the SQL syntax I am using by inserting the data?

@bernie Changed my code to this:

cursor.execute("""INSERT INTO sites(domainName,itemID,auctionType,timeLeft,price,bids,domainAge,traffic,valuationPrice) values (%s%s,%s,%s,%s,%s,%s,%s,%s);""")

Now getting this error:

(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s%s,%s,%s,%s,%s,%s,%s,%s)' at line 1") 

@VMai I followed your advice and changed my code to this:

for row in csv_data:
    insert = """INSERT INTO sites (domainName,itemID,auctionType,timeLeft,price,bids,domainAge,traffic,valuationPrice) values (%s,%s,%s,%s,%s,%s,%s,%s,%s);"""
    cursor.execute(insert,(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8]))

I am getting a long error which I assume has something to do with the %s's some of the columns's are integers that is why I think. Error:

Warning (from warnings module):
  File "/var/www/seomonster/ZeroPain.py", line 109
    cursor.execute(insert,(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8]))
Warning: Incorrect integer value: ' ItemID' for column 'itemID' at row 1

Warning (from warnings module):
  File "/var/www/seomonster/ZeroPain.py", line 109
    cursor.execute(insert,(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8]))
Warning: Incorrect integer value: ' Bids' for column 'bids' at row 1

Warning (from warnings module):
  File "/var/www/seomonster/ZeroPain.py", line 109
    cursor.execute(insert,(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8]))
Warning: Incorrect integer value: ' Domain Age' for column 'domainAge' at row 1

Warning (from warnings module):
  File "/var/www/seomonster/ZeroPain.py", line 109
    cursor.execute(insert,(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8]))
Warning: Incorrect integer value: ' Traffic' for column 'traffic' at row 1

Warning (from warnings module):
  File "/var/www/seomonster/ZeroPain.py", line 109
    cursor.execute(insert,(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8]))
Warning: Data truncated for column 'domainName' at row 1
È stato utile?

Soluzione

I suppose your only missing one comma after your first query parameter %s as you have 9 fields to fill but only 8 parameters in the end. First and second %s will be concatenated because of the missing comma between them.

Second, as already stated in the comments, pass your arguments as second parameter to this function.

Try this:

cursor.execute("""INSERT INTO sites (domainName,itemID,auctionType,timeLeft,price,bids,domainAge,traffic,valuationPrice) values (%s,%s,%s,%s,%s,%s,%s,%s,%s);""", (row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8]))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top