Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
TEACHING-MATERIALS/exercises/extra/05_data_persistence/02_sqlite/terminal/todo_recursive.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
39 lines (32 sloc)
910 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
'use strict' | |
const readline = require('readline') | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}) | |
const sqlite3 = require('sqlite3').verbose() | |
const db = new sqlite3.Database('./todo.db') | |
const list = process.argv[2] || 'data' | |
const recursiveAsyncReadLine = () => { | |
rl.question('Command: ', input => { | |
if (input === 'exit') { | |
db.close() | |
return rl.close() | |
} | |
console.log(`You input: "${input}"`) | |
//if(input === 'exit') process.exit(0) | |
if (input.indexOf('add ') === 0) { | |
const item = input.substring(input.indexOf(' ')).trim() | |
console.log(`adding "${item}"`) | |
const sql = `INSERT INTO items(list, item) VALUES("${list}", "${item}")` | |
console.log(sql) | |
db.run(sql, err => { | |
if(!err) console.log(`added ${item}`) | |
recursiveAsyncReadLine() | |
}) | |
} | |
//recursiveAsyncReadLine() | |
}) | |
} | |
recursiveAsyncReadLine() |