简体   繁体   English

为什么 MariaDB 在我的 sql 查询中不接受我的字符串变量作为表名?

[英]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.我在从 xampp 数据库中检索数据时遇到问题。 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;您的 SQL 语法有错误; check the manual that corresponds to your MariaDB server version for the right syntax to use near "c00101" at line 1查看与您的 MariaDB 服务器版本相对应的手册,了解在第 1 行的“c00101”附近使用的正确语法

c00101 is the table name and is what the variable contract holds c00101 是表名,是变量合约所包含的内容

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?有什么可以帮助我的吗?

If you'd looked at the actual SQL, rather than the code that builds it, you'd have seen the difference, ie the single quotes.如果您查看的是实际的 SQL,而不是构建它的代码,您就会看到不同之处,即单引号。 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.就像您在 VB 中将双引号括在文字Strings周围而不是在变量或其他标识符周围一样,您在 SQL 中将单引号括在文本文字周围,但在标识符周围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM