when i put a button to save
it adds the table "(editline Shoes ::)"
code:
require 'sqlite3'
Shoes.app do @name = edit_line
button "save" do
db = SQLite3::Database.new "test.db"
db.execute "create table test (test VARCHAR(25))"
db.execute "insert into test (test) values ('#{@name.text}')"
rows = db.execute "select * from test"
rows.each{|name| para "#{name}"}
end
end
require 'sqlite3'
Shoes.app do @name = edit_line
button "save" do
db = SQLite3::Database.new "test.db"
db.execute "create table test (test VARCHAR(25))"
db.execute "insert into test (test) values ('#{@name.text}')"
rows = db.execute "select * from test"
rows.each{|name| para "#{name}"}
end
end
Make sure to interpolate those instance variables!
db.execute "insert into db.table (column-name) values (#{@ivar})"
Yes, you can do like that. But I think that works only with VARCHAR types, because #{var} calls the to_s method on var.
Regards,
Michel.
···
Le vendredi 27 avril 2012 à 15:35 +0900, Zachary Scott a écrit :
On Thu, Apr 26, 2012 at 10:47 PM, Danilo L. <lists@ruby-forum.com> wrote:
> Hello, I am struggling to insert a variable into a table, for example:
>
>
> @name = edit_line
>
> db = SQLite3::Database.new "test.db"
> db.execute "create table teste (teste VARCHAR(25))"
> db.execute "insert into teste (teste) values (@name)"
>
>
> How can I add what has been written in edit line in table?
>
> --
> Posted via http://www.ruby-forum.com/\.
>
db.execute "insert into db.table (column-name) values (#{@ivar})"
I tried it but did not work = /
1) What does "did not work" mean??
Was there an error message? Did an incorrect value get inserted
into the DB? No value?
2) The solution would be obvious if you first tried the above insert
statement with a string literal rather than a variable...
Sorry me for not explaining,
I tried:
require 'sqlite3'
Shoes.app @name = "Danilo"
db = SQLite3::Database.new "test.db"
db.execute "create table test (test VARCHAR(25))"
db.execute "insert into test (test) values (#{@name})"
end
Then create the table but will not enter the @ name is null.
@Satoshi
This works, but I want to enter what is typed in the text box.
···
On Fri, Apr 27, 2012 at 9:35 AM, Danilo L. <lists@ruby-forum.com> wrote:
require 'sqlite3'
Shoes.app do @name = "Danilo"
db = SQLite3::Database.new "test.db"
db.execute "create table test (test VARCHAR(25))"
db.execute "insert into test (test) values ('#{@name}')"
rows = db.execute "select * from test"
rows.each{|name| para "#{name}"}
end
require 'sqlite3'
Shoes.app do @name = "Danilo"
db = SQLite3::Database.new "test.db"
db.execute "create table test (test VARCHAR(25))"
db.execute "insert into test (test) values ('#{@name}')"
rows = db.execute "select * from test"
rows.each{|name| para "#{name}"}
end