使用python在RDS MySQL中插入数据(缩进错误:未缩进不匹配任何外部缩进级别)
我使用RDS MySQL,我想用Python代码在表中插入数据,但我有一个错误使用python在RDS MySQL中插入数据(缩进错误:未缩进不匹配任何外部缩进级别),python,mysql,database,rds,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.c
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")
该行23周围的行未正确缩进。缩进是Python语法的一部分root@reham-Latitude-5490:/home/reham/A#python db8.py文件“db8.py”,第6行try:^IndentationError:unexpected indentation更新了上述代码,在我这边没有显示任何缩进异常。