0

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
9
  • 1
    What values are you inserting? Shouldn't it be 'INSERT INTO ... VALUES ...'? Commented May 10, 2014 at 20:50
  • ya the insert statement is not valid syntax. s/b e.g insert into sites (col1,col2) values (%s,%s); Commented May 10, 2014 at 20:51
  • @bernie Check my original post, edited it. Commented May 10, 2014 at 20:57
  • @Coder77 You've got to bind your row to the statement. Maybe stackoverflow.com/questions/16506643/… can help you. Commented May 10, 2014 at 21:02
  • @VMai Edited my original post, check it. Commented May 10, 2014 at 21:14

1 Answer 1

1

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]))
Sign up to request clarification or add additional context in comments.

5 Comments

I suppose your file has a headline with column names which you have to skip as inserting these strings to numeric or integer fields is your problem described in your last edit.
Yes, it does have a headline of all the column names. I will try to remove the first line and see what happens.
I am now getting this error: Warning (from warnings module): File "/var/www/seomonster/script.py", line 108 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])) Warning: Data truncated for column 'domainName' at row 1
Check the above comment.
You may provide some sample data in your question. I guess the domain name in your first row is longer than 40 characters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.