如何使用表单在数据库中添加列?

时间:2018-06-20 13:12:17

标签: ruby-on-rails ruby

有一个带有名称和数据类型输入的表单。在提交表单时,应在表格中添加另一列。它应该执行sql语句ALTER TABLE users ADD name datatype;。我怎样才能做到这一点?我已经尝试过了,但是行不通。

def add_columns
    sql = "ALTER TABLE users ADD " + name + data_type;
    ActiveRecord::Base.connection.execute(sql);
end

2 个答案:

答案 0 :(得分:1)

好像您错过了一个空格:

sessionInfo()
R version 3.4.1 (2017-06-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_2.2.1       data.table_1.10.4-3

loaded via a namespace (and not attached):
 [1] compiler_3.4.1   colorspace_1.3-2 scales_0.5.0     lazyeval_0.2.0   plyr_1.8.4       gtable_0.2.0     tibble_1.3.3     Rcpp_0.12.12     grid_3.4.1       rlang_0.1.1      munsell_0.4.3   

并在执行此操作时注意SQL注入

答案 1 :(得分:0)

您不应像这样添加列,应该始终使用迁移来填充数据库表和列