简体   繁体   中英

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?

You're missing quotes around now .

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM