python - 使用 python 在 RDS MySQL 中插入数据(IndentationError: unindent does not match any external indentation level)
问题描述
我使用 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")
推荐阅读
- javascript - 检测设备/浏览器是否支持全屏 API
- linux - 我如何从另一个目录访问目录中的文件(linux)
- android-fragments - 当您想在 android studio 3.6.3 中创建新片段时,复选框“包括接口回调”在哪里
- java - 使用 feign 客户端进行内部和外部服务通信
- python - 如何让按钮卡在 tkniter 窗口的底部?
- c# - 为什么我无法使用 C# 获取我网站的内容?
- c# - 我可以创建一个行为类似于向量/列表的 Session 变量吗?
- python - 如何抓取 X 秒后加载的网页?
- java - SpringBoot openJ9 Kubernetes Crash InterpreterEmulator.cpp:208
- amazon-web-services - 为什么我的安全组规则在从私有子网调用到公共子网时不起作用?