Я вставляю записи в таблицу maria db из файла, используя python. Входной файл имеет заголовок. Столбцы Population и Avg_Height в файле частично пусты. Я также хочу, чтобы go было нулевым значением в таблице. Столбец заполнения в таблице установлен как bigint (20) и может принимать нулевое значение. Я пробую следующий код -
Table Definition -
CREATE TABLE `local_db`.`table_x` (
`Unique_code` varchar(50) NOT NULL,
`city` varchar(200) DEFAULT NULL,
`state` varchar(50) DEFAULT NULL,
`population` bigint(20) DEFAULT NULL,
`Avg_Height` double DEFAULT NULL,
`Govt` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
try:
connection = mysql.connector.connect(host='localhost',
database='local_db',
user='root',
password='root',
port = '3306')
input_file = "input_file"
csv_data = csv.reader(open(input_file))
next(csv_data)
cursor = connection.cursor()
for row in csv_data:
cursor.execute("""
INSERT INTO table_x(Unique_code,city,state,population,Avg_Height,Govt)
VALUES(%s,%s,%s,%s,%s,%s)
ON DUPLICATE KEY UPDATE city = VALUES(city),state = VALUES(state), \
population = VALUES(population),Avg_Height = VALUES(Avg_Height),Govt = VALUES(Govt)""")
connection.commit()
print(cursor.rowcount, "Record inserted successfully into table_x")
cursor.close()
except mysql.connector.Error as error:
print("Failed to insert record into table_x table {}".format(error))
finally:
if (connection.is_connected()):
connection.close()
print("MySQL connection is closed")
Но я получаю ошибку ниже -
Failed to insert record into table_x table 1366 (22007): Incorrect integer value: '' for column `local_db`.`table_x`.`population` at row 1
MySQL connection is closed
Пожалуйста, предложите, какие изменения кода я могу сделать здесь, чтобы справиться с этой ситуацией.