Search code examples
mysqlrubyqtruby

Ruby + mysql2 querying fails with variables


Working with Ruby 2.0, Qt4 gem and Mysql2 gem. I need to compare the text of two lineedit and make a query with them, which is a failure so far.

client = Mysql2::Client.new(:host => "localhost", :username => "root", :password => "123456", :database => "school")
# text of both lineedits saved into local variables
[email protected]() 
[email protected]()
#then
res= client.query("SELECT usr_name, usr_pass, usr_tipo FROM user WHERE usr_name = tName AND usr_pass = tPass")

The only thing that fails is that query. I've tried to put the local variable as global (@tName, @tPass), or put them into #{}, which search for columns in the table user named tName and tPass, also tried to put them into '' but that only search for a user named tName.

I want the query to search for usr_name= "text inside tName". What am I doing wrong?

EDIT: if you are wondering, tName, tPass are strings and the fields usr_name and usr_pass are varchar(50).


Solution

  • Looks like you didn't interpolate the variables. do the following

    res= client.query("SELECT usr_name, usr_pass, usr_tipo 
                       FROM user 
                       WHERE usr_name = '#{tName}' AND usr_pass = '#{tPass}'")