Microsoft.Data.SQLite参数不起作用

时间:2019-02-13 04:58:51

标签: c# sqlite uwp

我正在使用Microsoft.Data.SQLite构建数据库信息亭。在构建命令时,我正在使用参数化查询。在VS遇到构建问题之前,此WAS一直有效,并且在应用了各种更新(详细信息如下)之后,不再应用我的参数。将值硬编码到commandText中。

我以前使用的是基本

selectCommand.Parameters.Add("@Table", Table)

,我尝试使用更明确的参数。这是完整的命令:

public static List<String> GetTables(string Table) // Get the Items in a Table
        {
            List<String> entries = new List<string>();

            using (SqliteConnection db = new SqliteConnection(ConnectionString)) // the db
            {
                db.Open();

                SqliteCommand selectCommand = new SqliteCommand("SELECT Item FROM @Table;", db);

                selectCommand.Parameters.Add("@Table", SqliteType.Text).Value = Table;

                SqliteDataReader query = selectCommand.ExecuteReader();

                while (query.Read()) // while still reading data
                {
                    entries.Add(query.GetString(0)); // add string to entries
                }

                db.Close();
            }

            return entries;
        }

但是该参数仍然没有被应用。这是完整的错误:

SQLite Error 1: 'near "@Table": syntax error'.
at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
at Microsoft.Data.Sqlite.SqliteCommand.<PrepareAndEnumerateStatements>d__62.MoveNext()
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader()
at DataAccessLibrary.DataAccess.GetTables(String Table)
at brogle.SelectionPage.<LoadItemGridContent>d__8.MoveNext()}   Microsoft.Data.Sqlite.SqliteException

很显然,@Table并未更改为字符串Table,其值为“ Bulbs”。 (有一个表“ Bulbs”,同样,将其硬编码到命令中也可以正常工作。)调试显示该命令接受值为{Bulbs”的参数@Table

为解决我的构建问题(代码分析引发错误),我安装了Microsoft.CodeAnalysis.FxCopAnalyzers,将SQLitePCLRaw.bundle_winsqlite3和Microsoft.NETCore.UniversalWindowsPlatform更新为最新的稳定版本,并将VS更新为15.9.7。

如果您需要任何详细信息,我可以提供。谢谢。

1 个答案:

答案 0 :(得分:1)

用于参数。您不能以这种方式提供表名,解决方法是:

SqliteCommand selectCommand = new SqliteCommand("SELECT Item FROM "+Table, db);

有关更多详细信息,请参阅Retrieve data from the SQLite database官方文档。