简体   繁体   English

如何从 Python 中的 SQL 查询中获取数据

[英]How to fetch data from SQL query in Python

Good Afternoon, I wrote a query in MySQL, and I want to execute the same query in Python.下午好,我在 MySQL 中写了一个查询,我想在 Python 中执行同样的查询。 The Code I wrote as Follows.我写的代码如下。 1. 1.

import mysql.connector
from mysql.connector import Error


try:
connection = mysql.connector.connect(host='localhost',
                                      database='AdventureWorks2012',
                                      user='root',
                                      password='r@#*****')
sql_select_Query = "select * from Person.person"
    cursor = connection.cursor()
    cursor.execute(sql_select_Query)
    records = cursor.fetchall()

However, I'm getting following error message while running part two- ''File "", line 5 password='r@#*****') ^ SyntaxError: unexpected EOF while parsing''但是,我在运行第二部分时收到以下错误消息 - ''File "", line 5 password='r@#*****') ^ SyntaxError: unexpected EOF while parsing''

Any suggestion please how to overcome this problem?任何建议请如何克服这个问题?

You are missing the except clause:您缺少except子句:

try:
    connection = mysql.connector.connect(host='localhost',
                                      database='AdventureWorks2012',
                                      user='root',
                                      password='r@#*****')
except Exception as e:
    print(e)

You should check the documentation for more information.您应该查看documentation以获取更多信息。

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

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