簡體   English   中英

使用python在mysql查詢中插入錯誤

[英]Inserting error in mysql query using python

我試圖以一種不易受到注入的方式將值插入到一行 mysql 數據庫中,但給出了我的語法錯誤。 這是我的一段代碼,導致錯誤:

cursor.execute("INSERT INTO api.mytable(id) VALUES (:id);", {"id": 1}) 

和錯誤:

連接中的錯誤:(1064,“您的 SQL 語法有錯誤;請檢查與您的 MySQL 服務器版本相對應的手冊,以獲取在第 1 行的 ':id)' 附近使用的正確語法”)

代碼你請告訴我我的代碼有什么問題?

我假設id是作為某種輸入給出的! 因此,您始終可以檢查所需的格式並只允許需要的格式! 這是為了避免 SQL 注入!。 因此,如下所示的自然格式應該可以完成這項工作! 這是非常基本的級別檢查!

id_in = input("Here is the id taken " )  ## can be through any source . It is just an example 

if isinstance(id_in,int): ##Please mention the required format here I am assuming it as integer
    cursor.execute("INSERT INTO api.mytable(id) VALUES (%s);", (id_in))
else:
    ##do some  stuff here 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM