How do you create prepared statements with the mysql2 gem?

前端 未结 7 1654
情歌与酒
情歌与酒 2021-01-04 00:50

I\'ve tried using google to answer this seemingly simple question, but to my surprise, it didn\'t help.

I have code in my rails application currently using the \'pre

7条回答
  •  说谎
    说谎 (楼主)
    2021-01-04 01:15

    UPDATE

    As Ryan Rapp pointed out correctly, mysql2 now supports prepared statements. Following snippet is extracted from the readme:

    statement = @client.prepare("SELECT * FROM users WHERE login_count = ?")
    result1 = statement.execute(1)
    result2 = statement.execute(2)
    
    statement = @client.prepare("SELECT * FROM users WHERE last_login >= ? AND location LIKE ?")
    result = statement.execute(1, "CA")
    

    Thanks Ryan!

    Original Post

    I found no such function either; neither in source nor in the documentation. Maybe the following snippet is a helpful replacement for your needs? (found in the documentation of mysql2 gem):

    escaped = client.escape("gi'thu\"bbe\0r's")
    results = client.query("SELECT * FROM users WHERE group='#{escaped}'")
    

提交回复
热议问题