0
我得到以下输出在SQL查询红宝石连接字符串
INSERT INTO AIRPORTS VALUES(null, #{iata} , #{latitude} , #{longitude}, #{name}, #{city})airportsScript.rb:27:in `query': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 (Mysql2::Error)
from airportsScript.rb:27:in `block in getAirports'
from airportsScript.rb:20:in `each'
from airportsScript.rb:20:in `getAirports'
from airportsScript.rb:32:in `<main>'
从这个脚本(实际上是完整的脚本):
#!/usr/bin/env ruby
require 'rubygems'
require 'json'
require 'rest-client'
require 'uri'
require 'mysql2'
def getAirports()
client = Mysql2::Client.new(:host => "localhost", :username => "****", :password => "****" , :database => "****")
url='https://api.flightstats.com/flex/airports/rest/v1/json/all?appId=APPIDHIDENKey=KEYHIDDEN'
airportsJson = JSON.parse(RestClient.get(url))
for airports in airportsJson["airports"]
iata = airports["iata"]
latitude = airports["latitude"]
longitude = airports["longitude"]
name = airports["name"]
city = airports["city"]
print 'INSERT INTO AIRPORTS VALUES(null, #{iata} , #{latitude} , #{longitude}, #{name}, #{city})'
client.query('INSERT INTO AIRPORTS VALUES(null, #{iata} , #{latitude} , #{longitude}, #{name}, #{city})')
end
client.close
end
getAirports()
正如你可能会注意到,在输出的第一行来from print 'INSERT INTO AIRPORTS VALUES(null, #{iata} , #{latitude} , #{longitude}, #{name}, #{city})'
为什么我不能获得iata,纬度,长度等变量的值而不是实际字符串“#{iata}”
我是新来的红宝石这是我的第一个sc ript btw。
我不知道你使用这个脚本是什么,但构建这样的SQL查询是非常不安全的。你很容易出现SQL注入和其他问题。 – 2014-09-19 20:37:53
我只会用很多航班数据填充数据库,只有一次。 – 2014-09-19 20:41:27
如果只是一次就没关系。你应该正确地引用和逃避你的字符串。或者更好的是,mysql2不可能如此完全破坏和无用,以至于不支持查询中的某种占位符,请使用它们。 – 2014-09-19 20:45:12