[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[特效代码] [分享]批处理版简单SQL

上次看到一老兄的解逻辑题,想了片刻,越想越远,于是有了以下部分代码。
模拟数据库,支持简单的基本SQL语法。
但是部分格式有点不同,参数最好不要包含 空格,=;等符号
只完成了一部分,已测试过create 跟insert ,希望高手给点建议;
  1. @echo off&setlocal ENABLEDELAYEDEXPANSION&setlocal ENABLEEXTENSIONS
  2. :main
  3. call :create table test "姓名,日期,年龄,date" values "玉清,1984-1-1,28,test123"
  4. call :update test set "姓名,年龄" = "陆海,31"
  5. goto :eof
  6. :create table [table_name] [(sum1,sum2,...)] values [(sum1,sum2,...)]
  7. rem 返回0创建成功,%cs%,%cn%,%table_name%可以直接调用,返回1表示已经存在该表,返回2表示数值太长,返回3表示创建初始数值错误
  8. 1>nul 2>nul set %2 && exit /b 1
  9. set "table_name=%2"
  10. set "cs=%~3"
  11. set "cs=!cs:,= !"
  12. set /a cn=0
  13. for %%i in (!cs!) do (
  14. set /a cn+=1
  15. set /a %%i=!cn!
  16. )
  17. set "!table_name!_ !cs!=table" || exit /b 2
  18. if %4.==values. (
  19. call :insert into !table_name! %5 || exit /b 3
  20. )
  21. exit /b 0
  22. goto :eof
  23. :insert into [table_name] [(sum1,sum2,...)] values [(sum1,sum2,...)]
  24. rem 返回0插入数值成功,返回1该表不存在,返回2插入数值超过表项,超出部分被丢弃,返回3插入数值变量失败。
  25. 1>nul 2>nul set %2 || exit /b 1
  26. set "table_name=%2"
  27. set is=%~3
  28. set is=!is:,= !
  29. set /a in=0
  30. if %4.==values. call :scinsert %3 %5
  31. for %%i in (!is!) do (
  32. set /a in+=1
  33. if !in! gtr !cn! exit /b 2
  34. if !in! lss 2 (set "iis=%table_name%_ %%i") else ( set "iis=!iis! %%i")
  35. )
  36. set "%iis%=values" || exit /b 3
  37. set %table_name%
  38. exit /b 0
  39. goto :eof
  40. :scinsert [(sum1,sum2,...)] [(sum1,sum2,...)]
  41. rem 选择部分KEY插入值,其它值如果没有就用nul填充,保留功能。
  42. goto :eof
  43. :update [table] set key = values "where ..."
  44. 1>nul 2>nul set %1 || exit /b 1
  45. set "table_name=%1"
  46. set "key=%~3"
  47. set /a kn=0
  48. set "values=%~4"
  49. set "run=%~5"
  50. set "tokens="
  51. for %%i in (!key!) do (
  52. set /a kn+=1
  53. if not defined tokens (set "tokens=!%%i!") else (set tokens=!tokens!,!%%i!)
  54. )
  55. echo !tokens!
  56. rem tokens 原本打算用进 for /f ,但定位查询容易,替换缺很难。打算用shift来做这个功能。
  57. goto :eof
  58. :shift [str]
  59. rem 对照tokens来移位,主要用来定位值的位置,替换后重组字符串 =.=
  60. shift
  61. for /l %%i in (1,1,) do set "str=!str! %1"
  62. goto :eof
  63. :select value* from [table_name] [where ...]
  64. set "table_name=%3"
  65. rem select * from table1 where field1 like ’%value1%’
  66. goto :eof
  67. :delete from [table] [where ...]
  68. goto :eof
  69. :save
  70. rem 保存数据库到文件。
  71. goto :eof
复制代码

[ 本帖最后由 caruko 于 2010-7-13 18:17 编辑 ]

看起来代码很美,可惜我看不懂,这个只是模拟吗,能不能操作SQL?

TOP

update 部分功能完成。 开始做最麻烦的where 了..
  1. @echo off
  2. setlocal ENABLEDELAYEDEXPANSION
  3. :main
  4. call :create table test "姓名,日期,年龄,date" values "玉清,1984-1-1,28,test123"
  5. echo create+insert后
  6. set %table_name%
  7. call :update test set "姓名,年龄" = "陆海,31"
  8. echo update后
  9. set %table_name%
  10. goto :eof
  11. :create table [table_name] [(sum1,sum2,...)] values [(sum1,sum2,...)]
  12. rem 返回0创建成功,%cs%,%cn%,%table_name%可以直接调用,返回1表示已经存在该表,返回2表示数值太长,返回3表示创建初始数值错误
  13. 1>nul 2>nul set %2 && exit /b 1
  14. set "table_name=%2"
  15. set "cs=%~3"
  16. set "cs=!cs:,= !"
  17. set /a cn=0
  18. for %%i in (!cs!) do (
  19. set /a cn+=1
  20. set /a %%i=!cn!
  21. )
  22. set "!table_name!_ !cs!=table" || exit /b 2
  23. if %4.==values. (
  24. call :insert into !table_name! %5 || exit /b 3
  25. )
  26. exit /b 0
  27. goto :eof
  28. :insert into [table_name] [(sum1,sum2,...)] values [(sum1,sum2,...)]
  29. rem 返回0插入数值成功,返回1该表不存在,返回2插入数值超过表项,超出部分被丢弃,返回3插入数值变量失败。
  30. 1>nul 2>nul set %2 || exit /b 1
  31. set "table_name=%2"
  32. set is=%~3
  33. set is=!is:,= !
  34. set /a in=0
  35. if %4.==values. call :scinsert %3 %5
  36. for %%i in (!is!) do (
  37. set /a in+=1
  38. if !in! gtr !cn! exit /b 2
  39. if !in! lss 2 (set "iis=%table_name%_ %%i") else ( set "iis=!iis! %%i")
  40. )
  41. set "%iis%=values" || exit /b 3
  42. exit /b 0
  43. goto :eof
  44. :scinsert [(sum1,sum2,...)] [(sum1,sum2,...)]
  45. rem 选择部分KEY插入值,其它值如果没有就用nul填充,保留功能。
  46. goto :eof
  47. rem ===================================================================================\
  48. rem update 功能
  49. :update [table] set key = values "where ..."
  50. 1>nul 2>nul set %1 || exit /b 1
  51. set "table_name=%1"
  52. set "key=%~3"
  53. set "values=%~4"
  54. set "run=%~5"
  55. set "tokens="
  56. for %%i in (!key!) do set "tokens=!tokens! !%%i!"
  57. set "str1="
  58. for /f "delims=*" %%i in ('set !table_name!_ ^| findstr /v "=table$"') do (
  59. set "str1=%%i"
  60. set "str1=!str1:~0,-7!"
  61. set "str1=!str1:%table_name%_= !"
  62. call :shift !str1!
  63. call :reset %%i
  64. )
  65. goto :eof
  66. rem ===================================================================================/
  67. rem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  68. rem ===================================================================================\
  69. rem 定位到准确key的函数
  70. rem 想到了新的方法, findstr "\<num\>" 来对照字符位置,tokens来定位替换字符串
  71. :shift [str]
  72. set "str="
  73. set /a nxn=0
  74. set /a bk=1
  75. set /a tk=0
  76. for %%i in (%*) do (
  77. set /a nxn+=1
  78. echo;!tokens! | findstr "\<!nxn!\>" >nul && set /a bk=0
  79. if !bk! equ 0 (
  80. set /a tk+=1
  81. call :shift2 !tk!
  82. ) else ( set "str=!str! %%i" )
  83. )
  84. goto :eof
  85. rem 替换某个位置values的函数
  86. :shift2 [num]
  87. for /f "tokens=%1 delims=," %%x in ("!values!") do (
  88. set "str=!str! %%x"
  89. set /a bk=1
  90. )
  91. goto :eof
  92. rem =====================================================================================/
  93. :reset [str]
  94. set "xh=%*"
  95. set "!xh:~0,-7!="
  96. set "%table_name%_!str!=values"
  97. goto :eof
  98. rem =====================================================================================\
  99. :select value* from [table_name] [where ...]
  100. set "table_name=%3"
  101. rem select * from table1 where field1 like ’%value1%’
  102. goto :eof
  103. rem =====================================================================================/
  104. rem =====================================================================================\
  105. :delete from [table] [where ...]
  106. goto :eof
  107. rem =====================================================================================/
  108. rem =====================================================================================\
  109. :save
  110. rem 保存数据库到文件。
  111. goto :eof
  112. rem =====================================================================================/
复制代码

TOP

返回列表