0

I'm have some problems and its more then likely something really dumb that i missed and i feel kind bad post something on here about this but if you guys could help it would be nice heres my code

    import MySQLdb
# Open database connection
db = MySQLdb.connect("localhost", "testuser", "123", "test")
# prepare a cursor object using cursor() method
cursor = db.cursor()
# Prepare SQL query to INSERT a record into the database.
number = 300
number1 = 5001



sql = "INSERT INTO PARTS (PART_NUMBER) VALUES (%d)"

try:
    number = 300
    # Execute the SQL command

    cursor.execute(sql,(number,))

    # Commit your changes in the database
    db.commit()
    print("Updated!")
except db.Error, e:
    print "Error %d: %s" % (e.args[0],e.args[1])
    # Rollback in case there is any error
    db.rollback()
    print("was rolled back")

# disconnect
db.close()

after i run it no problems but its not show up in my data base when recall the data back nothing can someone tell me why ? Thanks for your time.

1 Answer 1

3

You should always use %s, even for numbers.

sql = "INSERT INTO PARTS (PART_NUMBER) VALUES (%s)"

Check the first example here: http://mysql-python.sourceforge.net/MySQLdb.html#some-examples . It uses %s to the max_price variable, which is an int.

Quoted from the link:

In this example, max_price=5 Why, then, use %s in the string? Because MySQLdb will convert it to a SQL literal value, which is the string '5'. When it's finished, the query will actually say, "...WHERE price < 5".

Sign up to request clarification or add additional context in comments.

2 Comments

i have done that and well it still not showing up in my data base ?
@user3162030 I don't know why, you have to provide more information about your issue. Your code works here, with the change of this answer.

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.