1

I'm having an operational error when trying to run IDLE program (Python) in Raspberry Pi and it prompt me an error sentences:

Traceback (most recent call last):
 File "/var/www/Sensor-LED.py", line 34, in <module>
   Blink (int(iterations), float(speed))
 File "/var/www/Sensor-LED.py", line 15, in Blink
   curs.execute("INSERT INTO led values(timestamp('now'), (?)")
OperationalError: no such function: timestamp

I use SQLite 3 in Raspberry Pi to create my table and I only created 1 column which is timestamp only.

import sqlite3
import RPi.GPIO as GPIO
import time
import datetime

GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT

dbname='/var/www/ledDB.db'

##Define a function named Blink()
def Blink(numTimes, speed):
    conn=sqlite3.connect(dbname)
    curs=conn.cursor()
    curs.execute("INSERT INTO led values(timestamp('now'), (?))")

    for i in range(numTimes):## Run loop numTimes
        print "Iteration " + str(i + 1)## Print current loop
        GPIO.output(7, True)## Switch on pin 7
        time.sleep(speed)## Wait
        GPIO.output(7, False)## Switch off pin 7
        time.sleep(speed)## Wait
        #commit the changes
        conn.commit()
        conn.close()
    print "Done" ## When loop is complete, print "Done"
    GPIO.cleanup()

## Ask user for total number of blinks and length of each blink
iterations = raw_input("Enter total number of times to blink: ")
speed = raw_input("Enter length of each blink(seconds): ")

## Start Blink() function. Convert user input from strings to numeric data types and
##pass to Blink() as parameters
Blink(int(iterations), float(speed))

Hope you guys could help me out with it as soon as possible.

closed as off-topic by lenik, Milliways, Impulss, RPiAwesomeness, kolin Jul 7 '14 at 8:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be specific to the Raspberry Pi within the scope defined in the help center." – lenik, Milliways, Impulss, RPiAwesomeness, kolin
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    dbname='/var/www/ledDB.db' sounds like a security issue. – lenik Jul 1 '14 at 5:02
  • That's a path to get my database, lenik – user3127380 Jul 1 '14 at 5:03
  • Your for block is out of blink function. I hope this is just of poor markup, because in this state your code won't work properly. This question is definitely about <<General Python Usage>> and you better ask it on StackOverflow – oxfn Jul 1 '14 at 6:19
  • Lenik's probably referring to the location and the fact that it's not obfuscated/encrypted at all. Someone could look at your code and find out where you keep all the "top-secret secrets". – RPiAwesomeness Jul 3 '14 at 18:05
0

You've got the SQL command switched up. The (?) should be within the values([HERE]) and the timestamp('now') should be where the (?) is currently:

Wrong:

##Define a function named Blink()
def Blink(numTimes, speed):
    conn=sqlite3.connect(dbname)
    curs=conn.cursor()
    curs.execute("INSERT INTO led values(timestamp('now'), (?))")

Correct:

##Define a function named Blink()
def Blink(numTimes, speed):
    conn=sqlite3.connect(dbname)
    curs=conn.cursor()
    curs.execute("INSERT INTO led values(?)", (timestamp('now'),))

I believe this is what you need. As of right now, I believe your code is attempting to run the timestamp('now') function within SQLite, and I don't believe SQLite has that.

EDIT: My bad, you probably can't execute a command within a Python SQLite command/connection. This should fix it:

Correct

##Define a function named Blink()
def Blink(numTimes, speed):
    conn=sqlite3.connect(dbname)
    curs=conn.cursor()
    timeNow = time.time()
    curs.execute("INSERT INTO led values(?)", (timeNow,))

I simply added the statement timeNow = time.time() and swapped out timestamp('now') for timeNow.

  • RPi Awesomeness, I now been prompted a new error and its NameError: global name 'timestamp' is not defined – user3127380 Jul 1 '14 at 4:25
  • @user3127380 this is not the place to learn general python programming, please, ask the question on stackoverflow.com – lenik Jul 1 '14 at 5:03
  • I did tried but they either ignore or hold the question, lenik – user3127380 Jul 1 '14 at 6:09
  • There is no timestamp function. Just replace timestamp('now') with time.time() and everything should work fine – oxfn Jul 1 '14 at 6:12
  • 1
    Thanks for your tips, RPi Awesomeness. I modify your method and it works for me. – user3127380 Jul 3 '14 at 12:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.