我需要一些帮助才能使用Python处理MySQL中的NULL值。这是我的代码:
import MySQLdb
# This function connects to the MySQL database
def db_connect():
try:
db = MySQLdb.connect(host=HOST, user=USER, passwd=PASSWD, db=DATABASE, charset="utf8")
except Exception as e:
print(e)
sys.exit("Can't connect to database")
return db
def store(project_title, description, website, start_date):
try:
cursor = db.cursor()
insert_projects_query = "INSERT INTO projects (title, description, website, start_date) VALUES ('"+project_title+"','"+description+"','"+website+"','"+start_date+"') ON DUPLICATE KEY UPDATE `title`='"+project_title+"'"
cursor.execute(insert_projects_query)
db.commit()
except Exception as e:
print e
我不知道如果字段存储NULL
,例如start_date
的值为“NA”。我可以以某种方式直接将null分配给start_date
,还是应该将其从INSERT查询中排除?数据库中的我的表是以这样的方式创建的,默认情况下这些字段是NULL值。