Issue with inserting SQL Query and Python quotation marks

Bug description:
Hi all,

I dont necessarily know if this is a bug or potentially just a user error but I have tried every permutation of the following code and have not been able to get it to work properly. This is mainly a visual issue as the code AFTER the issue still works properly and as expected in Python. I suspect because of how many quotation marks are in the block, it may be messing with the code.

Please let me know if this is just a user error, I cant seem to get it working properly

The code I am running is:

  • after already connecting to a DB -
cur = conn.cursor() *# conn refers to the connection to the DB#*

cur.execute(
    f'''INSERT INTO "{user_id}"(ID, PLACEHOLDER1, PLACEHOLDER2, PLACEHOLDER3, PLACEHOLDER4, WINS, LOSSES, PLACEHOLDER)
    VALUES (1,'{example_text}',1,30,0,0,0,500);
    ''')
    
    # '''

Expected vs Current Behavior:
Current -
In the example above, each quotation string has its appropriate pair however, if I did not include the last # ‘’', the code after the SQL query would not appear visually like regular python code. It would instead appear as if the rest of the code in the Repl shows as its in a string block.

Expected -
Expected behavior would be that the first quote block ( f’‘’ ) would be paired with the last quote block ( ‘’') ) and everything in between the triple quotes would be included in that string.

Steps to reproduce:

  1. Connect to PostGreSQL DB that is provided by Repl for 100 cycles/day
  2. Insert the above code and begin typing new code after the command

Screenshot(s)/Screen Recording:
Before removing

Browser/OS/Device:
Windows - Firefox - Desktop

Replit Profile:
https://replit.com/@isaacjtrigoso

please format your code in triple backticks!

1 Like

Hi!
Backticks dont seem to be working for me in Python and I found some documentation stating backticks were removed in Python 3 :frowning:

No, that means format your code in Ask with backticks.

```py
your code here
```

Like this. It appears that MattDESTROYER, one of our TL4s, did it for you.

As you can see in this screenshot, something is wrong. Notice that the parentheses after your multiline f-string is also orange. It’s not working. You didn’t provide the link to your Repl, but I havea suggestion based on your screenshot: make your function on line 9 one line. To do this, you’ll have to make the f-string into a variable. I think it might be possible in some cases to use multiple lines for function parameters, but that clearly isn’t working in your case. I’d stick with one line and using variables.

OR, use a multiline regular string:

f'INSERT INTO "{user_id}"(ID,TEST1,TEST2)\nVALUES (1,"{name}",0);'
1 Like

Analyzing that, I think see why it did that. They could try moving the string opener onto the line where the function is opened.
Also, you could try """ instead of '''.

1 Like

Hi again!
Thank you both for the support! I will try these methods!
I am currently trying this and it seems to be working because the SQL query does not like single quotes around the table name and double quote in the value line.

currently trying this * sent the reply too early

string1 = f'''INSERT INTO "{user_id}"(ID,test1,test2)'''
string2 = f"""VALUES(1,'{name}',0)"""

cur.execute(string1+string2)

Please. Format your code when posted like this.
That quote really didn’t work right, lol.

2 Likes

Ah sorry! bad habit :sweat_smile: