“Erstellen Sie eine Datenbank in Python” Code-Antworten

So erstellen Sie eine Datenbank in Python

import mysql.connector

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

mycursor = mydb.cursor()

mycursor.execute("CREATE DATABASE mydatabase")
Zac

Erstellen Sie eine Datenbank 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")
CompSciGeek

Datenbank Python erstellen

# 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)
JoeyTatu

Ähnliche Antworten wie “Erstellen Sie eine Datenbank in Python”

Fragen ähnlich wie “Erstellen Sie eine Datenbank in Python”

Weitere verwandte Antworten zu “Erstellen Sie eine Datenbank in Python” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen