sqlite-net like statement crashes

2019-07-22 12:16发布

问题:

I have a SQL statement like this:

Global.db.Query<Cards>("select * from Cards where card_name like ?", nameTextBox.Text);

But I want to add % to both sides like this with the parameter value.

Global.db.Query<Cards>("select * from Cards where card_name like %?%", nameTextBox.Text);

But I'm throwing an error when I try to execute this. Any ideas why its crashing when I use the like statement like this? I ran the same query in my sqlite admin program with the same database and the results came out like they should.

回答1:

But I want to add % to both sides like this with the parameter value.

So do it for the value itself, rather than decorating the parameter in the SQL:

Global.db.Query<Cards>("select * from Cards where card_name like ?",
    "%" + nameTextBox.Text + "%");


回答2:

Global.db.Query<Cards>($"select * from Cards where card_name like %{nameTextBox.Text}%");