【问题标题】:Inserting into database via web service通过网络服务插入数据库
【发布时间】:2016-06-18 08:44:46
【问题描述】:

我正在通过网络服务将用户插入我的数据库,但每次我尝试这样做时,网络服务都会返回以下错误: TinyTds::Error: INSERT 语句中的列多于 VALUES 子句中指定的值。 我没有看到问题,因为每个语句中有 5 个值。

post '/insert_users/' do
conn = TinyTds::Client.new(...)
username = params[:username]
password = params[:password]
phone_number = params[:phone_number]
profile_state = params[:profile_state]
clasa = params[:clasa]
sql = "insert into ServerUsers(Username, Passwords,Phone_Number, Profile_State, Class) values('username, password, phone_number, profile_state, clasa')"
cursor = conn.execute(sql)
end

这是我尝试通过网络服务插入数据库的方式:

http://address:port/insert_users/?username=user3&password=parola3&phone_number=0723567432&profile_state=A&clasa=2

我正在使用高级 REST 客户端来测试网络服务。

我在这里使用了教程:https://github.com/rails-sqlserver/tiny_tds

【问题讨论】:

    标签: mysql ruby-on-rails web-services


    【解决方案1】:

    您只能引用字符串:

    将其更改为:

    sql = "insert into ServerUsers(Username, Passwords,Phone_Number, Profile_State, Class) values('username, password, phone_number, profile_state, clasa')"
    

    sql = "insert into ServerUsers(Username, Passwords,Phone_Number, Profile_State, Class) values('#{username}', '#{password}', '#{phone_number}', #{profile_state}, '#{clasa')"
    

    我不知道如何引用每个字符串

    【讨论】:

    • 在我的数据库中,Phone_Number 是 int 类型,如果我加上引号,我会收到一个新错误:将 varchar 值 'phone_number' 转换为数据类型 int 时失败
    • @meow - 我用谷歌搜索过,发现你引用了一个变量 #{data} 。我已经改变了我的答案,但它没有经过测试
    • 非常感谢 :D 它有效。你是一个救生员:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多