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选择输出?


- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- SQL 临时表问题 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01