問題描述
我有一個簡單的 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選擇輸出?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!