0

What is wrong in my code ? I want to add %s to my mysql db.

 titlux = tree.xpath('//*[@id="offer-title"]/h1/text()')
 pretx = tree.xpath('//*[@id="offer-price-stock"]/div[3]/span/@content')

 print "%s," % titlux 
 print "%s," % pretx
 print "\n"
 conn = ..............

 x = conn.cursor()

 try:
    x.execute ("""INSERT INTO produse (titlu, pret) VALUES (%s, %s)""")
    conn.commit()
 except:
    conn.rollback()

 conn.close()
1
  • Please: tell us more! And Don't put bold everywhere! Thanks in advance. Commented Jul 28, 2015 at 18:16

2 Answers 2

1

You're missing the replacement variables and some quotes in your SQL insert. Change it to:

x.execute ("""INSERT INTO produse (titlu, pret) VALUES ("%s", "%s")""" % (titlux[0], pretx[0]))
Sign up to request clarification or add additional context in comments.

4 Comments

Thancks, is worcking but give me the resulst like ['1199.99'], cant i remove [' ']?
That is just Python's output. Your database should show 1199.99. Does it?
No, is give me the result like: "titlu = [u'Laptop Asus X553MA-SX455B cu procesor Intelxae Celeronxae Dual-Core N2840, 2.16GHz, 4GB, 500GB, Intelxae HD Graphics, Microsoft Windows 8.1, Bing, Black']"
Your xpath results obviously return lists. I've updated my answer to select the first item in the list. Don't forget to accept my answer :)
0

@Alastair has the right answer but if you want to see the query you're using

print "INSERT INTO produse (titlu, pret) VALUES (%s, %s)" % (titlux, pretx)
x.execute ("""INSERT INTO produse (titlu, pret) VALUES (%s, %s)""" % (titlux, pretx))

Comments

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.