标题: [特效代码] [分享]批处理版简单SQL [打印本页]
作者: caruko 时间: 2010-7-13 18:04 标题: [分享]批处理版简单SQL
上次看到一老兄的解逻辑题,想了片刻,越想越远,于是有了以下部分代码。
模拟数据库,支持简单的基本SQL语法。
但是部分格式有点不同,参数最好不要包含 空格,=;等符号
只完成了一部分,已测试过create 跟insert ,希望高手给点建议;- @echo off&setlocal ENABLEDELAYEDEXPANSION&setlocal ENABLEEXTENSIONS
- :main
- call :create table test "姓名,日期,年龄,date" values "玉清,1984-1-1,28,test123"
- call :update test set "姓名,年龄" = "陆海,31"
- goto :eof
-
-
-
- :create table [table_name] [(sum1,sum2,...)] values [(sum1,sum2,...)]
- rem 返回0创建成功,%cs%,%cn%,%table_name%可以直接调用,返回1表示已经存在该表,返回2表示数值太长,返回3表示创建初始数值错误
- 1>nul 2>nul set %2 && exit /b 1
- set "table_name=%2"
- set "cs=%~3"
- set "cs=!cs:,= !"
- set /a cn=0
- for %%i in (!cs!) do (
- set /a cn+=1
- set /a %%i=!cn!
- )
- set "!table_name!_ !cs!=table" || exit /b 2
- if %4.==values. (
- call :insert into !table_name! %5 || exit /b 3
- )
- exit /b 0
- goto :eof
-
- :insert into [table_name] [(sum1,sum2,...)] values [(sum1,sum2,...)]
- rem 返回0插入数值成功,返回1该表不存在,返回2插入数值超过表项,超出部分被丢弃,返回3插入数值变量失败。
- 1>nul 2>nul set %2 || exit /b 1
- set "table_name=%2"
- set is=%~3
- set is=!is:,= !
- set /a in=0
- if %4.==values. call :scinsert %3 %5
- for %%i in (!is!) do (
- set /a in+=1
- if !in! gtr !cn! exit /b 2
- if !in! lss 2 (set "iis=%table_name%_ %%i") else ( set "iis=!iis! %%i")
- )
- set "%iis%=values" || exit /b 3
- set %table_name%
- exit /b 0
- goto :eof
-
- :scinsert [(sum1,sum2,...)] [(sum1,sum2,...)]
- rem 选择部分KEY插入值,其它值如果没有就用nul填充,保留功能。
- goto :eof
-
- :update [table] set key = values "where ..."
- 1>nul 2>nul set %1 || exit /b 1
- set "table_name=%1"
- set "key=%~3"
- set /a kn=0
- set "values=%~4"
- set "run=%~5"
- set "tokens="
- for %%i in (!key!) do (
- set /a kn+=1
- if not defined tokens (set "tokens=!%%i!") else (set tokens=!tokens!,!%%i!)
- )
- echo !tokens!
- rem tokens 原本打算用进 for /f ,但定位查询容易,替换缺很难。打算用shift来做这个功能。
- goto :eof
-
- :shift [str]
- rem 对照tokens来移位,主要用来定位值的位置,替换后重组字符串 =.=
- shift
- for /l %%i in (1,1,) do set "str=!str! %1"
- goto :eof
-
-
-
- :select value* from [table_name] [where ...]
- set "table_name=%3"
- rem select * from table1 where field1 like ’%value1%’
- goto :eof
-
-
- :delete from [table] [where ...]
- goto :eof
-
-
- :save
- rem 保存数据库到文件。
- goto :eof
复制代码
[ 本帖最后由 caruko 于 2010-7-13 18:17 编辑 ]
作者: caruko 时间: 2010-7-14 16:15
update 部分功能完成。 开始做最麻烦的where 了..- @echo off
- setlocal ENABLEDELAYEDEXPANSION
- :main
- call :create table test "姓名,日期,年龄,date" values "玉清,1984-1-1,28,test123"
- echo create+insert后
- set %table_name%
- call :update test set "姓名,年龄" = "陆海,31"
- echo update后
- set %table_name%
- goto :eof
-
-
-
- :create table [table_name] [(sum1,sum2,...)] values [(sum1,sum2,...)]
- rem 返回0创建成功,%cs%,%cn%,%table_name%可以直接调用,返回1表示已经存在该表,返回2表示数值太长,返回3表示创建初始数值错误
- 1>nul 2>nul set %2 && exit /b 1
- set "table_name=%2"
- set "cs=%~3"
- set "cs=!cs:,= !"
- set /a cn=0
- for %%i in (!cs!) do (
- set /a cn+=1
- set /a %%i=!cn!
- )
- set "!table_name!_ !cs!=table" || exit /b 2
- if %4.==values. (
- call :insert into !table_name! %5 || exit /b 3
- )
- exit /b 0
- goto :eof
-
- :insert into [table_name] [(sum1,sum2,...)] values [(sum1,sum2,...)]
- rem 返回0插入数值成功,返回1该表不存在,返回2插入数值超过表项,超出部分被丢弃,返回3插入数值变量失败。
- 1>nul 2>nul set %2 || exit /b 1
- set "table_name=%2"
- set is=%~3
- set is=!is:,= !
- set /a in=0
- if %4.==values. call :scinsert %3 %5
- for %%i in (!is!) do (
- set /a in+=1
- if !in! gtr !cn! exit /b 2
- if !in! lss 2 (set "iis=%table_name%_ %%i") else ( set "iis=!iis! %%i")
- )
- set "%iis%=values" || exit /b 3
- exit /b 0
- goto :eof
-
-
- :scinsert [(sum1,sum2,...)] [(sum1,sum2,...)]
- rem 选择部分KEY插入值,其它值如果没有就用nul填充,保留功能。
- goto :eof
-
-
- rem ===================================================================================\
- rem update 功能
- :update [table] set key = values "where ..."
- 1>nul 2>nul set %1 || exit /b 1
- set "table_name=%1"
- set "key=%~3"
- set "values=%~4"
- set "run=%~5"
- set "tokens="
- for %%i in (!key!) do set "tokens=!tokens! !%%i!"
- set "str1="
- for /f "delims=*" %%i in ('set !table_name!_ ^| findstr /v "=table$"') do (
- set "str1=%%i"
- set "str1=!str1:~0,-7!"
- set "str1=!str1:%table_name%_= !"
- call :shift !str1!
- call :reset %%i
- )
- goto :eof
- rem ===================================================================================/
- rem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- rem ===================================================================================\
- rem 定位到准确key的函数
- rem 想到了新的方法, findstr "\<num\>" 来对照字符位置,tokens来定位替换字符串
- :shift [str]
- set "str="
- set /a nxn=0
- set /a bk=1
- set /a tk=0
- for %%i in (%*) do (
- set /a nxn+=1
- echo;!tokens! | findstr "\<!nxn!\>" >nul && set /a bk=0
- if !bk! equ 0 (
- set /a tk+=1
- call :shift2 !tk!
- ) else ( set "str=!str! %%i" )
- )
- goto :eof
- rem 替换某个位置values的函数
- :shift2 [num]
- for /f "tokens=%1 delims=," %%x in ("!values!") do (
- set "str=!str! %%x"
- set /a bk=1
- )
- goto :eof
- rem =====================================================================================/
-
-
- :reset [str]
- set "xh=%*"
- set "!xh:~0,-7!="
- set "%table_name%_!str!=values"
- goto :eof
-
-
- rem =====================================================================================\
- :select value* from [table_name] [where ...]
- set "table_name=%3"
- rem select * from table1 where field1 like ’%value1%’
- goto :eof
- rem =====================================================================================/
-
-
-
-
- rem =====================================================================================\
- :delete from [table] [where ...]
- goto :eof
- rem =====================================================================================/
-
-
-
-
- rem =====================================================================================\
- :save
- rem 保存数据库到文件。
- goto :eof
- rem =====================================================================================/
复制代码
作者: fly2sky 时间: 2011-6-30 20:22
看起来代码很美,可惜我看不懂,这个只是模拟吗,能不能操作SQL?
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |