如何仅为特定用户存储数据

时间:2018-03-12 06:53:34

标签: javascript reactjs firebase firebase-realtime-database

我想将社会维护账单存储在每个用户数据节点中。所以我可以根据需要访问它们。我想用新记录更新已存在的记录,但我不知道如果firebase生成随机密钥,我如何获得特定记录。在这里,我评论了推送数据的行。我将如何以from Tkinter import * from pymodbus.client.sync import ModbusTcpClient as ModbusClient import time import datetime import RPi.GPIO as GPIO import MySQLdb GPIO.setmode(GPIO.BCM) GPIO.setwarnings(0) #-------------------------------------------------------GPIO GPIO.setup(21, GPIO.OUT)#red led GPIO.setup(20, GPIO.OUT)#yellow led GPIO.setup(16, GPIO.OUT)#green led GPIO.output(21, GPIO.HIGH) GPIO.output(20, GPIO.HIGH) GPIO.output(16, GPIO.HIGH) #-------------------------------------------------------GPIO class Screen(Frame): def __init__(self, master): Frame.__init__(self, master) #-------------------------------------------------------Main self.lbttn_clicks = 0 self.start_button() self.material_button() self.receive_button() self.emergency_button() self.lap_button() self.finish_button() self.minutes = 0 self.seconds = 0 #-------------------------------------------------------Modbus Var self.LedRed_State = False self.LedYellow_State = False self.LedGreen_State = False self.db = MySQLdb.connect("localhost","root","raspberry","DB_ASSY") self.sql = "INSERT INTO Table_Assembly (Finished Product, Lap Time) VALUES (%s, %s)" self.c= self.db.cursor() self.client = ModbusClient('192.168.4.166') self.client.connect() print "ModbusClient connected..." #-------------------------------------------------------Modbus Var self.time_start = time.time() self.pack(fill=BOTH, expand=1) #-------------------------------------------------------Main #-------------------------------------------------------Stopwatch self._start = 0.0 self._elapsedtime = 0.0 self._running = 0 self.timestr = StringVar() # self.makeWidgets() #-------------------------------------------------------Stopwatch #--------------------------------------------------------------------Main funcs def start_button(self): self.gbttn = Button(self) self.gbttn['text'] = "Start" self.gbttn['command'] = lambda: [ f() for f in [self.Start, self.LedGreen, self.LedYellowOff, self.LedRedOff]] self.gbttn.place(x = 100, y = 0) def material_button(self): self.ybttn = Button(self) self.ybttn['text'] = "Request Material" self.ybttn['command'] = self.LedYellow self.ybttn.place(x=100, y=50) def receive_button(self): self.rbttn = Button(self) self.rbttn ['text'] = "Receive Material" self.rbttn ['command'] = self.LedYellowOff self.rbttn.place(x=100, y=100) def emergency_button(self): self.ebttn = Button(self) self.ebttn['text'] = "Emergency" self.ebttn['command'] = lambda: [f() for f in [self.LedRed, self.LedYellowOff, self.LedGreenOff]] self.ebttn.place(x=100,y=150) def lap_button(self): self.lbttn = Button(self) self.lbttn['text'] = "Lap" self.lbttn['command'] = lambda: [ f() for f in [self.lap_count, self.Start, self.insert_to_db]] self.lbttn.place(x=100,y=200) def finish_button(self): self.fbttn = Button(self) self.fbttn['text'] = "Finish" self.fbttn['command'] = lambda: [f() for f in [self.Finish, self.Stop, self.Reset]] self.fbttn.place(x=100, y=250) def lap_count(self): self.lbttn_clicks += 1 print str(self.lbttn_clicks) # def stop_watch(self): # print self.seconds # time.sleep(1) # self.seconds = int(time.time() - self.time_start) #--------------------------------------------------------------------------------------Main funcs #--------------------------------------------------------------------------------------Modbus funcs #--------------------------------------------------------------------------------------Modbus funcs #--------------------------------------------------------------------------------------Stopwatch funcs # def makeWidgets(self): # """ Print the time. """ # l = Label(self, textvariable=self.timestr) # self._setTime(self._elapsedtime) # l.pack(fill=X, expand=NO, pady=2, padx=2) def _print(self): print self._elapsedtime def _update(self): """ Update the label with elapsed time. """ self._elapsedtime = time.time() - self._start self._setTime(self._elapsedtime) self._timer = self.after(50, self._update) def _setTime(self, elap): """ Set the time string to Minutes:Seconds:Hundreths """ minutes = int(elap / 60) seconds = int(elap - minutes * 60.0) hseconds = int((elap - minutes * 60.0 - seconds) * 100) self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, hseconds)) def Start(self): """ Start the stopwatch, ignore if running. """ print self._elapsedtime if not self._running: self._start = time.time() - self._elapsedtime self._update() self._running = 1 def insert_to_db(self): self.sql = "INSERT INTO Table_Assembly ('Finished_Product', 'Lap_Time') VALUES (%s, %s)" try: self.execute(sql,( str(self.lbttn_clicks), str(self._elapsedtime))) self.db.commit() except: self.db.rollback() def read_from_db(self): try: #c.execute("SELECT * FROM TTAB_CPU WHERE ID = (SELCET MAX(ID) FROM TAB_CPU)") self.c.execute("SELECT * FROM Table_Assembly ORDER BY ID DESC LIMIT 1") self.result = self.c.fetchall() if result is not None: print ('Finished_Product: ' , result[0][1], '| Lap_Time: ' , result[0][2]) except: print ("read error") def LedGreen(self): self.rr = self.client.read_coils(0,6) print "Start Button Pressed" if self.LedGreen_State == False: self.rr.bits[3] = True self.rq = self.client.write_coil(3, True) self.rq = self.client.write_coil(0, True) GPIO.output(16, GPIO.LOW) self.LedGreen_State = True def LedGreenOff(self): self.rr = self.client.read_coils(0,6) print "Green light is off" if self.LedGreen_State == True: self.rr.bits[3] = False self.rq = self.client.write_coil(3, False) self.rq = self.client.write_coil(0, False) GPIO.output(16, GPIO.HIGH) self.LedGreen_State = False def LedYellow(self): self.rr = self.client.read_coils(0,6) print "Request Material" if self.LedYellow_State == False: self.rr.bits[4] = True self.rq = self.client.write_coil(4, True) self.rq = self.client.write_coil(1, True) GPIO.output(20, GPIO.LOW) self.LedYellow_State = True def LedYellowOff(self): self.rr = self.client.read_coils(0,6) print "Material replenished" if self.LedYellow_State == True: self.rr.bits[4] = False self.rq = self.client.write_coil(4, False) self.rq = self.client.write_coil(1, False) GPIO.output(20, GPIO.HIGH) self.LedYellow_State = False def LedRed(self): self.rr = self.client.read_coils(0,6) print "Assembly process jammed" if self.LedRed_State == False: self.rr.bits[5] = True self.rq = self.client.write_coil(5, True) self.rq = self.client.write_coil(2, True) GPIO.output(21, GPIO.LOW) self.LedRed_State = True def LedRedOff(self): self.rr = self.client.read_coils(0,6) print "Red light is off" if self.LedRed_State == True: self.rr.bits[5] = False self.rq = self.client.write_coil(5, False) self.rq = self.client.write_coil(2, False) GPIO.output(21, GPIO.HIGH) self.LedRed_State = False def Finish(self): self.rr = self.client.read_coils(0,6) print "Assembly is finished" if self.LedGreen_State == True: self.rr.bits[3] = False self.rq = self.client.write_coil(3, False) self.rq = self.client.write_coil(0, False) GPIO.output(16, GPIO.HIGH) self.LedGreen_State = False if self.LedYellow_State == True: self.rr.bits[4] = False self.rq = self.client.write_coil(4, False) self.rq = self.client.write_coil(1, False) GPIO.output(20, GPIO.HIGH) self.LedYellow_State = False if self.LedRed_State == True: self.rr.bits[5] = False self.rq = self.client.write_coil(5, False) self.rq = self.client.write_coil(2, False) GPIO.output(21, GPIO.HIGH) self.LedRed_State = False def Stop(self): """ Stop the stopwatch, ignore if stopped. """ if self._running: self.after_cancel(self._timer) self._elapsedtime = time.time() - self._start self._setTime(self._elapsedtime) self._running = 0 GPIO.cleanup() def Reset(self): """ Reset the stopwatch. """ self._start = time.time() self._elapsedtime = 0.0 self._setTime(self._elapsedtime) def main(): while 1: insert_to_db() read_from_db() if __name__ == '__main__': try: db = MySQLdb.connect("localhost","root","raspberry","DB_ASSY") self.c= self.db.cursor() except: print ("No connection to server...") win = Tk() win.title("Screen") win.geometry('800x400') app = Screen(win) win.mainloop() 为每条记录生成自动生成的推送键的方式保存和检索数据?

