Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.14 KB | None | 0 0
  1. local sqlite3 = require('lsqlite3')
  2. local fetch = require( "fetch" )
  3. local textutils = require( "textutils" )
  4.  
  5. local db = sqlite3.open( "database.sqlite" )
  6.  
  7. local data = {}
  8.  
  9. -- Make sure the table exists before we use it
  10. local function createTable( symbol )
  11.     for row in db:nrows( "SELECT name FROM sqlite_master WHERE type='table' AND name='" .. symbol .. "';" ) do
  12.         return
  13.     end
  14.  
  15.     print( "Creating table for " .. symbol )
  16.     db:exec( "CREATE TABLE `" .. symbol .. [[` (
  17.         `date`  STRING UNIQUE,
  18.         `volume`    INTEGER,
  19.         `open`  REAL,
  20.         `close` REAL,
  21.         `high`  REAL,
  22.         `low`   REAL,
  23.         `adjustedClose` REAL
  24.     ); ]] )
  25. end
  26.  
  27. local function insert( symbol, csvData )
  28.     print( "Inserting data into " .. symbol )
  29.     createTable( symbol )
  30.     local insertStatement = db:prepare( [[ INSERT OR IGNORE INTO `]] .. symbol .. [[` VALUES (:date, :volume, :open, :close, :high, :low, :adjustedClose) ]] )
  31.  
  32.     for i, row in ipairs( csvData ) do
  33.         insertStatement:bind_names( row )
  34.         insertStatement:step()
  35.         insertStatement:reset()
  36.     end
  37.  
  38.     insertStatement:finalize()
  39.     print( "    Complete!" )
  40. end
  41.  
  42. -- get all the data for a certain stock (most recent first)
  43. function data.update( symbol )
  44.     print( "Update data for " .. symbol )
  45.     local data = fetch.downloadCSV( symbol )
  46.     print("    Complete!")
  47.     insert( symbol, data )
  48. end
  49.  
  50. -- get all the data for a certain stock (most recent first)
  51. function data.all( symbol )
  52.     print( "Getting ALL data for " .. symbol )
  53.     local all = {}
  54.     local n = 0
  55.     for row in db:nrows( "SELECT * FROM `" .. symbol .. "`;" ) do
  56.         all[n] = textutils.serialise( row )
  57.         n = n + 1
  58.     end
  59. end
  60.  
  61. -- get all the data for a certain stock (most recent first)
  62. function data.allUntil( symbol, date )
  63.     local all = {}
  64.     local n = 0
  65.     for row in db:nrows( "SELECT * FROM `" .. symbol .. "` WHERE `date` <= date('" .. date .. "') ORDER BY `date` DESC;" ) do
  66.         -- all[n] = textutils.serialise( row )
  67.         print(textutils.serialise( row ))
  68.         all[n] = row
  69.         n = n + 1
  70.     end
  71. end
  72.  
  73. data.update( "AVN.AX" )
  74.  
  75. -- data.all( "AVN.AX" )
  76. -- data.allUntil( "AVN.AX", "2015-11-13" )
  77. -- data.all( "AAPL" )
  78.  
  79. -- createTable( "AAPL" )
  80. -- createTable( "BOOA" )
  81.  
  82. return data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement