如何格式化sqlite选择输出?

时间:2023-02-19
本文介绍了如何格式化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选择输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:“未捕获的错误:接收到的数据包顺序错误"关闭 devtools - Electron + MySQL 节点驱动 下一篇:如何在sql中修复错误获取节点这个例子?

相关文章

最新文章