简体   繁体   English

LEFT 函数不适用于 python 中的 sqlite3

[英]LEFT function is not working with sqlite3 in python

I am trying to run the following SQL query in python, I have set up a mock database below.我正在尝试在 python 中运行以下 SQL 查询,我在下面设置了一个模拟数据库。

import sqlite3
from datetime import datetime
conn = sqlite3.connect('database.db')

conn.execute("""CREATE TABLE IF NOT EXISTS books (
            title TEXT,
            author TEXT,
            pages INTEGER,
            published INTEGER                
            )""")

   values = ('Deep Learning', 
      'Ian Goodfellow et al.', 
      775, 
      datetime(2016, 11, 18).timestamp())

conn.execute("""INSERT INTO books VALUES (?, ?, ?, ?)""", values)
r = conn.execute("SELECT LEFT(title,2) FROM books")
print(r.fetchall())

However I get the error:但是我收到错误:

  File "C:/Users/Daniel/Pictures/AllanGray/Revolut/stack.py", line 18, in <module>
    r = conn.execute("SELECT LEFT(title,2) FROM books")
  sqlite3.OperationalError: near "(": syntax error

If I just run a normal basic select statement, it works.如果我只是运行一个普通的基本选择语句,它就可以工作。 What is going wrong with the LEFT function? LEFT 函数有什么问题?

There is no LEFT() function in SQLite. SQLite 中没有LEFT()函数。
Use substr() :使用substr()

SELECT SUBSTR(title, 1, 2) FROM books

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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