Mysql autoit新手,此变量有什么问题?

Mysql autoit新手,此变量有什么问题?,mysql,autoit,Mysql,Autoit,所以今天我刚开始学习这门语言,我试着用我的部分示例脚本制作我的脚本 我试图运行一个mysql查询,即使它声明为一个全局查询,我也会得到“Variable Undeclared”错误(至少我认为我声明为一个…) 基本上,我的脚本应该做的是为局域网中的每台机器创建一个ID,并将其存储到AppData中的txt文件中,然后将其插入数据库中 #include <MsgBoxConstants.au3> #include <FileConstants.au3> #include "

所以今天我刚开始学习这门语言,我试着用我的部分示例脚本制作我的脚本

我试图运行一个mysql查询,即使它声明为一个全局查询,我也会得到“Variable Undeclared”错误(至少我认为我声明为一个…)

基本上,我的脚本应该做的是为局域网中的每台机器创建一个ID,并将其存储到AppData中的txt文件中,然后将其插入数据库中

#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
#include "EzMySql.au3"
#include <Array.au3>

Example()

Func Example()
   $id = Random(1, 1000, 1);Numar random de la 1 la 100
   Local Const $sFilePath = @AppDataDir & "\id.txt" ; Selectare %appdata% si id.txt
Local $iFileExists = FileExists($sFilePath)
    If $iFileExists Then

Else
    ; Create a temporary file to write data to.
    If Not FileCreate($sFilePath, $ID & @CRLF) Then Return MsgBox($MB_SYSTEMMODAL, "", "O eroare s-a produs in timp ce se scria fila temporara")

    ; Open the file for writing (append to the end of a file) and store the handle to a variable.
    Global $hFileOpen = FileOpen($sFilePath, $FO_APPEND)
    If $hFileOpen = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "O eroare s-a produs in timp ce se citea fila.")
        Return False
     EndIf

    ; Read the contents of the file using the handle returned by FileOpen.
    Global $sFileRead = FileRead($hFileOpen)
    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)

EndIf
EndFunc

; Create a file.
Func FileCreate($sFilePath, $sString)
    Local $bReturn = True ; Create a variable to store a boolean value.
    If FileExists($sFilePath) = 0 Then $bReturn = FileWrite($sFilePath, $sString) = 1 ; If FileWrite returned 1 this will be True otherwise False.
    Return $bReturn ; Return the boolean value of either True of False, depending on the return value of FileWrite.
 EndFunc   ;==>FileCreate

$name=@ComputerName
If Not _EzMySql_Startup() Then
    MsgBox(0, "Error Starting MySql", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg())
    Exit
 EndIf

If Not _EzMySql_Open("127.0.0.1", "root", "", "vrgaming", "3306") Then
    MsgBox(0, "Error opening Database", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg())
    Exit
 EndIf

If Not _EzMySql_Exec("INSERT INTO `lan` (id, nume) VALUES ('"& $sFileRead &"', '"& $name &"')") Then
    MsgBox(0, "Error opening Database", "Error: "& @error & @CR & "Error string: " & _EzMySql_ErrMsg())
    Exit
EndIf

_EzMySql_Close()
_EzMySql_ShutDown()
Exit
#包括
#包括
#包括“EzMySql.au3”
#包括
示例()
Func示例()
$id=Random(1,1000,1);努马尔兰登酒店
本地常量$sFilePath=@AppDataDir&“\id.txt”;Selectare%appdata%si id.txt
本地$iFileExists=FileExists($sFilePath)
如果存在$IFilee,则
其他的
; 创建要将数据写入的临时文件。
如果不是FileCreate($sFilePath,$ID&@CRLF),则返回MsgBox($MB_SYSTEMMODAL,“,”O eroare s-a produs in timp ce se scria fila temporara“)
; 打开要写入的文件(附加到文件末尾)并将句柄存储到变量。
全局$hFileOpen=FileOpen($sFilePath,$FO_APPEND)
如果$hFileOpen=-1,则
MsgBox($MB_SYSTEMMODAL,“,”O eroare s-a produs in timp ce se citea fila.)
返回错误
恩迪夫
; 使用FileOpen返回的句柄读取文件内容。
全局$sFileRead=FileRead($hFileOpen)
; 关闭FileOpen返回的句柄。
文件关闭($hFileOpen)
恩迪夫
EndFunc
; 创建一个文件。
Func FileCreate($sFilePath,$sString)
本地$bReturn=True;创建一个变量来存储布尔值。
如果文件存在($sFilePath)=0,则$bReturn=FileWrite($sFilePath,$sString)=1;如果FileWrite返回1,则为真,否则为假。
返回$bReturn;根据FileWrite的返回值,返回布尔值True或False。
EndFunc;==>文件创建
$name=@ComputerName
如果不是EzMySql_Startup(),则
MsgBox(0,“启动MySql时出错”,“错误:&@Error&@CR&”错误字符串:&&uezmysql\uerrmsg()
出口
恩迪夫
如果没有打开(“127.0.0.1”,“根”,“虚拟游戏”,“3306”),那么
MsgBox(0,“打开数据库时出错”,“错误:&@Error&@CR&”错误字符串:&&uezmysql\uerrmsg()
出口
恩迪夫
如果没有"Exec"("插入"lan"(id,nume)值("&$sFileRead&","&$name&"),则
MsgBox(0,“打开数据库时出错”,“错误:&@Error&@CR&”错误字符串:&&uezmysql\uerrmsg()
出口
恩迪夫
_埃祖克洛斯()
_关闭
出口

代码中有许多逻辑问题,这使得它有点难以理解

这种情况下的错误几乎肯定是由“id.txt”存在引起的,因此
$iFileExists
的计算结果为true,并且永远不会创建全局变量

然后,在读取所有数据之前,您将创建一个新的“id.txt”用于写入,因此$sFileRead将始终为空,而FileRead将设置@error,因为您正试图从为写入而打开的文件中读取

现在,我认为您希望$sFileRead是id.txt的任何内容,如果id.txt不存在,则应使用新的随机id创建id.txt。如果是这样,则需要将打开并读取文件的代码移到
Else…Endif
块之外(除了将FileOpen更改为使用$fou READ)


这个答案没有解决有问题的编程实践和代码结构。

整个错误是什么?我想在表中的“id”列中插入$sFileRead,但我得到了一个“Undeclared variable”错误,即使我在第一个函数中将它声明为全局变量。抛出此错误的原因是什么?$sFileRead此变量。