sth = dbh.prepare("select au_lname, au_fname from authors where state = #{state}")
[...]
I get:
/usr/local/lib/ruby/site_ruby/1.6/DBD/ODBC/ODBC.rb:129:in `prepare': S0022 (207)
[Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'CA'.
(DBI::DatabaseError)
from /usr/local/lib/ruby/site_ruby/1.6/dbi/dbi.rb:536:in `prepare'
from select_1.rb:8
Is there a problem in using where clauses in DBI with ODBC? Or am I
doing something wrong?
You forgot the single-quotes around the string literal in your SQL:
sth = dbh.prepare("select au_lname, au_fname from authors where state = '#{state}'")
WIA,
-- Dossy
···
On 2002.07.09, Mark Probert <probertm@nortelnetworks.com> wrote:
--
Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
"He realized the fastest way to change is to laugh at your own
folly -- then you can let go and quickly move on." (p. 70)
This is a Ruby-DBI question. I am using 0.0.15 under ruby 1.6.6 (cygwin).
I am using ODBC to connect to a SQL 7 database. The connection and
test programs work fine.
However, when I do:
require ‘dbi’
dbh = DBI.connect(‘DBI:ODBC:pubs’, ‘bob’, ‘bob’)
state = “CA”
sth = dbh.prepare(“select au_lname, au_fname from authors where state =
#{state}”)
sth.execute
I get:
/usr/local/lib/ruby/site_ruby/1.6/DBD/ODBC/ODBC.rb:129:in prepare': S0022 (207) [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'CA'. (DBI::DatabaseError) from /usr/local/lib/ruby/site_ruby/1.6/dbi/dbi.rb:536:in prepare’
from select_1.rb:8
Is there a problem in using where clauses in DBI with ODBC? Or am I
doing something wrong?
You need to put quotes around the #{state}substitution to compare the value
of the state column to a string literal “CA”. Without quotes, the value of
the state column is compared to the value of the CA column, which I guess
doesn’t exist and results in the error message you are seeing. The
statement should be built like this:
sth = dbh.prepare(“select au_lname, au_fname from authors where state =
"#{state}"”)
You might be able to use single quotes in SQL (I can’t remember; it’s a
while since I’ve done any SQL coding), in which case the statement can be
built like this, which is more readable:
sth = dbh.prepare(“select au_lname, au_fname from authors where state =
‘#{state}’”)
sth = dbh.prepare(“select au_lname, au_fname from authors where state = ?”)
sth.execute(state)
or even shorter:
dbh.execute(“select au_lname, au_fname from authors where state = ?”, state)
Regards,
Michael
···
On Tue, Jul 09, 2002 at 01:10:36AM +0900, Mark Probert wrote:
Hi.
This is a Ruby-DBI question. I am using 0.0.15 under ruby 1.6.6 (cygwin).
I am using ODBC to connect to a SQL 7 database. The connection and
test programs work fine.
However, when I do:
require ‘dbi’
dbh = DBI.connect(‘DBI:ODBC:pubs’, ‘bob’, ‘bob’)
state = “CA”
sth = dbh.prepare(“select au_lname, au_fname from authors where state = #{state}”)
sth.execute
since $/ can’t be a regexp, a shorthand was desired to append multiple
successive \n’s together in the #each method and unfortunetly ‘’ got the
job. but that left us with no equivalent to the fictional #each_chr,
I agree. However, even though the code may be different, that’s
basically what String#each is doing already, except that it’s by a
string.
foo.each(bar) …
could be implemented as
foo.split(/#{bar}/).each …
very easily, with the one difference that if bar.empty?, then it’s
treated as:
foo.split(/\n+/m)).each …
I think what I’m getting at is that String#each already does the
equivalent of String#split#each – but does so in a way that is less
useful than String#split#each (IMO).
Why not make String#each act like String#split#each?
-austin
– Austin Ziegler, austin@halostatue.ca on 2002.07.08 at 17.33.30
···
On Tue, 9 Jul 2002 05:55:28 +0900, David Alan Black wrote:
On Mon, 8 Jul 2002, Austin Ziegler wrote:
If $/ (the argument to String#each) could be made to accept
regex, then the record separator could be String#each(//) if
necessary, allowing character-by-character parsing. This is not
currently possible.
I’ll put in (yet another plug for String#split:
str.split(//).each …
I think the idea of having String#each take a regex is,
essentially, a kind of compression of this – in other words,
instead of #split’ing on a regex and then iterating through the
resulting array, moving the regex intelligence into the argument
to #each. I’m of two minds about the merits of this, but in any
case I don’t think there would be anything possible in the
language that isn’t possible now.
this involves excess overhead just to get each character. better to use:
str.each_byte { |c| c.chr }
but either way both of these take two steps when only one should be
needed.
str.each(‘’) …
or
$/ = ‘’
str.each …
is much more elegent and can be implemented in such a way as to be more
efficient.
~transami
···
On Mon, 2002-07-08 at 14:55, David Alan Black wrote:
Hello –
On Mon, 8 Jul 2002, Austin Ziegler wrote:
If $/ (the argument to String#each) could be made to accept regex,
then the record separator could be String#each(//) if necessary,
allowing character-by-character parsing. This is not currently
possible.
I’ll put in (yet another plug for String#split:
str.split(//).each …
I think the idea of having String#each take a regex is, essentially, a
kind of compression of this – in other words, instead of #split’ing
on a regex and then iterating through the resulting array, moving the
regex intelligence into the argument to #each. I’m of two minds about
the merits of this, but in any case I don’t think there would be
anything possible in the language that isn’t possible now.
On 2002.07.09, Mark Probert <probertm@nortelnetworks.com> wrote:
> sth = dbh.prepare("select au_lname, au_fname from authors where
> state = #{state}")
[...]
You forgot the single-quotes around the string literal in your SQL:
Thank you so much!
Sometimes the simple mistakes are the hardest to find ...
-mark.
---------------------
Mark Probert (probertm@nortelnnetworks.com)
Nortel Networks -- Optera Metro 3000 GNPS
Phone: (613) 768-1082 [ESN: 398-1082]
Also note you can use ?'s as placeholders, then pass the literal to the
execute or as an extra argument to prepare; it’ll quote it for you
taking into account any oddities of the server you’re using.
Better perhaps to try it in Ruby awhile
fbefore making an RCR:
class String
def chars
self.split(“”)
end
def each_char
self.each_byte {|b| yield b.chr }
end
end
Hal Fulton
···
----- Original Message -----
From: “Tobias Reif” tobiasreif@pinkjuice.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, July 08, 2002 2:41 PM
Subject: Re: is there a better string.each?
so what about a simple RCR, asking for
String#chars
, so that we can do
‘foo’.chars #> [“f”,“o”,“o”]
and
‘foo’.chars.each do |char|
perhaps plus String#each_char?
I think that this could work. I also think that an RCR should be
done so that $/ (and the argument to String#each) can be a regexp.
-austin
– Austin Ziegler, austin@halostatue.ca on 2002.07.08 at 15.51.04
···
On Tue, 9 Jul 2002 04:41:04 +0900, Tobias Reif wrote:
Tom Sawyer wrote:
but that left us with no equivalent to the fictional #each_chr,
so what about a simple RCR, asking for
String#chars
so that we can do
‘foo’.chars #> [“f”,“o”,“o”]
and
‘foo’.chars.each do |char|
perhaps plus String#each_char?
Tahnk you very much, now all tests are ok except tests/marshal.rb.
Here is the output:
…
FAILURES!!!
Test Results:
Run: 18/18(113 asserts) Failures: 0 Errors: 7
Errors: 7
tests/marshal.rb:187:in test_11_unknown'(TestBtree): undefined local variable o r method unknown’ for #TestBtree:0x100d0848 (NameError)
from tests/marshal.rb:299
…
if/when $/ accepts regexps, what about ‘’ as a seperator meaning each
character instead of multiple successive \n’s? to me this seems a
dubious equality --an exception caused by the lack of $/ accepting
regexps. if so, each_char (each_chr?) would technically be unneccessary,
but i wouldn’t mind it being there too, along side each_byte and
each_line.
also i don’t know if we need two seperate methods (e.g. #each_line vs. #lines) for iterating over a block versus returning an array. the same
method can be used for both, depending on whether a block is given or
not. right?
~transami
···
On Mon, 2002-07-08 at 13:52, Austin Ziegler wrote:
On Tue, 9 Jul 2002 04:41:04 +0900, Tobias Reif wrote:
Tom Sawyer wrote:
but that left us with no equivalent to the fictional #each_chr,
so what about a simple RCR, asking for
String#chars
so that we can do
‘foo’.chars #> [“f”,“o”,“o”]
and
‘foo’.chars.each do |char|
perhaps plus String#each_char?
I think that this could work. I also think that an RCR should be
done so that $/ (and the argument to String#each) can be a regexp.
-austin
– Austin Ziegler, austin@halostatue.ca on 2002.07.08 at 15.51.04
–
~transami
“They that can give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety.”
– Benjamin Franklin
Better perhaps to try it in Ruby awhile
fbefore making an RCR:
class String
def chars
self.split(“”)
end
def each_char
self.each_byte {|b| yield b.chr }
end
end
As I’ve said before, this is a nice solution, Hal. However, I would expect
that native support for this would be faster, and when one is itterating
over characters, speed can quickly become an issue. I was hoping for
something faster than split(‘’), which is also not very memory efficient,
creating an arbitrary Array object in the process.
:Tahnk you very much, now all tests are ok except tests/marshal.rb.
:
:Here is the output:
:…
:FAILURES!!!
:Test Results:
: Run: 18/18(113 asserts) Failures: 0 Errors: 7
:Errors: 7
:tests/marshal.rb:187:in test_11_unknown'(TestBtree): undefined local :variable o :r methodunknown’ for #TestBtree:0x100d0848 (NameError)
: from tests/marshal.rb:299
:…