Ruby/shoes GUI - sqlite, how to isert a variable key in table?

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/.

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?

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})"

···

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/\.

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.text}')"
  rows = db.execute "select * from test"
  rows.each{|name| para "#{name}"}
      end
  end

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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.

···

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/\.
>

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

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

···

On Fri, Apr 27, 2012 at 2:52 AM, Michel <michel.revesche@wanadoo.fr> wrote:

Le vendredi 27 avril 2012 à 15:35 +0900, Zachary Scott 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.

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/\.

Michel wrote in post #1058590:

···

Le vendredi 27 avril 2012 à 15:35 +0900, Zachary Scott 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 = /

--
Posted via http://www.ruby-forum.com/\.

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 but the same problem occurred
I use Windows XP with Shoes 3

···

--
Posted via http://www.ruby-forum.com/\.

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...

···

On Fri, Apr 27, 2012 at 9:35 AM, Danilo L. <lists@ruby-forum.com> wrote:

db.execute "insert into db.table (column-name) values (#{@ivar})"

I tried it but did not work = /

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

twitter: @hassan

>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

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

Hassan Schroeder wrote in post #1058680:

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:

--
Posted via http://www.ruby-forum.com/\.

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.

···

Le samedi 28 avril 2012 à 22:20 +0900, Michel a écrit :

> >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

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

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 :smiley:

···

--
Posted via http://www.ruby-forum.com/\.