0

I'm creating creating password using below code

def hashing_password(password = None):
    try:
        salt = os.urandom(16)
       
        key = hashlib.pbkdf2_hmac(
            'sha256',
            password.encode('utf-8'),
            salt,
            100000,
            dklen=128
        )

        password = salt + key
        return password
    
    except Exception as e:
        print(e)

After creating hash password I'll be inserting it into postgres database using below code

def insert_data(self):
        try:
            db = self.get_config()
            conn = psycopg2.connect(**db)
            try:
                conn.autocommit = True
                cur = conn.cursor()
                command = self.generate_insert_commands()
                # the below is my postgres sql statement
                INSERT INTO fq52yrwj6i09v3pcusr (uid,mid,user_name,password)
                VALUES (1,1,'admin','b'\x89A.\xe2\xc9^\x99\x9e\xc6F\xafqZ\x96\x04/}g\xc2\x8b\x0f\xf8\xf9\x01\x83k>n\x9e\x9e\x0b3\xd7\xd8\'\xe4\xe7<\x0f\xe3K\x9c\x1b\xa7j\xf8\xaapR\xf51\xeaT\xcf\xfe\xbbX\xb9}@!\x04\xe4d \xd5\xb5\x9d\xa4\xd2\x8b\x96\x98\x9e\x1dB\xda\xa7Y\xef3g$J\xfc\xd4\t\xe9\x04\xf1>\xfc\xae+\x14\xcf\xe6\xe2\x10\x92}\xa1\xb8/E\x9c\x82\xe5\x00}u0\x8e\xa2\x07\x87\xfd(\x05\x81\xc9\x1f"i\xa1\xdd\x01\x91\x9f\xefH\x00bj\x02\xe7\xcatA>\xfd\xaa\x1d\xa1'')

                cur.execute(command)
                print('insert new user, mac address and license')
                cur.close()
                return 'success'

            except Exception as e:
                return str(e)
            finally:
                if conn is not None:
                    conn.close()
        except Exception as e:
            return str(e)

I'm getting below error when I'm trying insert this

syntax error at or near "\"
LINE 2: ... VALUES (1,1,'admin','b'\x89A.\xe2...
^

How to resolve this?

Mike Scotty
  • 9,803
  • 5
  • 33
  • 45
Saranraj K
  • 353
  • 5
  • 14
  • Use parameter substitution to interpolate values into your query, then all the necessary variable quoting will be done automatically. See the linked duplicate. – snakecharmerb Sep 23 '21 at 11:15

0 Answers0