SQLite 命令行客户端 + HTA 实现简易UI
仅用于探索可行性,就只实现了 SELECT
。
SQLite 客户端.hta
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<HTA:APPLICATION
APPLICATIONNAME="Demo"
ID="JerryHTA"
VERSION="1.0"
ICON=""
BORDER="dialog"
SCROLL="no"
SINGLEINSTANCE="yes"
CONTEXTMENU="yes"
NAVIGABLE="yes"/>
<meta http-equiv="x-ua-compatible" content="ie=edge"/>
<title>SQLite 客户端 - HTA 版</title>
<style>
body { font-family: Arial, sans-serif; }
#cmdResult { white-space: pre-wrap; }
/* 表格样式 */
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table th,
table td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
/* 表头样式 */
tabl thead th {
background-color: #007BFF;
color: white;
font-weight: bold;
text-transform: uppercase;
}
/* 鼠标悬停效果 */
table tbody tr:hover {
background-color: #f5f5f5;
}
/* 交替行颜色 */
table tbody tr:nth-child(even) {
background-color: #f2f2f2;
}
</style>
<script language="JScript">
function runCmd() {
var cmd = document.getElementById('cmdInput').value;
try {
var shell = new ActiveXObject("WScript.Shell");
var sqlCmd = 'sqlite3.exe MY_DB.db ".mode html" ".headers on" ".width auto" "'+ cmd + '"';
var encodingCmd = 'cmd /C CHCP 65001 > nul & ' + sqlCmd;
var exec = shell.Exec(encodingCmd);
while (exec.Status == 0){}
var Stream = new ActiveXObject("ADODB.Stream");
Stream.Open();
Stream.Type = 2; // Text type
Stream.Charset = "UTF-8";
// 直接从文件读取数据,确保编码正确
Stream.LoadFromFile('sqltemp');
// 读取所有数据
var result = Stream.ReadText(-1);
Stream.Close();
// 清除之前的输出并显示新结果
document.getElementById('cmdResult').innerHTML = '<table>' + result + '</table>';
} catch (e) {
document.getElementById('cmdResult').innerText = "Error: " + e.message;
}
}
</script>
</head>
<body>
<h1>SQLite 客户端</h1>
<textarea id="cmdInput" rows="5" cols="60">SELECT * FROM 订单表;</textarea><br/>
<button onclick="runCmd()">执行</button>
<hr/>
<h2>执行结果</h2>
<pre id="cmdResult"></pre>
</body>
</html>
目录结构
V2
忍不住优化了一版
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge"/>
<HTA:APPLICATION
APPLICATIONNAME="Demo"
ID="JerryHTA"
VERSION="1.0"
ICON=""
BORDER="dialog"
SCROLL="no"
SINGLEINSTANCE="yes"
CONTEXTMENU="yes"
NAVIGABLE="yes"></HTA:APPLICATION>
<title>SQLite 客户端 - HTA 版</title>
<style>
html, body {
font-family: Arial, sans-serif;
height: 100%;
display: flex;
flex-direction: column; /* 设置为垂直方向的Flex布局 */
}
button {
width: 100px;
height: 100px;
}
.line-group {
display: flex;
flex-direction: row;
margin-left: 10px; /* 可以调整这个值来控制 textarea 和按钮之间的间距 */
}
.line-group > button:not(:last-child) {
margin-right: 5px;
}
h2 {
margin: 5px; 0;
padding: 0 0 0 15px;
}
#cmdResult {
flex: 1; /* #cmdResult 占用其父容器的剩余空间 */
white-space: pre-wrap;
background-color: #000;
color: #eee;
padding: 10px;
margin: 5px;
border-radius: 4px;
}
/* 表格样式 */
table {
width: 100%;
border-collapse: collapse;
background-color: #2b2b2b; /* 深色背景 */
}
table th, table td {
border: 1px solid #555; /* 较亮的边框颜色以区分单元格 */
padding: 8px;
text-align: left;
color: #fff; /* 白色文本 */
}
/* 表头样式 */
table thead th {
background-color: #3a3a3a; /* 更深的背景色 */
color: #fff; /* 白色文本 */
font-weight: bold;
text-transform: uppercase;
}
/* 鼠标悬停效果 */
table tbody tr:hover {
background-color: #333; /* 鼠标悬停时的更深背景色 */
}
/* 交替行颜色 */
table tbody tr:nth-child(even) {
background-color: #222; /* 更深的交替行颜色 */
}
</style>
<script language="JScript">
var shell = new ActiveXObject("WScript.Shell");
var Stream = new ActiveXObject("ADODB.Stream");
function runCmd(type) {
var sqlite = sqliteClient.value; // "sqlite3.exe"; //
var db = dbFile.value; // "MY_DB.db"; //
var cmd = document.getElementById('cmdInput').value;
var cmdStr = {
"html": sqlite + ' ' + db + ' ".mode html" ".headers on" ".output sqltemp" "' + cmd + '"',
"excel": sqlite + ' ' + db + ' ".headers on" ".excel" "' + cmd + '"',
"cmd": cmd
}[type];
try {
var encodingCmd = 'cmd /C CHCP 65001 > nul & ' + cmdStr;
var exec = shell.Exec(encodingCmd);
while (exec.Status == 0){}
var result = "执行结束!"
if (exec.ExitCode == 0) {
if(type == "html"){
Stream.Open();
Stream.Type = 2; // Text type
Stream.Charset = "UTF-8";
// 直接从文件读取数据,确保编码正确
Stream.LoadFromFile('sqltemp');
// 读取所有数据
result = Stream.ReadText(-1);
Stream.Close();
shell.Run("cmd /C del sqltemp");
}
if(type == "cmd"){
result = exec.StdOut.ReadAll();
}
}else{
result = exec.StdErr.ReadAll();
}
// 清除之前的输出并显示新结果
document.getElementById('cmdResult').innerHTML = '<table>' + result + '</table>';
} catch (e) {
document.getElementById('cmdResult').innerText = "Error: " + e.message;
}
}
</script>
</head>
<body>
<h1>SQLite 客户端</h1>
<div class="line-group">
<div>
<label for="sqliteClient">SQLite:</label>
<input type="text" id="sqliteClient" name="sqliteClient" value="sqlite3.exe">
</div>
<div>
<label for="dbFile">DB:</label>
<input type="text" id="dbFile" name="dbFile" value="MY_DB.db">
</div>
</div>
<div class="line-group">
<textarea id="cmdInput" rows="5" cols="60">SELECT * FROM 订单表;</textarea>
<div class="line-group">
<button onclick="runCmd('cmd')" accesskey="c">执行CMD(C)</button>
<button onclick="runCmd('html')" accesskey="f">执行SELECT(F)</button>
<button onclick="runCmd('excel')" accesskey="e">导出Excel(E)</button>
</div>
</div>
<hr/>
<h2>执行结果</h2>
<div id="cmdResult"></div>
</body>
</html>
V3
忍不住又来一版
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge"/>
<HTA:APPLICATION
APPLICATIONNAME="Demo"
ID="JerryHTA"
VERSION="1.0"
ICON=""
BORDER="dialog"
SCROLL="no"
SINGLEINSTANCE="yes"
CONTEXTMENU="yes"
NAVIGABLE="yes"></HTA:APPLICATION>
<title>SQLite 客户端 - HTA 版</title>
<style>
html, body {
font-family: Arial, sans-serif;
height: 100%;
display: flex;
flex-direction: column;
background-color: #212529; /* 深色背景 */
color: #dee2e6; /* 文本淡色 */
}
button {
width: 90px;
height: 90px; /* 调整按钮高度以适应界面 */
border: none;
border-radius: 4px;
cursor: pointer;
outline: none;
background-color: #485261;
color: #ffffff;
transition: all 0.3s ease;
}
button:hover {
background-color: #0a58ca;
}
button:active {
background-color: #0a429b;
}
.line-group {
display: flex;
align-items: center;
margin: 5px; /* 增加外边距 */
}
.line-group > *:not(:last-child) {
margin-right: 20px;
}
.input_grp {
display: flex;
align-items: center;
flex: 1;
}
.input_grp > input, select {
flex: 1;
padding: 0.5em;
border: 1px solid #343a40;
border-radius: 4px;
background-color: #343a40;
color: #dee2e6;
}
#cmdInput {
height: auto; /* 自动调整高度以适应内容 */
resize: vertical; /* 允许用户调整高度 */
background-color: #343a40;
color: #dee2e6;
padding: 0.5em;
flex: 1;
}
h1, h2 {
margin: 0;
padding: 10px 0;
font-weight: normal;
}
h1 {
font-size: 24px;
color: #ffffff;
}
h2 {
font-size: 18px;
color: #dee2e6;
}
#cmdResult {
flex: 1;
padding: 15px;
overflow-y: auto; /* 添加滚动条以防内容溢出 */
background-color: #343a40;
color: #dee2e6;
border-radius: 4px;
margin-bottom: 10px;
}
/* 表格样式优化为深色 */
table {
width: 100%;
border-collapse: collapse;
background-color: transparent;
}
table th, table td {
border: 1px solid #495057;
padding: 10px;
text-align: left;
}
table thead th {
background-color: #0d6efd;
color: #ffffff;
font-weight: bold;
text-transform: none;
}
table tbody tr:hover {
background-color: rgba(255, 255, 255, 0.05);
}
table tbody tr:nth-child(even) {
background-color: rgba(255, 255, 255, 0.1);
}
</style>
<script language="JScript">
var shell = new ActiveXObject("WScript.Shell");
var Stream = new ActiveXObject("ADODB.Stream");
function getSql(text){
// 分割文本为数组,每一项为一行
var lines = text.trim().split('\n');
// 处理每行文本,添加双引号并用空格连接
return lines.map(function(line) {
return '"' + line.trim() + '"';
}).join(' '); // 使用空格连接处理后的每一行
}
function runCmd(type) {
var sqlite = sqliteClient.value; // "sqlite3.exe"; //
var db = dbFile.value; // "MY_DB.db"; //
var cmd = document.getElementById('cmdInput').value;
var cmdStr = {
"html": sqlite + ' ' + db + ' ".mode html" ".headers on" ".output sqltemp" "' + cmd + '"',
"excel": sqlite + ' ' + db + ' ".headers on" ".excel" "' + cmd + '"',
"sql": sqlite + ' ' + db + ' ' + getSql(cmd),
"cmd": cmd
}[type];
try {
// alert(cmdStr); // 打印处理后的字符串
var encodingCmd = 'cmd /C CHCP 65001 > nul & ' + cmdStr;
var exec = shell.Exec(encodingCmd);
while (exec.Status == 0){}
var result = "执行结束!"
if (exec.ExitCode == 0) {
if(type == "html"){
Stream.Open();
Stream.Type = 2; // Text type
Stream.Charset = "UTF-8";
// 直接从文件读取数据,确保编码正确
Stream.LoadFromFile('sqltemp');
// 读取所有数据
result = Stream.ReadText(-1);
Stream.Close();
shell.Run("cmd /C del sqltemp");
}
if(type == "cmd" || type == "sql"){
result = exec.StdOut.ReadAll();
}
}else{
result = exec.StdErr.ReadAll();
}
// 清除之前的输出并显示新结果
document.getElementById('cmdResult').innerHTML = '<table>' + result + '</table>';
} catch (e) {
document.getElementById('cmdResult').innerText = "Error: " + e.message;
}
}
// 确保文档加载完毕后再绑定事件
document.onreadystatechange = function() {
if (document.readyState === "complete") {
init();
}
};
var sqlObj = {
"Order": "SELECT * FROM 订单表;",
"A5": "SELECT rowid as '序号', \"名称\", \"YW编号\", \"TY编号\", ext8 as '文档', '=HYPERLINK(E' || (rowid + 1) || ',\\\"打开\\\")' as '操作', * from A5表;",
"A7": "SELECT rowid as '序号', ext10 as \"DC编号\", \"XPDY编号\", \"TY编号\", ext8 as '文档', '=HYPERLINK(E' || (rowid + 1) || ',\\\"打开\\\")' as '操作', * from A7表;",
"A18": "SELECT rowid as '序号',\"DLWZ\", \"HZXM\", ext10 as \"YW编号\", \"TY编号\", ext8 as '文档', '=HYPERLINK(F' || (rowid + 1) || ',\\\"打开\\\")' as '操作', * from A18表;",
"file": "SELECT rowid as '序号', ext8 AS \"文档\", '=HYPERLINK(B' || (rowid + 1) || ',\\\"打开\\\")' as '操作', * FROM \"文件记录表",
}
function init() {
document.getElementById('selectSQL').onchange = function(){
var cmdInput = document.getElementById('cmdInput'); // 获取选中的值,
cmdInput.value = sqlObj[this.value]; // 更新textarea的内容
};
}
</script>
</head>
<body>
<h1>SQLite 客户端</h1>
<div class="line-group">
<div class="input_grp">
<label for="sqliteClient">SQLite:</label>
<input type="text" id="sqliteClient" name="sqliteClient" value="sqlite3.exe">
</div>
<div class="input_grp">
<label for="dbFile">DB:</label>
<input type="text" id="dbFile" name="dbFile" value="MY_DB.db">
</div>
<div class="input_grp">
<label for="selectSQL">选择SQL</label>
<select id="selectSQL" name="selectSQL">
<option value="Order">订单表</option>
<option value="A5">A5 表</option>
<option value="A7">A7 表</option>
<option value="A18">A18 表</option>
<option value="file">文件记录表</option>
</select>
</div>
</div>
<div class="line-group">
<textarea id="cmdInput" rows="5" cols="60">SELECT * FROM 订单表;</textarea>
<div class="line-group">
<button onclick="runCmd('cmd')" accesskey="c">执行CMD(C)</button>
<button onclick="runCmd('sql')" accesskey="s">执行SQL(S)</button>
<button onclick="runCmd('html')" accesskey="f">执行SELECT(F)</button>
<button onclick="runCmd('excel')" accesskey="e">导出Excel(E)</button>
</div>
</div>
<hr/>
<h2>执行结果</h2>
<div id="cmdResult"></div>
</body>
</html>
参考资料
笑虾:SQLite 命令行客户端 + Windows 批处理应用
VBScript Scripting Techniques > HTAs
HTA & WSC Examples
599cd:HTA Tips