Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to create a database in python

import mysql.connector

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

mycursor = mydb.cursor()

mycursor.execute("CREATE DATABASE mydatabase")
Comment

create a database in python

# Here is a sample from https://www.w3schools.com/python/python_mysql_create_db.asp 

import mysql.connector

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

mycursor = mydb.cursor()

mycursor.execute("CREATE DATABASE mydatabase")
Comment

create database python

# Create the Database and Tables (Example data):

import sqlite3

conn = sqlite3.connect('test_database') 
c = conn.cursor()
                   
c.execute('''
          INSERT INTO products (product_id, product_name)

                VALUES
                (1,'Computer'),
                (2,'Printer'),
                (3,'Tablet'),
                (4,'Desk'),
                (5,'Chair')
          ''')

c.execute('''
          INSERT INTO prices (product_id, price)

                VALUES
                (1,800),
                (2,200),
                (3,300),
                (4,450),
                (5,150)
          ''')

conn.commit()

## ## ## ## ## ## ## ## ## 

# Display the results:

import sqlite3
import pandas as pd

conn = sqlite3.connect('test_database') 
c = conn.cursor()
                   
c.execute('''
          SELECT
          a.product_name,
          b.price
          FROM products a
          LEFT JOIN prices b ON a.product_id = b.product_id
          ''')

df = pd.DataFrame(c.fetchall(), columns=['product_name','price'])
print (df)
Comment

PREVIOUS NEXT
Code Example
Python :: python dedent 
Python :: python shuffle list with seed 
Python :: python datetime with timezone 
Python :: how to print all rows in pandas 
Python :: python pandas dataframe from csv index column 
Python :: how to download excel file from s3 using python 
Python :: Get all columns with particular name in string 
Python :: linkedin dynamic scrolling using selenium python 
Python :: read xls file in python 
Python :: selenium python 
Python :: python set a specific datetime 
Python :: python how to change size of a window 
Python :: python write to file csv 
Python :: read pickle file python 
Python :: python get lan ip 
Python :: remove spaces from a list python 
Python :: connect flask with postgresql 
Python :: python json load file 
Python :: python requests cookies 
Python :: user nextcord interactions 
Python :: sin and cos in python 
Python :: draw a circle in python turtle 
Python :: psyche asteroid 
Python :: how to show pandas last record 
Python :: how to fill nan values with mean in pandas 
Python :: if in lambda function python 
Python :: python tkinter frame title 
Python :: set python 3 as default ubuntu 
Python :: unnamed 0 pandas 
Python :: unlimited keyword arguments python 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =