如何格式化sqlite选择输出?

2021-09-23 00:00:00 html 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;

相关文章