SQLite error:SQLite error near “@table“: syntax error

created at 02-23-2022 views: 8

problem

In the C# operation SQLite post written before, I found that sometimes the following error occurs when running:

SQLite error near "@table": syntax error

The code where the error occurs is as follows:

public static Tuple<bool, DataSet, string> GetBarcode(string barcode)
{
     if (string.IsNullOrEmpty(barcode))
     {
         return new Tuple<bool, DataSet, string>(false, null, "The passed parameter cannot be empty");
     }

     string sql = "SELECT * FROM @table WHERE Barcode = @barcode";
     SQLiteParameter[] parameter = new SQLiteParameter[]
     {
         new SQLiteParameter("table", "Database table name"),
         new SQLiteParameter("barcode", barcode)
     };
     return SQLiteHelpers.ExecuteDataSet(sql, parameter);
}

Solution:

I found that the database table name cannot be added to the SQLiteParameter parameter list, other column names are fine, and the SQL statement will not report an error if the string connection method is used, as follows:

public static Tuple<bool, DataSet, string> GetBarcode(string barcode)
{
     if (string.IsNullOrEmpty(barcode))
     {
         return new Tuple<bool, DataSet, string>(false, null, "The passed parameter cannot be empty");
     }

     string sql = string.Format("SELECT * FROM {0} WHERE Barcode = @barcode", "Database table name");
     SQLiteParameter[] parameter = new SQLiteParameter[]
     {
         new SQLiteParameter("barcode", barcode)
     };
     return SQLiteHelpers.ExecuteDataSet(sql, parameter);
}
created at:02-23-2022
edited at: 02-23-2022: