On Thu, Oct 22, 2020 at 10:58:00AM +0200, Hans Ginzel wrote: > Hello, > > consider this snippet please > > cursor.execute(f"INSERT INTO {table} VALUES (1, '{}');") > SyntaxError: f-string: empty expression not allowed
Escape the braces by doubling them: f"INSERT INTO {table} VALUES (1, '{{}}');" > It is (absolutely) correct to insert empty json into database table field. Sure, but the syntax error has nothing to do with the database. The syntax error occurs long before the SQL gets executed. The problem here is with the f-string, not the call to cursor.execute. We can simplify the example to this: f'{}' and avoid the distraction of JSON, SQL, databases, etc. > Empty expression in f-string should > * (silently) expand as '{}' (opening and closing braces), > * generate a (compile time) warning if requested, e.g. with -W. We could do that, but this is more likely to just hide bugs in the f-string than be useful. -- Steve _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/WAJL4ZSEGASNLHV3L63VSDZH23IV5CKL/ Code of Conduct: http://python.org/psf/codeofconduct/