Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >> cat talist.nim
- from os import fileExists
- import db_sqlite, prompt, strutils
- let db_check = fileExists("tmp/test.db")
- # create the file if it doesn't exist, and initialize it.
- if not db_check:
- let db = open("tmp/test.db", "", "", "")
- db.exec(sql"CREATE TABLE items ( id INTEGER PRIMARY KEY, name VARCHAR(500) NOT NULL , label VARCHAR(100) NOT NULL )")
- db.exec(sql"CREATE TABLE lists ( name VARCHAR(100) NOT NULL )")
- db.exec(sql"CREATE TABLE due_dates ( date real NOT NULL )")
- db.exec(sql"INSERT INTO lists (name) VALUES (?)", "To-Do")
- db.exec(sql"INSERT INTO lists (name) VALUES (?)", "To-Do Today")
- db.exec(sql"INSERT INTO lists (name) VALUES (?)", "Done")
- db.exec(sql"INSERT INTO items (name, label) VALUES (?, ?)", "Finish Note Thing", "To-Do")
- db.close()
- let db = open("tmp/test.db", "", "", "")
- #var output = db.getAllRows(sql"SELECT name FROM sqlite_master WHERE type='table'")
- #var output = db.getAllRows(sql"SELECT * FROM items")
- #
- #for val in output:
- # echo val
- var lists = db.getAllRows(sql"SELECT name FROM lists")
- var index = 0
- proc isInt(value: string): bool =
- var val = true
- try:
- discard parseInt(value)
- except ValueError:
- val = false
- return val
- proc printBox(name: string) =
- var ind = 0
- var line = "---------------"
- echo line
- echo name
- echo line
- var items = db.getAllRows(sql"SELECT name FROM items WHERE label=(?)", lists[index][0])
- for i in items:
- echo line
- echo $ind & ". " & i[0]
- ind = ind + 1
- echo line
- proc readEntry(entry: string): int =
- if entry == "h":
- if index == 0:
- return 0
- else:
- index = index - 1
- return index
- elif entry == "l":
- if index == len(lists) - 1:
- return index
- else:
- index = index + 1
- return index
- elif entry == "q":
- quit(1)
- elif isInt(entry):
- return parseInt(entry)
- else:
- return 0
- proc main() =
- var input = ""
- var prompt = Prompt.init(promptIndicator = ">> ")
- prompt.showPrompt()
- while true:
- prompt.clear()
- echo "\nhelp: \n"
- printBox(lists[index][0])
- let input = prompt.readLine()
- let entry = input
- discard readEntry(entry)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement