Search code examples
mysqlvb.netxamppmariadbvb.net-2010

Why won't MariaDB accept my string variable as table name on my sql query?


I'm having problems with retrieving data data from my xampp database. I'm pretty new to this so i have no idea how to fix it. Here is my code:

Dim contract As String

Private Sub projectchart()
  OpenConnection()
  sql = "SELECT activityname, progress FROM '" & contract & "';"
  dr = cmd.ExecuteReader
  Chart2.Series("Progress").Points.Clear()

  While dr.Read
    Chart2.Series("Progress").Points.AddXY(dr("activityname"),dr("progress"))
  End While

  cmd.Dispose()
  con.Close()
End Sub

When I run this code this error comes out

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near "c00101" at line 1

c00101 is the table name and is what the variable contract holds

But when I try to run the code in the format below, everything runs okay, the chart I'm trying to display data with works perfectly.

sql = "SELECT activityname, progress FROM c00101;"


I really have no clue why this happens. Can any help me out?


Solution

  • If you'd looked at the actual SQL, rather than the code that builds it, you'd have seen the difference, i.e. the single quotes. That's for specifying text values. Just as you put double quotes around literal Strings in VB but not around variables or other identifiers, so you put single quotes around text literals in SQL but around identifiers.