Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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

database with python

#create database in python with mysql

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword"
)

mycursor = mydb.cursor()

mycursor.execute("CREATE DATABASE mydatabase")
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

connect with database python

#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 :: python read xlsx file 
Python :: from random input python 
Python :: urllib_errors 
Python :: python classes and objects 
Python :: pandas from range of columns 
Python :: 2nd to last index python 
Python :: django venv activate 
Python :: python test module 
Python :: puython is not equal to 
Python :: delete plotted text in python 
Python :: global array python 
Python :: python get module name 
Python :: multiple line string 
Python :: list remove method in python 
Python :: import user model 
Python :: convert 2 lists into dictionary 
Python :: python regex true false 
Python :: program to replace lower-case characters with upper-case and vice versa in python 
Python :: set default palette seaborn 
Python :: attributes in python 
Python :: get parent of current directory python 
Python :: docstring in python 
Python :: tree implementation in python 
Python :: lambda expression python 
Python :: PermissionError: [Errno 13] Permission denied on flask 
Python :: explode function in python 
Python :: python sandbox 
Python :: how to find the last occurrence of a character in a string in python 
Python :: Converting time python 
Python :: python how to switch between true and false 
ADD CONTENT
Topic
Content
Source link
Name
5+3 =