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 :: split string python 
Python :: convert string to number python 
Python :: python removing duplicate item 
Python :: tkinter simple application 
Python :: math module sin() function in python 
Python :: file storage django 
Python :: python dataframe calculate difference between columns 
Python :: lowercase python 
Python :: jupyter notebook cell background color 
Python :: #remove a sublist from a list-use remove method 
Python :: interface, abstract python? 
Python :: permutation of a string in python 
Python :: shuffle text lines python 
Python :: what are args and kwargs in python 
Python :: largest number python 
Python :: strip characters from a string python 
Python :: index duplicates python 
Python :: select rows with multiple conditions pandas query 
Python :: how to create a button using tkinter 
Python :: python how to raise an exception 
Python :: how to get more than one longest string in a list python 
Python :: datetime print the current time 
Python :: hyperparameters 
Python :: python area calculator 
Python :: dict to string 
Python :: Regular Expression to Stop at First Match 
Python :: python dict if key does not exist 
Python :: to_frame pandas 
Python :: text generate gpt 2 huggingface 
Python :: python csv delete all rows 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =