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")
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")
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)
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us