这是我想要存储在特定用户节点中的代码:

firebase realtime database

1 个答案:

答案 0 :(得分:2)

从我在代码中看到的,数据结构是:

users: {
  "$year": {
    "$month": {
        bill_eml: "...",
        bill_name: "...",
        bill_due_date: "...",
        bill_period: "...",
        status: "...",
        total: ...,
        bill_id: "..."
      }
    }
  }
}

在您当前的数据结构中,您可以轻松加载特定年份或该年内特定月份的所有帐单。如果不加载所有账单并在应用程序代码中过滤,就无法查询此结构中特定用户的所有账单。

在NoSQL数据库中,您必须修改数据模型以允许您为应用程序使用的用例。例如:如果您想允许列出特定用户的账单,您可以修改现有数据结构以存储每个用户的账单:

users: {
  "$uid": {
    "$year": {
      "$month": {
          bill_eml: "...",
          bill_name: "...",
          bill_due_date: "...",
          bill_period: "...",
          status: "...",
          total: ...,
          bill_id: "..."
        }
      }
    }
  }
}

现在,您可以通过加载/users/$uid来获取特定用户的帐单。

但是,当然,通过这种修改后的结构,现在再也无法为特定月份的所有用户获取账单。如果您想要两个用例(特定用户的所有账单,特定月份所有用户的所有账单),您可能需要复制一些数据。例如:

users: {
  "$uid": {
    "$year": {
      "$month": {
      }
    }
  }
}
months: {
  "$year": {
    "$month": {
  }
}

如果您是这种数据结构的新手,我强烈建议您阅读NoSQL data modeling。如果您来自更传统的关系数据库背景,我建议您观看Firebase for SQL developers

相关问题