To insert null values to the database you have two options:
- omit that field from your INSERT statement, or
- use
None
Also: To guard against SQL-injection you should not use normal string interpolation for your queries.
You should pass two (2) arguments to execute()
, e.g.:
mycursor.execute("""INSERT INTO products
(city_id, product_id, quantity, price)
VALUES (%s, %s, %s, %s)""",
(city_id, product_id, quantity, price))
Alternative #2:
user_id = None
mycursor.execute("""INSERT INTO products
(user_id, city_id, product_id, quantity, price)
VALUES (%s, %s, %s, %s, %s)""",
(user_id, city_id, product_id, quantity, price))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…