---
title: "Prevent SQL Injection"  
description: "Prevent SQL Injection"  
author: "Anonymous User"  
published: 2018-07-09  
updated: 2018-07-09  
canonical: https://www.mindstick.com/forum/34596/prevent-sql-injection  
category: "python"  
tags: ["python"]  
reading_time: 1 minute  

---

# Prevent SQL Injection

What is [Prevent SQL Injection](https://www.mindstick.com/forum/776/are-pdo-prepared-statements-sufficient-to-prevent-sql-injection) in [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python) ?

\

## Replies

### Reply by Prakash nidhi Verma

Prevent [SQL Injection](https://www.mindstick.com/blog/227/sql-injection) :

[prevent SQL](https://www.mindstick.com/forum/159134/how-can-i-prevent-sql-injection-in-php) injections, which is a common web hacking technique to destroy your database import mysql.connector .

```
mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  passwd="yourpassword",
  database="mydatabase"
)
mycursor = mydb.cursor()
sql = "UPDATE customers SET address = %s WHERE address = %s"
val = ("Valley 345", "Canyon 123")
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")
```

Python MySQL Join :

users:

```
{ id: 1, name: 'rohan', fav: 154}, { id: 2, name: 'prakash', fav: 154},
{ id: 3, name: 'Arti', fav: 155},
{ id: 4, name: 'Aditya', fav:},
{ id: 5, name: 'Vishal', fav:}
```

\

Products :

```
{ id: 154, name: 'Chocolate Heaven' }, { id: 155, name: 'Tasty Lemons' },
{ id: 156, name: 'Vanilla Dreams' }
```

```
mycursor = mydb.cursor() 
sql = "SELECT \   users.name AS user, \
  products.name AS favorite \
  FROM users \
  INNER JOIN products ON users.fav = products.id"

mycursor.execute(sql)
myresult = mycursor.fetchall()
for x in myresult:
  print(x)
```


---

Original Source: https://www.mindstick.com/forum/34596/prevent-sql-injection

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
