question

ozkaro-7807 avatar image
0 Votes"
ozkaro-7807 asked Viorel-1 answered

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

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

dotnet-csharpdotnet-sqlite
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
1 Vote"
Viorel-1 answered

Did you try

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

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

thanks it works!

0 Votes 0 ·
TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.