MySQLdb查询命令,未获取正确的数据库(python)
修复:现在只使用mysql.connector包 我现在很少使用python编程,我想创建一个使用登录/注销系统,并将数据库链接到一个自行创建的web平台,用于管理、记录等。。。 现在我想执行一个查询,从我的数据库中获取所有用户,但由于某些原因,我无法获得我尝试过的任何结果:MySQLdb查询命令,未获取正确的数据库(python),python,mysql,mysql-python,Python,Mysql,Mysql Python,修复:现在只使用mysql.connector包 我现在很少使用python编程,我想创建一个使用登录/注销系统,并将数据库链接到一个自行创建的web平台,用于管理、记录等。。。 现在我想执行一个查询,从我的数据库中获取所有用户,但由于某些原因,我无法获得我尝试过的任何结果: # as requested, connector method. def initiate_connection(self): return MySQLdb.connect("localh
# as requested, connector method.
def initiate_connection(self):
return MySQLdb.connect("localhost", "root", "", "tester")
# This works !
def get_database_version(self):
db = self.initiate_connection() # Instantiate db connection
curs = db.cursor() # Server sided cursors - ref more info: https://mysqlclient.readthedocs.io/user_guide.html#cursor-objects
curs.execute("SELECT VERSION();")# Query command
data = curs.fetchone() # Fetch result.
db.close() # Close conn
return data
# This doesnt? :(
def get_users(self):
db = self.initiate_connection() # Instantiate db connection
curs = db.cursor() # Server sided cursors - ref more info: https://mysqlclient.readthedocs.io/user_guide.html#cursor-objects
curs.execute("SELECT name FROM users")# Query command
data = curs.fetchone() # Fetch result.
db.close() # Close conn
return data
但我得到了一个uknown列错误,所以我尝试选择所有内容以查看从该结果中得到的结果:Nonetype,也!我能够从数据库中检索版本,所以我假设im连接正确。
我不知道我做错了什么有什么想法吗
数据库结构也是:
db->tester
table->users
- id
- name
- password
- salt
- email
编辑:
实际误差:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "D:\Programmas\PyCharm Community Edition 2019.2.5\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "D:\Programmas\PyCharm Community Edition 2019.2.5\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/oj/PycharmProjects/RegisteryHandeler/DatabaseHandler.py", line 25, in <module>
print(database_handler().get_users())
File "C:/Users/oj/PycharmProjects/RegisteryHandeler/DatabaseHandler.py", line 18, in get_users
curs.execute("SELECT name FROM users")# Query command
File "C:\Users\oj\PycharmProjects\RegisteryHandeler\venv\lib\site-packages\MySQLdb\cursors.py", line 209, in execute
res = self._query(query)
File "C:\Users\oj\PycharmProjects\RegisteryHandeler\venv\lib\site-packages\MySQLdb\cursors.py", line 315, in _query
db.query(q)
File "C:\Users\oj\PycharmProjects\RegisteryHandeler\venv\lib\site-packages\MySQLdb\connections.py", line 239, in query
_mysql.connection.query(self, query)
MySQLdb._exceptions.OperationalError: (1054, "Unknown column 'name' in 'field list'")
您是否连接到正确的数据库?你能粘贴你在屏幕上看到的实际错误吗?请同时分享实际的代码,例如,启动_连接的参数及其定义,以及描述用户output@richyen我想是这样的,我能够检索数据库版本,用连接方法更新帖子:我知道这可能是个愚蠢的问题,但是,该列是否真的是“name”,而不是类似于带有空格或其他非打印字符的“name”?我这样问是因为我看到了一些情况,如果你搜索一下你的错误1054,你会发现其他人也遇到了类似的问题。你可以使用或之类的工具来检查数据库中的内容。