How To Run Raw Sql Queries With Sequel
Solution 1:
Note that instead of:
DB.fetch("SELECT * FROM zone WHERE dialcode = '#{@dialcode}' LIMIT 1")
you should do:
DB.fetch("SELECT * FROM zone WHERE dialcode = ? LIMIT 1", @dialcode)
Otherwise, you open yourself to SQL injection if you don't control the contents of @dialcode.
Solution 2:
I have a few pointers which may be useful:
You could simply do:
@zonename = DB.fetch("SELECT * FROM zone WHERE dialcode = ? LIMIT 1", @dialcode).firstNB: you are ignoring the fact that there could be more results matching the criteria. If you expect multiple possible rows to be returned then you probably want to build an array of results by doing ...
@zonename = DB.fetch("SELECT * FROM zone WHERE dialcode = ? LIMIT 1", @dialcode).alland processing all of them.
The return set is a hash. If
@zonenamepoints to one of the records then you can do@zonename[:column_name]to refer to a field called "column_name". You can't do
@zonename.colum_nname(you could actually decorate@zonenamewith helper methods using some meta-programming but let's ignore that for the moment).
Sequel is an excellent interface, the more you learn about it the more you'll like it.
Post a Comment for "How To Run Raw Sql Queries With Sequel"