0

I'm trying to store an c# datatype in a sqlite3 table and it gives an error.

                    "id INTEGER NOT NULL PRIMARY KEY, " +
                    "time DATETIME, " +
                    "total DECIMAL)

When trying to insert a value

            string Sql = "INSERT INTO " + TableName + " (symbolId, time, total)"
            + string.Format("VALUES (@{0},@{1},@{2})",
                    (int)symbolId, time, total);
            SqliteCommand command = new SqliteCommand(Sql, Connection);
            command.ExecuteNonQuery();

When executing the query the error is the following:

enter image description here

What is the correct approach? Use text type instead?

Nmaster88
  • 1,405
  • 2
  • 23
  • 65
  • 2
    SQLite doesn't have a ["DATETIME" or "DECIMAL"](https://www.sqlite.org/datatype3.html) data type. You'll have to convert the Decimal type to something else, the choice will depend on your exact needs. – Anon Coward Mar 05 '22 at 16:53
  • 1
    Use SQLite parameters and the provider will handle saving the decimal for you. Not to mention all the other reasons to use proper parameters – Crowcoder Mar 05 '22 at 17:40
  • 2
    Never do string concatenation on SQL statements. You'll have SQL injection issues. – Thomas Weller Mar 05 '22 at 18:05
  • Does this answer your question? [Why do we always prefer using parameters in SQL statements?](https://stackoverflow.com/questions/7505808/why-do-we-always-prefer-using-parameters-in-sql-statements) – Charlieface Mar 05 '22 at 21:29
  • You *must* use parameters, or you may get syntax errors such as the above, and SQL injection attacks. Note that the table name cannot be parameterized, however if you want to do that then you probably have a normalization issue anyway (your data should probably be in one table) – Charlieface Mar 05 '22 at 21:31

0 Answers0