SQLite How to Query in SQL code a string wich startswith()?

ozkaro 66 Reputation points
2021-06-27T03:07:45.957+00:00

Hi there. I'm have a trouble trying to find in my table elements witch starts with a specified character using this code:

 using (SQLite.SQLiteConnection cx = new SQLite.SQLiteConnection(dbPath))
                {
                    var squery = cx.Query<DictDatabase>("SELECT * FROM DictTable WHERE Word LIKE ?", sel[e.Position].Character );

                    dict_lst = squery.ToList();
                }

I'll try using this "... LIKE ?%" in the query, but have a message error saying 'input incomplete'

I try to use linq too, but for some reason i have another message saying 'System.AggregateException'
using this code.

 var _dictionario = from dct in dbconnection.Table<DictDatabase>()
                                       where dct.Word.Contains(sel[e.Position].Character)
                                       select dct;
                dict_lst = _dictionario.ToListAsync().Result; // message error in this line

I don't know if the error message is why I'm using another linq querying another table before this code line

so I opt for use the Query statement, also to know how use it.
thanks in advance

SQL Server Other
Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2021-06-27T07:31:14.48+00:00

    Did you try

    var squery = cx.Query<DictDatabase>("SELECT * FROM DictTable WHERE Word LIKE ?", sel[e.Position].Character + "%" ); ?

    1 person found this answer helpful.

  2. Timon Yang-MSFT 9,606 Reputation points
    2021-06-28T02:58:22.473+00:00

    Your code should be useful, no matter I use sql or linq for testing, there is no problem.

     using (SQLiteConnection m_dbConnection = new SQLiteConnection(@"Data Source=C:\...\MyDatabase1.sqlite;"))  
                {  
                    m_dbConnection.Open();  
      
                    string sql = "SELECT * FROM highscores WHERE name LIKE ?";  
                    using (SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection))  
                    {  
                        command.Parameters.AddWithValue("param1", "(%");  
                        command.ExecuteReader();  
                        ....  
                    }  
                }  
    

    So I guess sel[e.Position].Character may be wrong, it is not the expected ‘(’.

    Try to add a breakpoint and check its value.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.