Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

connecting to mysql database using python

import mysql.connector
from mysql.connector import Error

try:
    connection = mysql.connector.connect(host='localhost',
                                         database='Electronics',
                                         user='pynative',
                                         password='pynative@#29')
    if connection.is_connected():
        db_Info = connection.get_server_info()
        print("Connected to MySQL Server version ", db_Info)
        cursor = connection.cursor()
        cursor.execute("select database();")
        record = cursor.fetchone()
        print("You're connected to database: ", record)

except Error as e:
    print("Error while connecting to MySQL", e)
finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("MySQL connection is closed")
Comment

connecting python with database

import mysql.connector
from mysql.connector import Error
try:
    connection = mysql.connector.connect(host='localhost',
                                         database='techshop',
                                         user='root',
                                         password=' ')
    if connection.is_connected():
        db_Info = connection.get_server_info()
        print("Connected to MySQL Server version ", db_Info)
        cursor = connection.cursor()
        cursor.execute("select database();")
        record = cursor.fetchone()
        print("You're connected to database: ", record)
except Error as e:
    print("Error while connecting to MySQL", e)
finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("MySQL connection is closed")
Comment

how to connect database in python

import mysql.connector
from mysql.connector import Error

try:
    connection = mysql.connector.connect(host='localhost',
                                         database='Electronics',
                                         user='pynative',
                                         password='pynative@#29')
    if connection.is_connected():
        db_Info = connection.get_server_info()
        print("Connected to MySQL Server version ", db_Info)
        cursor = connection.cursor()
        cursor.execute("select database();")
        record = cursor.fetchone()
        print("You're connected to database: ", record)

except Error as e:
    print("Error while connecting to MySQL", e)
finally:
    if connection.is_connected():
        cursor.close()
        connection.close()
        print("MySQL connection is closed")
Comment

database with python connection

#connect to sqlite database in python

import sqlite3

connection = sqlite3.connect("survey.db")
cursor = connection.cursor()
cursor.execute("SELECT Site.lat, Site.long FROM Site;")
results = cursor.fetchall()
for r in results:
    print(r)
cursor.close()
connection.close()
Comment

PREVIOUS NEXT
Code Example
Python :: convert index of a pandas dataframe into a column 
Python :: python copy variable 
Python :: random picker in python 
Python :: pandas dataframe remove rows by column value 
Python :: how to unique list in python 
Python :: get only first 10 columns pandas 
Python :: feature scaling in python 
Python :: python list to string 
Python :: delete directory if exists python 
Python :: get a slice of string in python 
Python :: python resize image in tkinter 
Python :: image rotate in python 
Python :: mouse bottom in pygame 
Python :: create new dataframe with columns from another dataframe pandas 
Python :: matplotlib cheatsheet 
Python :: To View the entire Row and Column in a Dataframe 
Python :: keras linear regression 
Python :: python sorted word frequency count 
Python :: how to delete role discord py rewrite 
Python :: python get last element of iterator 
Python :: pandas sort by columns 
Python :: curl python 
Python :: python numpy vstack 
Python :: convert decimal to hex python 
Python :: csv module remove header title python 
Python :: input and ouput array in python 
Python :: python square all numbers in list 
Python :: isnull().mean() python 
Python :: hardest python questions 
Python :: if else python 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =