Giorgi / DuckDB.NET

Bindings and ADO.NET Provider for DuckDB
https://duckdb.net
MIT License
411 stars 69 forks source link

Parameterized `CREATE TABLE` gives syntax error #79

Closed thengineer closed 1 year ago

thengineer commented 1 year ago

Hi,

I'm trying to create a table with a parameterized query, but I get a syntax error. Interestingly, not all creation commands fail (see commented out lines)

var connection = new DuckDBConnection("DataSource=:memory:");
using (var duckDbCommand = connection.CreateCommand())
{
  connection.Open();

  // WORKS:
  // duckDbCommand.CommandText = "CREATE TABLE t1 (id INT, name VARCHAR);";  

  // WORKS:
  // duckDbCommand.CommandText = "CREATE TABLE t2 AS SELECT * FROM read_csv_auto(?);"; 
  // duckDbCommand.Parameters.Add(new DuckDBParameter(csvPath));

  // FAILS:
  duckDbCommand.CommandText = "CREATE TABLE t3(id INT, ? VARCHAR);";  
  duckDbCommand.Parameters.Add(new DuckDBParameter("name"));

  duckDbCommand.ExecuteNonQuery();
}
Error: DuckDB.NET.Data.DuckDBException (0x00000001): Parser Error: syntax error at or near "?"
LINE 1: CREATE TABLE t2 (id INT, ? VARCHAR);
                                 ^
   at DuckDB.NET.Data.PreparedStatement.Prepare(DuckDBNativeConnection connection, String query) in /_/DuckDB.NET.Data/Internal/PreparedStatement.cs:line 55
   at DuckDB.NET.Data.DuckDbCommand.PrepareIfNeeded() in /_/DuckDB.NET.Data/DuckDBCommand.cs:line 114
   at DuckDB.NET.Data.DuckDbCommand.ExecuteNonQuery() in /_/DuckDB.NET.Data/DuckDBCommand.cs:line 70
   at Submission#98.<<Initialize>>d__0.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync[TResult](ImmutableArray`1 precedingExecutors, Func`2 currentExecutor, StrongBox`1 exceptionHolderOpt, Func`2 catchExceptionOpt, CancellationToken cancellationToken)

What am I doing wrong? Or is this a bug?

Giorgi commented 1 year ago

You aren't doing anything wrong. The error message comes from DuckDB itself so I guess it doesn't support parameterized column names when creating a table.

thengineer commented 1 year ago

Got it. Thanks for creating DuckDB.NET, btw.

Giorgi commented 1 year ago

@thengineer Check the discord channel why it's like that: DuckDb Discord