Python MySQL Delete From
- Previous Page MySQL Order By
- Next Page MySQL Drop Table
حذف السجلات
يمكنك استخدام جملة "DELETE FROM" لإزالة السجلات من الجدول الحالي:
Example
حذف أي سجلات تحتوي على عنوان "Mountain 21"
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", passwd="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() sql = "DELETE FROM customers WHERE address = 'Mountain 21' mycursor.execute(sql) mydb.commit() print(mycursor.rowcount, "record(s) deleted")
مهم:الرجاء الانتباه إلى الجملة mydb.commit()
إلا سيبقى الجدول كما هو.
الرجاء الانتباه إلى جملة WHERE في قواعد DELETE: جملة WHERE تحدد الأعمدة التي يجب حذفها. إذا تم تجاهل جملة WHERE، سيتم حذف جميع السجلات!
Prevent SQL injection
It is also a good habit to escape any queried value in delete statements.
This is to prevent SQL injection, which is a common network hacker technique that can damage or abuse your database.
The mysql.connector module uses placeholders %s
To escape values in delete statements:
Example
Using placeholders %s
Methods to escape values:
import mysql.connector mydb = mysql.connector.connect( host="localhost", user="yourusername", passwd="yourpassword", database="mydatabase" ) mycursor = mydb.cursor() sql = "DELETE FROM customers WHERE address =" %s" adr = ("Yellow Garden 2", ) mycursor.execute(sql, adr) mydb.commit() print(mycursor.rowcount, "record(s) deleted")
- Previous Page MySQL Order By
- Next Page MySQL Drop Table