Lesson 13 / 14
apri_db e gomorra
apri_db() opens a SQLite database: it is already in the fratm library, nothing to install. In the playground the database lives in memory; with Node it can be saved to a file.
Put gomorra in front of a template string and write the query in Neapolitan (GomorraSQL): ripigliammo … mmiez 'a … is SELECT … FROM …, arò is WHERE, mettimmo in fila pe is ORDER BY. The compiler translates it and tells you when it is wrong.
A value put in with ${…} goes in as a parameter, never inside the query text: that way nobody can pull a SQL injection on you.
Exercise
Insert a Marinara at 4 euro and print only the pizzas that cost less than 6 euro, cheapest first: arò prezzo < ${6} mettimmo in fila pe prezzo.
chiamm { apri_db, gomorra } da "fratm"
chist è db = apri_db()
db.esegui(gomorra`facimm 'a tavula pizze (nomme TEXT, prezzo REAL)`)
db.esegui(gomorra`nzipp 'ngoppa pizze nomme, prezzo chist ${"Margherita"}, ${5}`)
db.esegui(gomorra`nzipp 'ngoppa pizze nomme, prezzo chist ${"Diavola"}, ${7}`)
chist è pizze = db.ripiglia(gomorra`ripigliammo nomme, prezzo mmiez 'a pizze`)
pe ogni (chist è pizza dint pizze) {
stamm a dì(`${pizza.nomme}: ${pizza.prezzo} euro`)
}
When you are done, the program prints
Marinara: 4 euro
Margherita: 5 euro