类型不匹配MySQL

时间:2016-04-21 16:42:18

标签: mysql vbscript

我有一个VBScript,它需要一些系统资源并将它们存储在变量中,我想将它们写入MySQL表。

当我到达SQL语句时,我收到错误

  

类型不匹配:' [字符串:"插入监视器"]'

我用Google搜索并知道它试图将错误的数据传递给数据类型。

MySQL表

CREATE TABLE monitor (
  id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  pcname VARCHAR(30),
  cpu decimal(4,2),
  hdd decimal(4,2),
  mem decimal(4,2),
  rdate TIMESTAMP
);

示例输出:

+----+-------------+------+-------+-------+---------------------+
| id | pcname      | cpu  | hdd   | mem   | rdate               |
+----+-------------+------+-------+-------+---------------------+
|  1 | HOSTNAME-PC | 4.00 | 39.26 | 74.28 | 2016-04-21 12:16:04 |
+----+-------------+------+-------+-------+---------------------+

设置变量

Dim Connection
Dim ConnectionString
Dim Recordset
Dim SQL
dim cpu 
dim hdd 
dim mem
dim host

获取主机名并存储在主机中:

Set wshShell = CreateObject( "WScript.Shell" )
strRegValue = "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Hostname"
strHostName = wshShell.RegRead( strRegValue )
host = strHostName

获取cpu并存储在cpu中:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name = '_Total'")
For Each objItem In colItems
    cpu = objItem.PercentProcessorTime
Next

获取hdd空间并存储在hdd中

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_LogicalDisk Where Name='C:'",,48)
For Each objItem In colItems
    intFreeSpace = objItem.FreeSpace
    intTotalSpace = objItem.Size
    pctFreeSpace = Round((intFreeSpace/intTotalSpace)*100, 2)
    hdd = pctFreeSpace
Next

获取内存空间并存储在mem:

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
GB = 1024 *1024 * 1024
For Each objItem In colItems
    intTotal = Round(objItem.TotalPhysicalMemory / GB, 3)
Next
Set colItems1 = objWMIService.ExecQuery("Select * from Win32_PerfFormattedData_PerfOS_Memory",,48)
For Each objItem1 In colItems1
    intAvail =  Round(objItem1.AvailableBytes / GB, 3)
Next
pctFree = Round((intAvail/intTotal)*100, 2)
mem = pctFree

SQL语句获取错误:

  

"类型不匹配:' [字符串:"插入监视器"]'"

SQL = "insert into monitor (pcname,cpu,hdd,mem) values ('"+host+"','"+cpu+"','"+hdd+"','"+mem+"')"

其余代码:

ConnString = "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=servername; DATABASE=dbname; " &_
    "UID=username;PASSWORD=password; OPTION=3"

Set Connection = CreateObject("ADODB.Connection")
Set Recordset = CreateObject("ADODB.Recordset")

Connection.Open ConnString

Recordset.Open SQL,Connection

If Recordset.EOF Then
    Wscript.Echo("No records returned.")
Else
    Do While NOT Recordset.Eof   
      WScript.Echo Recordset("pcname")
      WScript.Echo Recordset("cpu")
      WScript.Echo Recordset("hdd")
      WScript.Echo Recordset("mem")
      WScript.Echo "<br>"   
      Recordset.MoveNext    
    Loop
End If

Recordset.Close
Set Recordset = Nothing
Connection.Close
Set Connection = Nothing

2 个答案:

答案 0 :(得分:1)

问题是您正在尝试基于插入查询创建记录集。 Recordset的主要目标是从数据库显示数据,但是如果执行插入查询,则无需显示任何内容。

您有两个选择:

  1. 使用连接对象的Execute method执行插入查询:

    Connection.Execute sql, , 128 --128=0x80=adExecuteNoRecords
    
  2. 然后使用选择或表名打开监视器表上的记录集。

    1. 使用select打开监视器表上的记录集对象,并使用记录集对象的AddNew method

      Recordset.Open "SELECT * FROM monitor", Connection, 1
      Recordset.AddNew
      Recordset.Fields("pcname").Value = xxx
      ...
      Recordset.Update
      
    2. 更新:

      正如@ Ekkehard.Horner所指出的那样:十进制列的值不应该用单引号括起来。虽然MySQL会默默地将字符串转换为数字(除非启用了严格的sql模式),但正确的做法是不将数字作为字符串传递。

答案 1 :(得分:0)

更改

SQL = "insert into monitor (pcname,cpu,hdd,mem) values ('"+host+"','"+cpu+"','"+hdd+"','"+mem+"')"

SQL = "insert into monitor (pcname,cpu,hdd,mem) values ('" & host & "','" & cpu & "','" & hdd & "','" & mem & "')"

应该全部设置