Advertisement
Guest User

database

a guest
Jan 21st, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. import pymysql
  2. import datetime
  3. import json
  4.  
  5.  
  6. class market():
  7.     def __init__(self):
  8.         #self.dbTable = table
  9.  
  10.         self.connection = pymysql.connect(host='localhost',
  11.                                           user='root',
  12.                                           password='',
  13.                                           db='market')
  14.         self.cursorObject = self.connection.cursor()
  15.  
  16.     def setTicker(self,ticker):
  17.         self.ticker = ticker
  18.  
  19.     def disconnect(self):
  20.         self.connection.close()
  21.  
  22.     def doesTableExist(self,tablename):
  23.  
  24.         query = "SELECT COUNT(*) FROM information_schema.tables WHERE table_name = '{0}'".format(tablename)
  25.  
  26.         self.cursorObject.execute(query)
  27.         if self.cursorObject.fetchone()[0] == 1:
  28.             print("exists")
  29.             return True
  30.  
  31.         print("false")
  32.         return False
  33.  
  34.     def insertResults(self,re,table):
  35.         results = json.loads(re)
  36.         print(len(results['candles']))
  37.         for days in results['candles']:
  38.             # day = days['candles']
  39.             open = float(days['open'])
  40.             high = float(days['high'])
  41.             low = float(days['low'])
  42.             close = float(days['close'])
  43.             volume = int(days['volume'])
  44.             timestmap = days['datetime'] / 1000
  45.  
  46.             timestmap = datetime.datetime.fromtimestamp(timestmap).strftime('%Y-%m-%d %H:%M:%S')
  47.  
  48.         #print(days)
  49.             insert_post = "INSERT IGNORE INTO {} " \
  50.                       "(open,high,low,close,volume,symbol,timestamp)" \
  51.                       "VALUES ({},{},{},{},{},{},{});".format(table,open,high,low,close,volume,'"{}"'.format(self.ticker),'"{}"'.format(timestmap))
  52.             print(insert_post)
  53.             self.cursorObject.execute(insert_post)
  54.         self.connection.commit()
  55.  
  56.     def printResponse(self,re):
  57.         results = json.loads(re)
  58.         for days in results['candles']:
  59.             print(days)
  60.  
  61.  
  62.     def getLastUpdated(self,ticker):
  63.         query = 'SELECT timestamp FROM {} ORDER BY ID DESC LIMIT 1'.format(ticker)
  64.         self.cursorObject.execute(query)
  65.         return self.cursorObject.fetchone()
  66.  
  67.     def getFromPeriod(self,ticker,start,end):
  68.         query = 'select * from {} where timestamp >="{}"  and timestamp <="{}"'.format(ticker,start,end)
  69.         self.cursorObject.execute(query)
  70.         return self.cursorObject.fetchall();
  71.  
  72.     def createTable(self,ticker):
  73.         query = 'CREATE TABLE {}(ID int NOT NULL AUTO_INCREMENT,open float,high float,low float,close float,volume int,timestamp datetime,PRIMARY KEY (ID))'.format(ticker)
  74.         self.cursorObject.execute(query)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement