0

This is the code.

def create_user(self, id: str, password: str):
    query=SQL(
        "CREATE USER {id} WITH PASSWORD {password}".format(
            id=id
            password=password
        )
    )
    try:
        self.cur.execute(
            query=query
        )
    except DuplicateObject:
        print("{id} User already created.".format(id=id))
    else:
        print("{id} User create.".format(id=id))

in the code below

query=SQL(
    "CREATE USER {id} WITH PASSWORD {password}".format(
        id=id
        password=password
    )
)

I am trying to use the class of the sql module for the id and password variables of the query.

id=Identifier(id)
password=SQL(password)  

like the code above.

Using identifiers and SQL will result in a syntax error.
Which class fits the id password variable?

8
  • take a look at: stackoverflow.com/questions/53022587/… Commented Jan 11, 2022 at 15:31
  • password = sql.Literal(password). It is good idea to use the sql module namespace, it makes it clearer where things are coming from. Commented Jan 11, 2022 at 22:59
  • Actually it would be safer to do: qry = sql.SQL("CREATE USER {id} WITH PASSWORD %s").format(id=sql.Identifier('dog')) then: cur.execute(qry, ['test']). Then the password string would be properly escaped. Commented Jan 11, 2022 at 23:07
  • @AdrianKlaver Applying the identifier class to the id variable throws an error. Commented Jan 12, 2022 at 13:47
  • @AdrianKlaver Is the ID an identifier in postgresql? Commented Jan 12, 2022 at 13:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.