无法从 mysql table 读取任何数据,但我可以插入其中
Can't read any data from mysql table but I can insert into it
我正在使用 python mysql.connector,我可以插入 table,并通过 DBeaver(数据库管理器)检查它并验证数据提交。但是,如果我尝试从 table 到 read/SELECT,我会得到 'None'。我不确定我能从这里做什么。
我的代码:
import mysql.connector, config
# declare vars
cfg = config.Setting(config.argparser())
sql_username = cfg.get_setting('sql', 'username')
sql_password = cfg.get_setting('sql', 'password')
sql_host = cfg.get_setting('sql', 'host')
sql_db = cfg.get_setting('sql', 'db')
db_config = mysql.connector.connect(user=sql_username, password=sql_password, host=sql_host, database=sql_db)
cursor = db_config.cursor(buffered=True)
print(cursor.execute('SELECT "date_created", "date_due", "title", "completed"'
'FROM remind;'))
# query function, checks for errors, returns result of query
# columns: id | date_created | date_due | title | completed
#
def insert(db_id: object, db_table: object, date_created: object, date_due: object, title: object,
completed: object) -> object:
try:
stmt = ("INSERT INTO " + db_table +
"(id, date_created, date_due, title, completed) "
"VALUES (%(id)s, %(date_created)s, %(date_due)s, %(title)s, %(completed)s)")
data = {
'id': db_id,
'date_created': date_created,
'date_due': date_due,
'title': title,
'completed': completed
}
query_result = cursor.execute(stmt, data)
db_config.commit()
except mysql.connector.Error as err:
query_result = ("Insert Query: Something went wrong: {}".format(err))
return query_result
cursor.execute()
将执行 select 查询。 return 类型是 None 意味着 执行已通过 。
如果您想查看结果,请尝试 cursor.fetchall()
。这会获取结果集中的所有行。
cursor.execute('SELECT "date_created", "date_due", "title", "completed"'
'FROM remind;')
resultSet = cursor.fetchall()
for row in resultSet:
print(row)
我正在使用 python mysql.connector,我可以插入 table,并通过 DBeaver(数据库管理器)检查它并验证数据提交。但是,如果我尝试从 table 到 read/SELECT,我会得到 'None'。我不确定我能从这里做什么。
我的代码:
import mysql.connector, config
# declare vars
cfg = config.Setting(config.argparser())
sql_username = cfg.get_setting('sql', 'username')
sql_password = cfg.get_setting('sql', 'password')
sql_host = cfg.get_setting('sql', 'host')
sql_db = cfg.get_setting('sql', 'db')
db_config = mysql.connector.connect(user=sql_username, password=sql_password, host=sql_host, database=sql_db)
cursor = db_config.cursor(buffered=True)
print(cursor.execute('SELECT "date_created", "date_due", "title", "completed"'
'FROM remind;'))
# query function, checks for errors, returns result of query
# columns: id | date_created | date_due | title | completed
#
def insert(db_id: object, db_table: object, date_created: object, date_due: object, title: object,
completed: object) -> object:
try:
stmt = ("INSERT INTO " + db_table +
"(id, date_created, date_due, title, completed) "
"VALUES (%(id)s, %(date_created)s, %(date_due)s, %(title)s, %(completed)s)")
data = {
'id': db_id,
'date_created': date_created,
'date_due': date_due,
'title': title,
'completed': completed
}
query_result = cursor.execute(stmt, data)
db_config.commit()
except mysql.connector.Error as err:
query_result = ("Insert Query: Something went wrong: {}".format(err))
return query_result
cursor.execute()
将执行 select 查询。 return 类型是 None 意味着 执行已通过 。
如果您想查看结果,请尝试 cursor.fetchall()
。这会获取结果集中的所有行。
cursor.execute('SELECT "date_created", "date_due", "title", "completed"'
'FROM remind;')
resultSet = cursor.fetchall()
for row in resultSet:
print(row)