Red Cricket
Red Cricket

Reputation: 10470

How to format sqlite select output?

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?

Upvotes: 0

Views: 4781

Answers (1)

MPelletier
MPelletier

Reputation: 16687

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;

Upvotes: 3

Related Questions