0

Any idea why Im getting a syntax error on cGrantCreate

if(Viewdata[4][f] == 'true'):
    # The line below gives me a syntax error, all values Im adding are valid and print separately 
    cGrantCreate = "grant insert on "+data[1][d]+"."+data[2][d]+"."+data[3][d]" to " + data[0][d]
    cur.execute(cGrantCreate)
    con.commit()

4 Answers 4

2

You'll have better luck if you invest more time in making your code easily readable on the page (my advice: spend lines, not width, because it's easier to scan visually and spot errors).

cGrantCreate = (
    "grant insert on " + 
    data[1][d] + 
    "." + 
    data[2][d] + 
    "." + 
    data[3][d] +    # This one was missing.
    " to " +
    data[0][d]
)

Also, learn about str.join().

Sign up to request clarification or add additional context in comments.

Comments

2

Note also that a much more pythonic and understandable notation is to use f-strings, which are both easy to understand (compared to the older .format) and have good performance versus scale. Here is how you would do it for your code.

cGrantCreate = f"grant insert on {data[1][d]}.{data[2][d]}.{data[3][d]} to {data[0][d]}"

This is much easier to read and it will handle if your data is not a string.

Comments

1

You need to add a + sign between data[3][d] and "to".

Your code should look like this:

if(Viewdata[4][f] == 'true'):
    # The line below gives me a syntax error, all values Im adding are valid and print separately 
    cGrantCreate = "grant insert on "+data[1][d]+"."+data[2][d]+"."+data[3][d] + " to " + data[0][d]
    cur.execute(cGrantCreate)
    con.commit()

Comments

1

please add a plus sign between data[3][d] and " to " to fix the error.

Comments

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.