代码之家  ›  专栏  ›  技术社区  ›  Xie Steven

uwp sqlite“in”条件不支持参数化sql语句

  •  0
  • Xie Steven  · 技术社区  · 7 年前

    我正在使用 The Microsoft.Data.Sqlite library UWP项目中的相关sqlite API。

    我发现如果我使用 IN

    例如,如果SQL语句如下所示:

    sqliteCommand.CommandText = @"delete from [test_table] where col1 in(@para1,@para2)";
    sqliteCommand.Parameters.Add(new SqliteParameter("@para1", '1'));
    sqliteCommand.Parameters.Add(new SqliteParameter("@para2", '2'));
    sqliteCommand.ExecuteNonQuery();
    

    如果我将SQL更改为:

    sqliteCommand.CommandText = @"delete from [test_table] where col1 in(@para1,@para2)";
    sqliteCommand.Parameters.Add(new SqliteParameter("@para1", "1"));   
    sqliteCommand.Parameters.Add(new SqliteParameter("@para2", "2"));
    

    它确实奏效了。

    sqliteCommand.CommandText = @"delete from [test_table] where col1 in ('1', '2')";
    sqliteCommand.ExecuteNonQuery();
    

    有了这个,它也起到了作用。

    但是,如果我将SQL改为:

    delete from [test_table] where col1 in(@para1)   
    sqliteCommand.Parameters.Add(new SqliteParameter("@para1", "'1','2'"));
    //or sqliteCommand.Parameters.Add(new SqliteParameter("@para1", "1,2"));
    

    在这种情况下,它不起作用。

    我认为微软需要调查这个问题。

    以下是整个代码演示:

    SqliteConnection sqliteConnection = new SqliteConnection("Filename=test.db");
    sqliteConnection.Open();
    
    SqliteDataReader sqliteDataReader;
    
    string sql = @"CREATE TABLE IF NOT EXISTS [test_table] ( col1 VARCHAR(1), col2 VARCHAR(1), col3 VARCHAR(4));";
    
    SqliteCommand sqliteCommand = new SqliteCommand(sql, sqliteConnection);
    sqliteCommand.ExecuteNonQuery();
    
    sql = @"INSERT INTO [test_table] (col1, col2, col3) VALUES('1', '1', '0001')";
    sqliteCommand.CommandText = sql;
    
    sqliteCommand.ExecuteNonQuery();
    
    sql = @"INSERT INTO [test_table] (col1, col2, col3) VALUES('2', '2', '0002')";
    sqliteCommand.CommandText = sql;
    sqliteCommand.ExecuteNonQuery();
    
    sql = @"INSERT INTO [test_table] (col1, col2, col3) VALUES('3', '3', '0003')";
    sqliteCommand.CommandText = sql;
    sqliteCommand.ExecuteNonQuery();
    
    sqliteCommand.CommandText = @"delete from [test_table] where col1 in(@para1,@para2)";
    
    //sqliteCommand.CommandText = @"delete from [test_table] where col1 in('1','2')";
    
    sqliteCommand.Parameters.Add(new SqliteParameter("@para1", '1'));
    sqliteCommand.Parameters.Add(new SqliteParameter("@para2", '2'));
    
    sqliteCommand.ExecuteNonQuery();
    
    sqliteCommand.CommandText = @"select * from [test_table]";
    sqliteDataReader = sqliteCommand.ExecuteReader();
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   mjwills Myles McDonnell    7 年前

    // Example data
    var valuesToDelete = new List<string> { "1", "2", "4" };
    
    sqliteCommand.CommandText = @"delete from [test_table] where col1 in(" + string.Join(",", Enumerable.Range(0, valuesToDelete.Count).Select(z => "@para" + z)) + ")";
    
    for (var i = 0; i < valuesToDelete.Count; i++)
    {
        sqliteCommand.Parameters.Add(new SqliteParameter("@para" + i, valuesToDelete[i]));
    }
    
    sqliteCommand.ExecuteNonQuery();
    

    string.Join 用于在SQL中设置正确数量的参数,以及 for 循环旨在填充这些参数。这允许在内部使用多个值 IN

    出身背景

    原因:

    sqliteCommand.CommandText = @"delete from [test_table] where col1 in(@para1,@para2)";
    sqliteCommand.Parameters.Add(new SqliteParameter("@para1", "1"));
    sqliteCommand.Parameters.Add(new SqliteParameter("@para2", "2"));
    sqliteCommand.ExecuteNonQuery();
    

    sqliteCommand.CommandText = @"delete from [test_table] where col1 in(@para1,@para2)";
    sqliteCommand.Parameters.Add(new SqliteParameter("@para1", '1'));
    sqliteCommand.Parameters.Add(new SqliteParameter("@para2", '2'));
    sqliteCommand.ExecuteNonQuery();
    

    不是因为后者,您意外地将参数传递为 char 而不是 string .

    感觉 正确,因为 ' ' 可以在SQL中围绕字符串使用,但不能在C中使用(C使用 ' 和 " 表示“string”)。

    因此,在传入字符串参数时,请确保将其作为 (使用 " 烧焦 .

    推荐文章