-1

I keep getting the exception

System.Data.SQLite.SQLiteException: 'SQL logic error near "=": syntax error'

and I have no idea why or whats causing it :(

my code is as follows

SQLiteConnection c = new SQLiteConnection("Data Source = StuTchInfo.db");
SQLiteCommand cmd = new SQLiteCommand(c);
cmd.CommandText = "SELECT *" +
    "FROM tblTeachers" +
   $"WHERE teacherID = '{idField.Text.ToString()}'";
c.Open();
SQLiteDataReader r = cmd.ExecuteReader();

//code processing data from data reader
        

and the exception is thrown on the last line. idField.Text is a string value and I have already tried using idField.Text.ToString() but it still throws the same error. Any ideas?

budywudy9
  • 29
  • 7
  • 1
    There is no point calling ToString on a string – Caius Jard Aug 10 '21 at 09:51
  • 1
    Does this answer your question? [What are good ways to prevent SQL injection?](https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection) and [SqlCommand Parameters Add vs. AddWithValue](https://stackoverflow.com/questions/21110001/sqlcommand-parameters-add-vs-addwithvalue) –  Aug 10 '21 at 09:51
  • Besides not using string concatenation and bad formatting/indentation, here it is a typo: a space is missing before WHERE, thus write for example `$" WHERE ..."`. –  Aug 10 '21 at 09:53

3 Answers3

4

Your string concatenation is causing an sql that contains tblTeachersWHERE

If you want to keep a multi line format for readability, you can ditch the string concat by using an @string

cmd.CommandText = @"
  SELECT *
  FROM tblTeachers
  WHERE teacherID = @name";

//you'll need this too; gives a value to the above parameter
cmd.Parameters.AddWithValue("@name", idField.Text);

You can also investigate Resources facility (right click on project name and choose Properties, then Resources on the left, and "click there" if your project doesnt have one); it allows you to write long strings like huge SQLs and not clutter your code with them. Code would be like:

cmd.CommandText = Properties.Resources.SelectFromTeachersWhereNameIs;

and your designer for the Resources be like:

enter image description here


As a side note, investigate how to parametrize your queries (I did it above with @name), because even if you fix this your code will explode again as soon as someone enters a ' into the textbox, or worse

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
2

You need to add space.

cmd.CommandText = "SELECT * " +
    "FROM tblTeachers " +
    $"WHERE teacherID = '{idField.Text.ToString()}'";
Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
1

You need to use some spaces in your string. Also, you can have one-liner code by using string interpolation.

cmd.CommandText = $"SELECT * FROM tblTeachers WHERE teacherID = '{idField.Text.ToString()}'";
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
  • 1
    I'll probably try doing this from now on at least until i get more used to SQL commands considering I managed to forget string concatenation doesn't automatically add spaces – budywudy9 Aug 10 '21 at 09:52