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

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

PREVIOUS NEXT
Code Example
Sql :: flask marshmallow sqlalchemy 
Sql :: Create boolean column in MySQL with false as default value? 
Sql :: median mysql 
Sql :: mysql select distinct date from timestamp 
Sql :: mysql order by 
Sql :: mysql datetime with timezone offset 
Sql :: dump heroku database to sql 
Sql :: postgresql import data from csv 
Sql :: postgresql parse json array 
Sql :: sqlite3 turn off case sensitive 
Sql :: q operator in sql 
Sql :: oracle view dependencies 
Sql :: how to delete a table in mysql 
Sql :: run sql script from command line 
Sql :: is numeric in sql 
Sql :: sql server split string last 
Sql :: rename column mysql 
Sql :: into sql 
Sql :: /bin/sh: 1: mysql_config: not found 
Sql :: get largest number in database sql 
Sql :: select true if exists on another table or false sqlserver 
Sql :: sql string function update replace 
Sql :: sql right characters 
Sql :: sql select non unique 
Sql :: oracle create table as select 
Sql :: sqlite select split string 
Sql :: raiserror with nowait 
Sql :: move files from one folder to another in sql server 
Sql :: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: 
Sql :: sql get month and year from date 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =