一个bat文件中创建一个数据库 [英] Create a database within a bat file

查看:230
本文介绍了一个bat文件中创建一个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个bat文件在本质上是一个数据库。我希望能够进入被束缚在单一记录信息。当我进入一个纪录,我希望能够通过分配给它的卡号来查找记录。在code我现在已经没有真正的工作,由于这两个变量没有被正确保存的事实。

I am trying to create a bat file that is in essence a database. I want to be able to enter information that is tied to a single record. When I enter a record, I want to be able to look up the record by the card number assigned to it. The code I have now doesn't really work due to the fact that the variables are not being stored properly.

这是我的code:

Color 5F
@echo off
:start
cls
echo ==========================================
echo           Gift Card
echo ==========================================
echo.
echo What would you like to do?
echo.
echo    1 Add Card
echo    2 Check Information
echo    3 Edit Card Balance
echo    4 Delete Card
echo.
set /p choice=Please enter choice: 
if /I %choice%==1 goto 1
if /I %choice%==2 goto 2

:1
echo.
set /p var=Enter Card Number: 
set /p val=Enter Amount: 
set /p fname=Enter First Name: 
set /p lname=Enter Last Name: 
set /p cbal=Enter Current Balance: 
set /p diss=Enter Date issued: 
@echo set %var%=%val%=%fname%=%lname%=%cbal%=%diss% > %var%.bat
echo.
echo The data has been stored!
pause
goto start

:2
echo.
set /p var=Please enter card number: 
setlocal enabledelayedexpansion
call %var%.bat
echo !%fname%! !%lname%!'s !%var%! card has $!%cbal%! on it as of !%diss%!!
pause > nul
goto start

我试图单独和共发送变量,没有工作过。我想那是因为我没有做延迟扩展语法来正确的。

I have tried to send the variables separately and altogether and none have worked. I am thinking it is because I do not have the delayed expansion sytax correct.

任何帮助非常AP preciated!

Any help is very appreciated!

推荐答案

您的问题似乎在于,你生成批处理文件来填充变量...您只产生一条线,这实际上并没有任何分配给任何的你需要的变量。

Your problem seems to lie where you generate the batch file to fill the variables... You generate only one line, which does not actually assign anything to any of the variables you need.

尝试更改

@echo set %var%=%val%=%fname%=%lname%=%cbal%=%diss% > %var%.bat

@echo set var=%var% > %var%.bat
@echo set val=%val% >> %var%.bat
@echo set fname=%fname% >> %var%.bat
@echo set lname=%lname% >> %var%.bat
@echo set cbal=%cbal% >> %var%.bat
@echo set diss=%diss% >> %var%.bat

这应该让你的变量要正确装回。

This should allow your variables to be loaded back properly.

此外,变更

echo !%fname%! !%lname%!'s !%var%! card has $!%cbal%! on it as of !%diss%!!

阅读

echo %fname% %lname%'s %var% card has $%cbal% on it as of %diss%!

您永远不应该同时使用包围变量在批处理文件中,只有一个或其他。 在大多数情况下使用; 时,你需要阅读多行里面的code座的变量,应使用(例如,一个结果如果这是用括号括语句或循环体)。

You should never use both ! and % to surround variables in a batch file, only one or the other. % should be used in most cases; ! should be used when you need to read a variable inside a multi-line "code block" (for example, the result of an if statement or the body of a for loop) which is surrounded by parentheses.

更多的一些建议:


  • 您可以把 SETLOCAL delayedexpansion 只是在文件的开头,之后一度关闭@echo 。但是,你是不是在这个程序做任何事情(然而,至少)需要延迟扩展。延迟扩展是用来启用访问变量与象征,只有用括号括多行语句身体里非常有用。正因为如此,你应该摆脱它完全,除非/直到你真正需要它,因为它可能会导致其他问题。

  • 有一个在你打电话后关闭@echo (但它不会破坏任何东西)的任何命令没有必要的@符号。在 @ 符号简单燮presses呼应它precedes命令,而是因为所有命令默认情况下,你叫<$ C后,沉默是多余$ C>呼应了。出于这个原因,重要的是只有当调用使用它的第一行关闭@echo ,使用户不会看到该命令的响应。

  • You can put setlocal delayedexpansion just once in the beginning of the file, right after @echo off. However, you are not doing anything in this program (yet, at least) to need delayed expansion. Delayed expansion is used to enable accessing variables with the ! symbol and is only useful inside multi-line statement bodies surrounded by parentheses. Because of that, you should get rid of it completely unless/until you actually need it, as it can cause other problems.
  • There is no need for the "@" symbol in any command after you call @echo off (but it won't break anything). The @ symbol simply suppresses echoing of the command which it precedes, but it is redundant because all commands are silenced by default after you call echo off. For this reason, it is important to only use it on the first line when calling @echo off, so that the user does not see that command echoed.

这篇关于一个bat文件中创建一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