Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 617 Vote(s) - 3.41 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I connect to a MySQL Database in Python?

#21
Best way to connect to MySQL from python is to Use MySQL Connector/Python because it is official Oracle driver for MySQL for working with Python and it works with both Python 3 and Python 2.

follow the steps mentioned below to connect MySQL

1. install connector using pip

`pip install mysql-connector-python`

or you can download the installer from [

[To see links please register here]

][1]

2. Use `connect()` method of mysql connector python to connect to MySQL.pass the required argument to `connect()` method. i.e. Host, username, password, and database name.

3. Create `cursor` object from connection object returned by `connect()`method to execute SQL queries.

4. close the connection after your work completes.

**Example**:

import mysql.connector
from mysql.connector import Error
try:
conn = mysql.connector.connect(host='hostname',
database='db',
user='root',
password='passcode')
if conn.is_connected():
cursor = conn.cursor()
cursor.execute("select database();")
record = cursor.fetchall()
print ("You're connected to - ", record)
except Error as e :
print ("Print your error msg", e)
finally:
#closing database connection.
if(conn.is_connected()):
cursor.close()
conn.close()

Reference - [

[To see links please register here]

][2]

**Important API of MySQL Connector Python**

- For DML operations - Use `cursor.execute()` and `cursor.executemany()` to run query. and after this use `connection.commit()` to persist your changes to DB

- To fetch data - Use `cursor.execute()` to run query and `cursor.fetchall()`, `cursor.fetchone()`, `cursor.fetchmany(SIZE)` to fetch data

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through