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?
How can I add what has been written in edit line in table?
db.execute( “insert into teste values ( ?)”, @name)
Hi Danilo, How are you enjoying shoooes?
Make sure to interpolate those instance variables!
db.execute “insert into db.table (column-name) values (#{@ivar})”
Le vendredi 27 avril 2012 à 15:35 +0900, Zachary S. a écrit :
Hi Danilo, How are you enjoying shoooes?
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.
From what I’ve read[1], sqlite will coerce values into the declared
datatype of the column[2].
1: _why's Estate - A Quick Guide to SQLite and Ruby
2: Distinctive Features Of SQLite
Michel wrote in post #1058590:
Le vendredi 27 avril 2012 à 15:35 +0900, Zachary S. a écrit :
Hi Danilo, How are you enjoying shoooes?
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.
I tried it but did not work = /
On Fri, Apr 27, 2012 at 9:35 AM, Danilo L. [email protected] wrote:
db.execute “insert into db.table (column-name) values (#{@ivar})”
I tried it but did not work = /
-
What does “did not work” mean??
Was there an error message? Did an incorrect value get inserted
into the DB? No value?
-
The solution would be obvious if you first tried the above insert
statement with a string literal rather than a variable…
Hassan S. wrote in post #1058680:
On Fri, Apr 27, 2012 at 9:35 AM, Danilo L. [email protected] wrote:
db.execute “insert into db.table (column-name) values (#{@ivar})”
I tried it but did not work = /
-
What does “did not work” mean??
Was there an error message? Did an incorrect value get inserted
into the DB? No value?
-
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.
@ashbb
This works, but I want to enter what is typed in the text box.
@name = edit_line
Hi Danilo,
Try out the following again.
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
ashbb
Hi Danilo and folks,
I tried it but did not work = /
Umm,…
Could you try out the following snippet with your Shoes?
require ‘sqlite3’
Shoes.app do
db = SQLite3::Database.new “testtest.db”
db.execute “create table t1 (text TEXT,num INTEGER)”
db.execute “insert into t1 (text,num) values (3,3)”
rows = db.execute “select * from t1”
rows.each{|t, n| para “#{t.class} : #{t}, #{n.class} : #{n}”}
end
I got “String : 3, Fixnum : 3” on a Shoes window.
I confirmed the above snippet with Shoes 3 (0.r1514) for Windows 7.
ashbb
From what I’ve read[1], sqlite will coerce values into the declared
datatype of the column[2].
1: _why's Estate - A Quick Guide to SQLite and Ruby
2: Distinctive Features Of SQLite
It seems that sqlite doesn’t have the same behavior in Linux (fedora)
and windows 7.
With this script :
require ‘sqlite3’
var1 = ‘1’
var2 = 2
var3 = ‘3’
var4 = 4
puts “var2 (#{var2.class}) = #{var2} — var4 (#{var4.class}) =
#{var4}”
db = SQLite3::Database.new “Ttest.db”
db.execute “create table t1 (text TEXT, num INTEGER)”
db.execute “insert into t1 (text, num) values (#{var1}, #{var2})”
db.execute “insert into t1 values ( ?, ?)”, var3, var4
db.execute “insert into t1 (text, num) values (5, 6)”
rows = db.execute “select text, num from t1”
rows.each{|t,n| puts “#{t.class} : #{t}, #{n.class} : #{n}”}
db.close
With linux, ruby 1.8.6, I got :
var2 (Fixnum) = 2 — var4 (Fixnum) = 4
String : 1, String : 2
String : 3, String : 4
String : 5, String : 6
But with Windows 7, ruby 1.8.7, I got :
var2 (Fixnum) = 2 — var4 (Fixnum) = 4
String : 1, Fixnum : 2
String : 3, Fixnum : 4
String : 5, Fixnum : 6
Somebody knows why ?
Regards,
Michel
Le samedi 28 avril 2012 à 22:20 +0900, Michel a écrit :
db.execute “insert into t1 (text, num) values (#{var1}, #{var2})”
String : 5, String : 6
Michel
If I add : db.type_translation = true just after openning the data base,
I get the same result in Linux as in Windows.
Sorry, I forgot that.
Michel.
I have a new problem =\
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}’)”
rows = db.execute “select * from test”
rows.each{|name| para “#{name}”}
end
end
ashbb shoeser wrote in post #1058772:
Hi Danilo,
Try out the following again.
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
ashbb
Worked man, thanks
Hi Danilo,
Umm,… your code works on my Windows 7 with Shoes 3…
Isn’t there already the ‘test.db’ file?
If it is, try again after deleting the file.
ashbb
i got it
work place .text in a insert line
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
ashbb shoeser wrote in post #1058908:
Hi Danilo,
Umm,… your code works on my Windows 7 with Shoes 3…
Isn’t there already the ‘test.db’ file?
If it is, try again after deleting the file.
ashbb
I tried to delete, but the same problem occurred
I use Windows XP with Shoes 3