Hello guys,</br>
I'm writing a database in C# and I ran into a weird Problem.</br>
This is my code:
<Code>
using (SQLiteConnection db = new SQLiteConnection(@"Data Source = " + mypathtodb))
{
SQLiteCommand insertCommand = new SQLiteCommand("", db);
if (string.IsNullOrEmpty(inputText[2]))
{
insertCommand.CommandText = "INSERT INTO [@Table] VALUES (@Value);";
}
else
{
insertCommand.CommandText = "INSERT INTO [@Table] VALUES (" + inputText[2] + "@Value);";
}
//inputText[2] is a fix statement wich I use only in some cases so I don't need to check on SQL-Injection
insertCommand.Parameters.AddWithValue("@Table", inputText[0]);
insertCommand.Parameters.AddWithValue("@Value", inputText[1]);
try
{
db.Open();
Console.WriteLine(inputText[0]);
Console.WriteLine(inputText[1]);
Console.WriteLine(inputText[2]);
Console.WriteLine(insertCommand.CommandText);
insertCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
db.Close();
}
</Code>
In my console I get this output:
<Code>
tblTable
"someValue"
"a-internal-prepared-SQL-statement",
INSERT INTO [@Table] VALUES (("a-internal-prepared-SQL-statement"), @Value);
code = Error (1), message = System.Data.SQLite.SQLiteException (0x800007BF): SQL logic error
no such table: @Table
bei System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String& strRemain)
bei System.Data.SQLite.SQLiteCommand.BuildNextCommand()
bei System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
bei System.Data.SQLite.SQLiteDataReader.NextResult()
bei System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
bei System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
bei System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(CommandBehavior behavior)
bei System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
bei myClass.myFunction in pathToProject.myProject.myClass.cs:Zeile 47.
</Code>
As you can see in the fourth row "AddWithValue" does not exchange "@Table" and "@Value" with the values I want to.</br>
Why does this happen?</br>
I don't understand.