How to format sqlite select output?(如何格式化sqlite选择输出?)
问题描述
我有一个简单的 SQLite 数据库.一个带有键和名称以及描述列的表.我希望能够格式化数据,以便我可以在 html 选择下拉列表中使用它.这是我尝试过的众多事情之一......
I have a simple SQLite DB. One table with a key and name and a description columns. I want to be able to format the data so I can use it in a html select pulldown. Here's one of the many things I have tried ...
sqlite> select * from ppt_branch;
1|smellopment|where everything sucks all the time
2|development|just a mess
3|stage|all the world's a stage
4|production|DO NOT TOUCH IT!
sqlite> select '<option value=' ppt_branch_id '>' from ppt_branch ;
SQL error: near "'>'": syntax error
sqlite>
...但正如你所看到的那样是不对的.我希望输出像......
... but as you can see that is not right. I was hoping for output like ...
<option value='1'>smellopment</option>
<option value='2'>development</option>
<option value='3'>stage</option>
<option value='4'>production</option>
...但我什至不接近.谁能帮帮我?
... but I am not even close. Can anyone help me out?
推荐答案
好吧,您的语言应该捕获该查询结果并根据需要对其进行格式化.当您干净利落地编写软件(MVC 方法)时,您不要混合数据和格式.这不仅仅是为了有一天非网络部署的可能性,它主要是为了以专门的逻辑结构组织您的工作:一个用于数据,一个用于控制,一个用于查看.
Well, your language should catch that query result and format it as you want. When you write software cleanly (MVC approach), you don't mix data and formatting. It's not just for the possibility of non-web deployment one day, it's primarily to organize your work in specialized logical structures: one for data, one for control, one for viewing.
但这是使用文字的方法.你很接近,你只需要连接:
But here's the way to do it with literals. You were close, you just needed concatenation:
SELECT '<option value=''' || ppt_branch_id || '''>' || ppt_branch_name || '</option>'
FROM ppt_branch;
这篇关于如何格式化sqlite选择输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何格式化sqlite选择输出?


基础教程推荐
- 从字符串 TSQL 中获取数字 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带更新的 sqlite CTE 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01