MySQLdb not inserting... and i do have cursor.commit()
I apologize in advance for the long code, but it might be relevant. It's also easier that making new code to demonstrate. I can DROP the table using phpMyAdmin, run this script, then go back to phpMyAdmin and see that it created the table. However, the table is empty, and this script should populate with a test row.
import MySQLdb
def makeTable():
dbInfo = { 'username':'livetaor_atowrw', 'password':'~HIDDEN~', \
'server':'~HIDDEN~.com', 'base':'livetaor_towing', \
'table':'inventory' }
try:
sql = MySQLdb.connect(user=dbInfo['username'], \
passwd=dbInfo['password'], \
host=dbInfo['server'], db=dbInfo['base'])
cursor = sql.cursor ()
cursor.execute ("SELECT VERSION()")
cursor.execute ("""
CREATE TABLE inventory (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
itemNumber VARCHAR(24),
itemDescription VARCHAR(255),
itemCategory VARCHAR(24),
itemVendor VARCHAR(48),
itemVendorItemNumber VARCHAR(24),
itemCost FLOAT,
itemMarkup FLOAT,
item4Customers BOOL,
itemOnReplenishment BOOL,
itemReplenishAlert INT,
itemBolivarQuantity INT,
itemLamarQuantity INT,
itemSpringfieldQuantity INT )
""")
cursor.execute ("""
INSERT INTO invento开发者_如何学Gory (
itemNumber,
itemDescription,
itemCategory,
itemVendor,
itemVendorItemNumber,
itemCost,
itemMarkup,
item4Customers,
itemOnReplenishment,
itemReplenishAlert,
itemBolivarQuantity,
itemLamarQuantity,
itemSpringfieldQuantity,
) VALUES (
'TestItemNumber001',
'fictitious item description',
'TestCategory',
'Scripted Vendor',
'ITEM:maketable.py',
'1.00',
'1.33',
'1',
'1',
'6',
'65613',
'64759',
'65802'
)
""")
cursor.commit()
cursor.close()
sql.close()
except MySQLdb.Error, e:
error = "Error %d: %s" % (e.args[0], e.args[1])
confirm = None
confirm = raw_input('Do you know what you are doing? ')
if confirm == 'YeS':
makeTable()
else:
print "Didn't think so. Now, it's probably best to forget about this file."
There are a few guesses as to what the problem with your code is, but I'm going to tackle your implicit, but unsaid, question instead.
That question reads as follows:
My program runs, does not crash, and yet my table is empty. Why is that?
That is a slightly different question, and the reason it doesn't crash is because you explicitly told it not to:
See this code?
except MySQLdb.Error, e:
error = "Error %d: %s" % (e.args[0], e.args[1])
This catches your exception, and places the exception message and other bits into an error variable.
However, unless you inspect that variable, and act upon its contents, you're effectively swallowing the exception.
Remove that bit and re-execute your program, and you'll find the real reason your program fail.
Personally I would guess it is the extra comma in your SQL, after listing up all the column names, here:
itemLamarQuantity,
itemSpringfieldQuantity, <-- here
) VALUES (
'TestItemNumber001',
'fictitious item description',
Unless Im losing my marbles.
Your insert shows 13 columns and 12 values.
Should it not be not commit on the cursor?
cursor.close()
sql.commit()
sql.close()
精彩评论