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 vs java 
Python :: django add to cart 
Python :: sudo apt-get install python2-pip 
Python :: pytesseract restrict char 
Python :: create time array whith np.datetime64 
Python :: python cant find keras utils to_categorical 
Python :: 1 12 123 python 
Python :: sqlalchemy create engine Oracle 
Python :: autokeras import colab 
Python :: Code Example of Hashmap in Python 
Python :: difference between local and global variable in python 
Python :: df dtype 
Python :: Python Difference between two timedelta objects 
Python :: pil saves blue images 
Python :: 2 plater die game in python 
Python :: how to get max value and min values in entire dataframe 
Python :: python nearly equal 
Python :: how to make a bot send whatever you dm it into a server discord.py 
Python :: How to find the most similar word in a list in python 
Python :: python __add__ 
Python :: run django server on any network address of the system 
Python :: godot remove node from group 
Python :: command to install python3.6 on mac os 
Python :: h2o dataframe columns drop 
Python :: pandas find column with max value for each row 
Python :: how to use with statement in python 2.5 and earlier 
Python :: webex teams api attach file 
Python :: programmer tool 
Python :: How to change application icon of pygame 
Python :: python += dictionary 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =