python - 使用python在RDS MySQL中插入数据(IndentationError : unindent does not match any outer indentation level)

标签 python mysql database rds

我使用RDS MySQL,我想用Python代码向表中插入数据,但出现错误

File "DB2.py", line 23
mySql_insert_query = """INSERT INTO empolyee (ID, Name, date) VALUES (1, 'reham', '2019-08-14')"""
IndentationError: unindent does not match any outer indentation level

代码

import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode


 try:
     connection = mysql.connector.connect(host="****",
                                     user="****",
                                     passwd="****",
                                     database="attendance1")

   if connection.is_connected():
      db_Info = connection.get_server_info()
      print("Connected to MySQL Server version ", db_Info)
      cursor = connection.cursor()
      cursor.execute("select database();")
      record = cursor.fetchone()
      print("You're connected to database: ", record)

except Error as e:
      print("Error while connecting to MySQL", e)

  mySql_insert_query = """INSERT INTO empolyee (ID, Name, date) VALUES (1, 'reham', '2019-08-14')"""

   cursor = connection.cursor()
   result = cursor.execute(mySql_insert_query)
   connection.commit()
   print("Record inserted successfully into Laptop table")
   cursor.close()

except mysql.connector.Error as error:
print("Failed to insert record into table {}".format(error))

finally:
   if (connection.is_connected()):
    connection.close()
    print("MySQL connection is closed")

最佳答案

试试这个:- 缩进和“:”基本上取代了 Python 中的 { 和 }。

import mysql.connector
from mysql.connector import Error
from mysql.connector import errorcode


try:
    connection = mysql.connector.connect(host="****",
                                     user="****",
                                     passwd="****",
                                     database="attendance1")

    if connection.is_connected():
        db_Info = connection.get_server_info()
        print("Connected to MySQL Server version ", db_Info)
        cursor = connection.cursor()
        cursor.execute("select database();")
        record = cursor.fetchone()
        print("You're connected to database: ", record)

except Error as e:
    print("Error while connecting to MySQL", e)

    mySql_insert_query = """INSERT INTO empolyee (ID, Name, date) VALUES (1, 'reham', '2019-08-14')"""

    cursor = connection.cursor()
    result = cursor.execute(mySql_insert_query)
    connection.commit()
    print("Record inserted successfully into Laptop table")
    cursor.close()

except mysql.connector.Error as error:
    print("Failed to insert record into table {}".format(error))

finally:
    if (connection.is_connected()):
        connection.close()
        print("MySQL connection is closed")

关于python - 使用python在RDS MySQL中插入数据(IndentationError : unindent does not match any outer indentation level),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58644398/

相关文章:

python - python字符串中的单引号和双引号

mysql - 连接可能不存在的表/记录 sql

mysql - 是否有针对 "insert or update"的非数据库特定命令

mysql - 按表中出现的次数排序

java - 使用 SessionFactory 时 Hibernate 中的奇怪 NumberFormatException

python - 对来自不同用户的多个响应进行评分

python - 如果色调中缺少类别,则条形图中的中心条

php - 如何遍历 $_POST 数组并将数据发送到不同的 MySQL 表

sql - 使用来自 db2 的 SQL 从 XML Clob 中提取数据

python - pylab matplotlib "show"等待窗口关闭