我已成功连接到python中的数据库,但它没有保存我提供的数据。这是代码:
try:
connection = MySQLdb.connect(host="localhost",user="root",passwd="",db="traffic_alert")
except Exception as e:
print('Not connected')
cursor = connection.cursor()
cursor.execute("insert into data (main_status,extracted_info,time) values (%s,%s,%s)",[sentence,final_result,time])
print(sentence)
print(final_result)
print(time)
它可以成功打印句子,final_result和时间,但不能将它们保存在database.sentence,final_result和时间少于50个字符。我已经在mysql中用这些行手动创建了数据库:
id -> auto_increment,pk
main_status -> varchar(255)
extracted_info -> varchar(255)
seen -> int(10),Null=True
time ->varchar(255)
答案 0 :(得分:1)
connection.commit()
在 cursor.execute