Skip to content

Classes 04

David Raynes edited this page Feb 6, 2020 · 5 revisions
  • Hoisting
  • SQL with SQLite
    sqlite3 database.db
        
    CREATE TABLE tasks(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255), description TEXT);
    SELECT * from tasks;
    INSERT INTO tasks (name, description) VALUES ("this is a name", "this is a description");
    INSERT INTO tasks (name, description) VALUES ("this is another name", "this is the other description");
    
    UPDATE tasks SET name = "This is the new name";
    DELETE FROM tasks where id = 2;
        
  • Incorporating SQLite into node
    npm install --save node-sqlite
        
    form(action="/tasks", method="POST")
        input(type="text", name="task_name")
        input(type="text", name="task_description")
        button(type="submit") Submit!
        
    // in server.js
    const sqlite = require('sqlite');
    const dbPromise = sqlite.open('./database.db', { Promise });
    
    // ...
    
    app.post('/task', (request, response) => {
        const name = request.body.task_name;
        const description = request.body.task_description;
        dbPromise.then((db) => {
            db.run(`INSERT INTO tasks (name, description) VALUES ("${name}", "${description}");`);
        });
    
        response.render('index', { name, description });
    });
        
Clone this wiki locally