sqlite3 python内连接语法

时间:2017-03-29 09:55:44

标签: python sqlite

我使用SQLITE3和Python 3为演示项目创建了客户和订单表。我正在尝试从CUSTOMER和ORDERS表中选择数据。

CUSTOMERID是ORDERS表上的外键。

import sqlite3
conn = sqlite3.connect('promhire.db')
print("Opened database successfully")

cursor = conn.execute ('''\
            SELECT ORDERS.ORDERID, ORDERS.CUSTOMERID ORDERS.ORDERDATE CUSTOMERS.CUSTOMERID, CUSTOMERS.FIRSTNAME
            FROM ORDERS
            INNER JOIN CUSTOMERS
            ON ORDERS.CUSTOMERID = CUSTOMERS.CUSTOMERID
            WHERE CUSTOMERS.CUSTOMERID = 1''')
for row in cursor:
    print(row)
conn.close()

screenshot of code

我得到的错误是

"OperationalError: near ".": syntax error"
Highlighting the last line of SQL.

screenshot of error

如何为Python正确编码?

由于

1 个答案:

答案 0 :(得分:1)

在查询中查看逗号(" ,")

select orders.orderid, orders.customerid,orders.orderdate,customers.customerid,customers.firstname from orders ..

编辑:

cursor = conn.execute ('''\
            SELECT ORDERS.ORDERID, ORDERS.CUSTOMERID, ORDERS.ORDERDATE, CUSTOMERS.CUSTOMERID, CUSTOMERS.FIRSTNAME
            FROM ORDERS
            INNER JOIN CUSTOMERS
            ON ORDERS.CUSTOMERID = CUSTOMERS.CUSTOMERID
            WHERE CUSTOMERS.CUSTOMERID = 1''')