samedi 13 février 2016

SQLite database to store event from arduino

I'm currently making a simple alarm-system for the arduino, using an ultrasonic sensor.

def register():
    time = strftime("%Y-%m-%d %H:%M:%S", gmtime())
    print("At %s" % time)
    return True

This function is called whenever someone passes the sensors (can't show everything - 150 lines). My question is how do I make this store this data into a SQLite database.

This was my attempt:

(I am new to coding, and have never worked with databases)

def register():
    db = sqlite3.connect('pass')
    # Insert a date object into the database
    today = datetime.today()
    c.execute('''INSERT INTO example(created_at) VALUES(?)''', (today,))
    db.commit()

    # Retrieve the inserted object
    c.execute('''SELECT created_at FROM example''')
    row = c.fetchone()
    i = 0
    print('The date is {0}'.format(row[i]))
    i += 1
    db.close()
    return True 

It did kind of work, on the first pass. After that it just printet the time of the first pass over and over again.

Please help :-)

PS: The idea is to later connect this to a Django website.

Aucun commentaire:

Enregistrer un commentaire