【问题标题】:No operator matches the given name and argument types. You might need to add explicit type casts没有运算符与给定的名称和参数类型匹配。您可能需要添加显式类型转换
【发布时间】:2021-12-21 09:54:54
【问题描述】:

我正在使用 PostgreSQL 存储用户名及其相应的加盐和哈希密码,但它不断给我这个错误。

LINE 1: ...egister where USERNAME = 'siddharth' and PASSWORD = '\x24326...
                                                             ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

我的表名是hassle_free_register,其结构如下:

                                       Table "public.hassle_free_register"
  Column  |          Type          | Collation | Nullable |                        Default
----------+------------------------+-----------+----------+-------------------------------------------------------
 user_id  | integer                |           | not null | nextval('hassle_free_register_user_id_seq'::regclass)
 username | character varying(255) |           | not null |
 password | character varying(255) |           | not null |
Indexes:
    "hassle_free_register_pkey" PRIMARY KEY, btree (user_id)
    "hassle_free_register_username_key" UNIQUE CONSTRAINT, btree (username)

用户注册代码:-

   @app.route('/register' ,methods =['POST'])
def register():
   try:
      NAME = request.form['USER_NAME']
      PASSWORD = request.form['USER_PASSWORD']
      if(len(NAME)==0):
         return jsonify({"message" :"USERNAME CANNOT BE EMPTY"}),400
      if(len(PASSWORD)<=8):
         return jsonify({"message" :"PASSWORD LENGTH TOO SHORT"}),400
      if(len(PASSWORD)>=30):
         return jsonify({"message" :"PASSWORD LENGTH TOO LONG"}),400
      HASHEDPASS = bcrypt.hashpw(PASSWORD.encode('utf-8'),bcrypt.gensalt())    
      mycursor.execute("insert into hassle_free_register (USERNAME,PASSWORD) values(%s, %s);",(NAME,HASHEDPASS)) 

      #ERROR IS OCCURING IN THIS LINE
      mycursor.execute("select USER_ID from Hassle_Free_Register where USERNAME = %s and PASSWORD = %s;",(NAME,HASHEDPASS)) 

      data = mycursor.fetchone()
      print(data)
      mycursor.execute("create table {TABLENAME} (PASSWORD_ID int SERIAL NOT NULL PRIMARY KEY,APP_NAME varchar(255) NOT NULL, APP_USERNAME varchar(255) NOT NULL , APP_PASSWORD varchar(255) NOT NULL);".format(TABLENAME = NAME + "_" + str(data[0])))
      mydb.commit()
      return jsonify("REGISTERED SUCCESSFULLY") 
   except TypeError as error:
      print(error)
      return jsonify({"message":str(error)}),403
   except ValueError as error:
      print(error)
      return jsonify({"message":str(error)}),403
   except psycopg2.Error as error:
      print(error)
      return jsonify({"message":str(error)}),403

(评论后编辑) 我是 Python 和 postgrSQL 的新手。请帮忙!

我运行了在 SQL shell (psql) 中给出错误的查询,但它运行成功。

查询:-

hassle_free=# select USER_ID from hassle_free_register where USERNAME = 'siddharth' and PASSWORD = '\x24326224313224587174764d532e6265334d6654354e47514a6d73674f6d72327a75723966747a42542e5a2e4f48374e74446d6e76355353752e7461';

结果:-

 user_id
---------
      39
(1 row)

请帮忙。

【问题讨论】:

  • PASSWORD 是 Postgresql 保留字。尝试用小写双引号,即"password"。不相关,但我建议对 Postgresql 名称使用小写。
  • @Stefanov.sm,password 是一个非保留词,因此您不必引用它:select * from pwd_test where password = 'pwd'; 1 | pwd。见Reserved words
  • @AdrianKlaver 很公平,你是对的。
  • 错误消息中应该有一个ERROR 行,告诉您Postgres 认为正在比较的内容。请将其添加到您的问题中。最好的猜测是HASHEDPASS 以某种Binary type 的形式出现。
  • 我已经在 SQL shell (PSQL) 中运行了查询,查询运行成功,请查看。我想在 python 中运行这个查询。

标签: python postgresql psycopg2


【解决方案1】:

要清楚发生了什么:

create table pwd_test(id int, password varchar);

import psycopg2
import bcrypt 
pwd = bcrypt.hashpw('test_pwd'.encode('utf-8'),bcrypt.gensalt()) 

pwd
b'$2b$12$DTSJSsuuhwgyMdSOqLmb0.4RAk.smxQERas/i7WcR3NKTPtLQfoPK'
# Note the b'
# From here [Binary adaptation](https://www.psycopg.org/docs/usage.html#adapt-binary)
# This gets converted to bytea

cur.execute('insert into pwd_test values(%s,%s)', [2, pwd])
-- On the INSERT it gets cast to a varchar
select * from pwd_test where id = 2;
 id |                                                          password                                                          
----+----------------------------------------------------------------------------------------------------------------------------
  2 | \x243262243132244454534a53737575687767794d64534f714c6d62302e3452416b2e736d7851455261732f6937576352334e4b5450744c51666f504b

# When you do the comparison you are comparing the varchar value in the table to the bytea type that is pwd.

cur.execute('select * from pwd_test where password = %s', [pwd])                                                                                                          
---------------------------------------------------------------------------
UndefinedFunction                         Traceback (most recent call last)
<ipython-input-15-e2bde44ff261> in <module>
----> 1 cur.execute('select * from pwd_test where password = %s', [pwd])

UndefinedFunction: operator does not exist: character varying = bytea
LINE 1: select * from pwd_test where password = '\x24326224313224445...
                                              ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

# You need to cast the varchar to bytea
cur.execute('select * from pwd_test where password::bytea = %s', [pwd]) 

cur.fetchone()
 id |                                                          password                                                          
----+----------------------------------------------------------------------------------------------------------------------------
  2 | \x243262243132244454534a53737575687767794d64534f714c6d62302e3452416b2e736d7851455261732f6937576352334e4b5450744c51666f504b

所以您的选择是在查询中将password 转换为bytea 或将password 的列类型更改为bytea

【讨论】:

  • 谢谢@Adrian klaver 你很好地理解了我的问题,并且非常巧妙地解释了解决方案............我理解我的错误!现在正在运行。
猜你喜欢
  • 1970-01-01
  • 2021-05-13
  • 1970-01-01
  • 2011-04-13
  • 2021-02-05
  • 1970-01-01
  • 2016-01-18
  • 2020-08-26
相关资源
最近更新 更多