我正在尝试将数据插入到我的停止表中stops
表以及正在创建的route
表,但程序无法将数据插入到停止表中,而我是收到此错误:
Unknown column '1' in 'field list'
我感谢任何帮助。
stt.execute("CREATE TABLE IF NOT EXISTS routes"
+ "(route_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, "
+ "route INT(11) NOT NULL)" );
stt.execute("INSERT INTO routes(route) VALUES"
+ "(1),"
+ "(9)");
stt.execute("CREATE TABLE IF NOT EXISTS stops"
+ "(stop_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, "
+ " name varchar(30) NOT NULL, "
+ " lat double(10,6) NOT NULL, "
+ " longi double(10,6)NOT NULL,"
+ "route_id INT, FOREIGN KEY fk_route_id(route_id) REFERENCES routes(route_id) )" );
stt.execute("INSERT INTO stops(route_id, name, lat, longi) VALUES"
+ "(`1`, 'Alex allee', '83.868937', '18.665545' ),"
+ "(`1`,'city street', '83.840642', '18.701246' ),"
+ "(`2`,'apple street', '83.740642', '18.761246' ),"
+ "(`2`,'wall street', '83.868256', '18.669520' )");
答案 0 :(得分:5)
对于转义字符串,您需要使用单引号。列或表名称需要反引号。对于数字,没有逃避特征。
因为你的route_id
值被反引号转义mysql将其解释为列名。删除它们,您的查询将有效。
stt.execute("INSERT INTO stops(route_id, name, lat, longi) VALUES"
+ "(1, 'Alex allee', 83.868937, 18.665545 ),"
+ "(1,'city street', 83.840642, 18.701246 ),"
+ "(9,'apple street', 83.740642, 18.761246 ),"
+ "(9,'wall street', 83.868256, 18.669520 )");