简体   繁体   English

如何在 SQLite3 查询中包含环境变量

[英]How to include enviroment variables in SQLite3 query

I'm working on writing a batch file to pull data from multiple SQLite3 databases.我正在编写一个批处理文件来从多个 SQLite3 数据库中提取数据。 I need to set 2 System environment variables then use them two environment variables within my SELECT statement.As this will run against multiple databases that are the same schema, but for different site locations.我需要设置 2 个系统环境变量,然后在我的 SELECT 语句中使用它们两个环境变量。因为这将针对具有相同架构但针对不同站点位置的多个数据库运行。 Currently 14 differen locations.目前有 14 个不同的地点。 What I have come up with so far is my main queryrun.bat file:到目前为止我想出的是我的主要 queryrun.bat 文件:

@echo off
setx /m DateStart '20200101'
setx /m DateEnd '20200103'
sqlite3 SiteID40.db < query40.dat
TIMEOUT /t 3
sqlite3 SiteID41.db < query41.dat

Then the query#.dat files look like this:然后 query#.dat 文件如下所示:

.mode list
.separator ,
.output results40.csv
SELECT * FROM FilesActive WHERE CompactDate >= %DateStart% and CompactDate <= %DateEnd%;
.quit

The %DateStart% and %DateEnd% is where I am wanting to insert the system variables of the same name. %DateStart% 和 %DateEnd% 是我想要插入同名系统变量的地方。 I have tried using $DateStart and %DateStart% both with no luck.我曾尝试使用 $DateStart 和 %DateStart% 都没有运气。

Any constructive help is appreciated.任何建设性的帮助表示赞赏。 I must RE-ITERATE though that this is SQLite3 NOT MS SQL Server or mySQL.我必须重新RE-ITERATE尽管这是SQLite3不是 MS SQL Server 或 mySQL。

Using the .param command in sqlite, something like this should work:在 sqlite 中使用 .param 命令,这样的事情应该可以工作:

Change the .bat call from sqlite3 SiteID40.db < query40.dat to将 .bat 调用从sqlite3 SiteID40.db < query40.dat

sqlite3 SiteID40.db ".param set :DateStart %DateStart%" ".param set :DateEnd %DateEnd%" ".read query.dat"

Change the query#.dat file SELECTs to:将 query#.dat 文件 SELECTs 更改为:

SELECT * FROM FilesActive WHERE CompactDate >= :DateStart and CompactDate <= :DateEnd;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM