Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. conn = sqlite3.connect('mydatabase.db')
  2.         with conn:
  3.             conn.row_factory = sqlite3.Row
  4.             cur = conn.cursor()
  5.             cur.execute("""CREATE TABLE IF NOT EXISTS `Peaks` (
  6.          `state` TINYINT UNSIGNED,
  7.          `tact` SMALLINT UNSIGNED,
  8.          `recorded` DATETIME UNSIGNED NOT NULL)
  9.                       """)
  10.             super_state = None
  11.             tact_cycle = 1
  12.             record_time = datetime.now().strftime('%Y-%m-%d %H')
  13.  
  14.             cur.execute("SELECT * FROM Peaks WHERE STRFTIME('%Y-%m-%d', recorded) == STRFTIME('%Y-%m-%d', 'now') ORDER BY STRFTIME('%Y-%m-%d %H', recorded) DESC LIMIT 1")
  15.             data = cur.fetchone()
  16.             if data is None:
  17.                 cur.execute("INSERT INTO Peaks (state, tact, recorded) VALUES (?, ?, ?)",
  18.                 (super_state, tact_cycle, record_time))
  19.             else:
  20.                 cur.execute("UPDATE Peaks set state=? WHERE STRFTIME('%Y-%m-%d', recorded) == STRFTIME('%Y-%m-%d', 'now') ORDER BY STRFTIME('%Y-%m-%d %H', recorded) DESC LIMIT 1",
  21.                 (super_state))
  22.             conn.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement