-1

I'm trying to compare a local variable

currentTime = time.time()

with data in my table, under the AuthorID column, to see which value is bigger. Then, I will proceed to update the column 'Reminded' to True. My table is called reminderFormat.

This is how the Database looks: http://prntscr.com/gb9ay1

What is the best way to go about this? I believe I could specifically fetch FutureTime and store it as a variable, then compare it to currentTime. However, would there be a better option for this which is quicker and more efficient using a query?

Thanks

smye
  • 79
  • 1
  • 1
  • 10
  • See [How to format SQL tables in a Stack Overflow post?](https://meta.stackexchange.com/q/96125) – CL. Aug 21 '17 at 16:35

2 Answers2

1

I think, the best practice is to use a single SQL statement to update the records.

Since, SQLite has no boolean value, you need to set the value 1 (instead of True).

For instance, the statement could be:

import time

st = "UPDATE reminderFormat SET Reminded = 1 " \
     "WHERE FutureTime <= {currentTime}".format(currentTime=time.time())

EDIT: Demo

Here is a demo:

import time
import sqlite3

records = [
    (20183740995, 1503330725.0, "testtt", 0),
    (20183740995, 1503330732.0, "testtt", 0),
    (20183740995, 1503331334.0, "testtt", 0),
    (20183740995, 1509999999.0, "testtt", 0),
    ]

con = sqlite3.connect(":memory:")

# Create the table
con.execute("create table reminderFormat(AuthorID, FutureTime, Message, Reminded)")

# Fill the table
con.executemany("INSERT INTO reminderFormat(AuthorID, FutureTime, Message, Reminded) VALUES (?, ?, ?, ?)", records)

curr_time = time.time()
con.execute("UPDATE reminderFormat SET Reminded = 1 WHERE FutureTime <= ?", (curr_time,))

# Print the table contents
for row in con.execute("SELECT AuthorID, FutureTime, Reminded FROM reminderFormat"):
    print(row)

You get:

(20183740995, 1503330725.0, 1)
(20183740995, 1503330732.0, 1)
(20183740995, 1503331334.0, 1)
(20183740995, 1509999999.0, 0)
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
-2

In sqlite3 you can use a Case statement in query to check if your current time is less than or greater than the AuthorID for this your query in sqlite3 would be something like this:

select case AuthorID<Futuretime then 1 else 0 end from table;

This should provide you with 1 and 0 in the column without having to fetch time to python.

Joe Mayo
  • 7,501
  • 7
  • 41
  • 60
Daniyal Ahmed
  • 715
  • 5
  • 11