简体   繁体   中英

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'keyword")\'


    url = 'test.com///"asdasdasd'
    name = "test"
    formatURL = url.replace("//","/")
    print(formatURL)

    conn= db.cursor()
    conn.execute("Insert Into website (URL,NAME) VALUES("{}","{}")".format(url,name))
    data_base.commit()

Most likely, Replace operation was not done correctly and I get the error below.

OUTPUT:

> test.com//"asdasdasd 
> pymysql.err.ProgrammingError: (1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'test")\' at line 1')

How can I convert all "//" characters to a '/' character?

You messed up the delimiters of your string. Don't use str.format() to format parameters into sql strings, use parametrized queries:

conn.execute("Insert Into website (URL,NAME) VALUES( %s, %s)", (url,name))

Here is the mess-up:

conn.execute("Insert Into website (URL,NAME) VALUES(" {} "," {} ")".format(url,name))
              111111111111111111111111111111111111111    222    333
                                         unrelated    {}     {}

all the 1 are one strings, all the 2 are another one and all 3 the third string. Both {} are unrelated curly braces(?) and the .format(url,name) is only applied to ")" (aka 3 ) which is not a valid format string.

Simply use parametrized queries - they are safer and much easier:

Source: https://xkcd.com/327/ ( License )

https://xkcd.com/327/

and syntax hints for lots of languages: https://bobby-tables.com/python

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM