I want to join several tables with each other. The database connection is established and I'm getting this error:
SyntaxError: EOL while scanning string literal
import mysql.connector
from mysql.connector import Error
import csv
import pandas as pd
import sqlite3
import numpy as np
cnx = mysql.connector.connect(user = "n.neshat",
passwd = "Start123",
db = "base",
host="192.168.126.29")
cursor = cnx.cursor()
test = ("select * from projects\
join positions\ on projects.id = positions.project_id\
join loads\ on positions.id = loads.position_id\
join equipments\ on positions.id = equipments.position_id\
join module_fields\ on positions.id = module_fields.position_id\
join configurations\ on positions.id = configurations.position_id\
join module_blocks\ on configurations.id = module_blocks.configuration_id")
cursor.execute(test)
myresult = cursor.fetchall()
use """
for multilines string:
test = """select * from projects
join positions on projects.id = positions.project_id
join loads on positions.id = loads.position_id
join equipments on positions.id = equipments.position_id
join module_fields on positions.id = module_fields.position_id
join configurations on positions.id = configurations.position_id
join module_blocks on configurations.id = module_blocks.configuration_id"""
To avoid unintended SQL syntax typos, I recommend using Python's multiline syntax. And also, use aliases:
test = """SELECT * FROM projects pr
INNER JOIN positions po ON pr.id = po.project_id
INNER JOIN loads l ON po.id = l.position_id
INNER JOIN equipments e ON po.id = e.position_id
INNER JOIN module_fields mf ON po.id = mf.position_id
INNER JOIN configurations c ON po.id = c.position_id
INNER JOIN module_blocks mb ON c.id = mb.configuration_id"""
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.