Search
 
SCRIPT & CODE EXAMPLE
 

SQL

connect python to mysql

import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  port = 8888, #for Mamp users
  database='whatever db you want'
)
print(mydb) 
Comment

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

python simple connect to mysql

import pymysql.cursors
import pymysql

# Connect to the database
connection = pymysql.connect(host='localhost',
                             user='user',
                             password='passwd',
                             db='db',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

try:
    with connection.cursor() as cursor:
        # Create a new record
        sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
        cursor.execute(sql, ('webmaster@python.org', 'very-secret'))

    # connection is not autocommit by default. So you must commit to save
    # your changes.
    connection.commit()

    with connection.cursor() as cursor:
        # Read a single record
        sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
        cursor.execute(sql, ('webmaster@python.org',))
        result = cursor.fetchone()
        print(result)
finally:
    connection.close()
Comment

python connect to mysql

import mysql.connector

# MySql Connection 
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="yourpassowrd",
  database="databasename"
)
Comment

mysql connection python

from sqlalchemy import types, create_engine
import pymysql

try:
	conn = create_engine('mysql+pymysql://user:pass@IP/database_name')
  	print("MySQL Connection Sucessfull!!!!!!!!!!!")

except Exception as err:

	print("MySQL Connection Failed !!!!!!!!!!!")
	print(err)
Comment

PREVIOUS NEXT
Code Example
Sql :: show databases mysql docker 
Sql :: The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement 
Sql :: raw query must include primary key 
Sql :: oracle drop chain step 
Sql :: sql change default collation to utf8 
Sql :: how to add auto increment primary key 
Sql :: oracle next run dates 
Sql :: how to delete view in sql 
Sql :: mysql show column data types 
Sql :: sql getdate minus 1 day without time 
Sql :: generate random data postgresql 
Sql :: postgres killing connections on db 
Sql :: STOP message of how many rows affected sql 
Sql :: django sqllite config 
Sql :: copy from folders in sql server 
Sql :: rename table snowflake 
Sql :: how to select department from table 
Sql :: mysql find foreign key references 
Sql :: get a list of table names and field names from SQL 
Sql :: mysql insert generate serie 
Sql :: psql: error: FATAL: role "postgres" does not exist 
Sql :: group by 15 minute interval sql server 
Sql :: insert current timestamp in postgresql 
Sql :: how to count null values in mysql 
Sql :: oracle list service names 
Sql :: mysql reset auto increment id 
Sql :: postgres format date in select 
Sql :: mysql set primary key 
Sql :: oracle ora-00054 causes 
Sql :: oracle grant select on schema 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =