Renan
Renan

Reputation: 462

SELECT date('now') does not work in Python + SQLite

In the SQLite documentation, it says you can get the current date by running the query

SELECT date('now');

and indeed it works in the SQLite command line:

sqlite> SELECT date('now');
2012-03-03

However, when I try to use it in a Python program, the same query doesn't work:

import sqlite3
conn=sqlite3.connect('results.db')
c=conn.cursor()
c.execute('SELECT date(now);')

says no such column: now.

Any suggestions?

Upvotes: 0

Views: 874

Answers (3)

Jan Bodnar
Jan Bodnar

Reputation: 11637

The 'now' literal can be omitted. If the date() function has no parameters, the 'now' string is assumed. So the script can be written as follows:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sqlite3 as lite

con = lite.connect(':memory:')

with con:

    cur = con.cursor()     
    cur.execute("SELECT date()")
    today = cur.fetchone()[0]

    print today


$ ./today.py
2013-02-05

Upvotes: 0

xeor
xeor

Reputation: 5455

Are you missing some ticks? Do c.execute('SELECT date(\'now\');') works?

Upvotes: 1

grifaton
grifaton

Reputation: 4046

You're missing quotes around now.

Try c.execute("SELECT date('now');")

Upvotes: 4

Related Questions