views:

16

answers:

2

How would i translate these mysql queries to work with sqlite3?:

self.find(:first, :conditions => ['concat(first_name, \' \', middle_names, \' \', last_name) = ?', name])

self.find(:all, :conditions => ['(concat(first_name, \' \', last_name) LIKE ?) OR (concat(first_name, \' \', middle_names, \' \', last_name) LIKE ?)', "%#{name}%", "%#{name}%"])
A: 

|| is the SQLite concatenation operator. So this'll probably work:

self.find(:first, :conditions => ['first_name || \' \' || middle_names || \' \' || last_name = ?', name])
self.find(:all, :conditions => ['(first_name || \' \' || last_name LIKE ?) OR (first_name || \' \' || middle_names || \' \' || last_name LIKE ?)', "%#{name}%", "%#{name}%"])
Albert Peschar
A: 

A little google search could have helped: http://www.google.com/search?q=sqlite+concat

In SQLite the concat operator is ||. and for easier reading of string with single quotes ' I would enclose these strings with double quotes ", so you don't need so much escaping:

self.find(:first, :conditions => ["first_name || ' ' || middle_names || ' ' || last_name = ?", name])

self.find(:all, :conditions => ["(first_name || ' ' || last_name LIKE ?) OR (first_name, ' ', middle_names || ' ' || last_name LIKE ?)", "%#{name}%", "%#{name}%"])
jigfox
Thanks! Looks like sqlite sticks the the standard SQL in that case...
thomasfedb