Python -Tutorial

Python-HOME Python-Einführung Python-Erste Schritte Python-Syntax Python-Kommentare Python-Variablen Python-Datentypen Python-Zahlen Python-Casting Python-Strings Python-Booleans Python-Operatoren Python-Listen Python-Tupel Python-Sets Python-Wörterbücher Python Wenn ... Sonst Python-While-Schleifen Python-For-Schleifen Python-Funktionen Python-Lambda Python-Arrays Python-Klassen/Objekte Python-Vererbung Python-Iteratoren Python-Bereich Python-Module Python-Daten Python-Mathematik Python-JSON Python-RegEx Python-PIP Python Versuchen ... Außer Python-Benutzereingabe Formatierung von Python-Strings

Umgang mit Dateien

Umgang mit Python-Dateien Python-Dateien lesen Python-Dateien schreiben/erstellen Python-Dateien löschen

Python-Module

NumPy-Tutorial Panda-Komplettlösung Scipy-Tutorial

Python-Matplotlib

Matplotlib-Einführung Matplotlib loslegen Matplotlib-Pyplot Matplotlib-Plotten Matplotlib-Marker Matplotlib-Linie Matplotlib-Labels Matplotlib-Raster Matplotlib-Subplots Matplotlib-Scatter Matplotlib-Bars Matplotlib-Histogramme Matplotlib-Kreisdiagramme

Maschinelles Lernen

Einstieg Mittlerer Medianmodus Standardabweichung Perzentil Datenverteilung Normale Datenverteilung Streudiagramm Lineare Regression Polynomiale Regression Mehrfache Regression Skala Trainieren/Testen Entscheidungsbaum

Python-MySQL

MySQL-erste Schritte MySQL-Datenbank erstellen MySQL-Tabelle erstellen MySQL-Einfügung MySQL-Auswahl MySQL-Wo MySQL-Reihenfolge nach MySQL löschen MySQL-Drop-Tabelle MySQL-Update MySQL-Limit MySQL-Beitritt

Python-MongoDB

Beginnen Sie mit MongoDB MongoDB Datenbank erstellen MongoDB-Create-Sammlung MongoDB-Einfügung MongoDB-Suche MongoDB-Abfrage MongoDB-Sortierung MongoDB löschen MongoDB-Drop-Sammlung MongoDB-Update MongoDB-Limit

Python-Referenz

Python-Übersicht Eingebaute Python-Funktionen Python-String-Methoden Python-Listenmethoden Methoden des Python-Wörterbuchs Python-Tupelmethoden Python-Set-Methoden Python-Dateimethoden Python-Schlüsselwörter Python-Ausnahmen Python-Glossar

Modulreferenz

Zufallsmodul Anforderungsmodul Statistikmodul Mathe-Modul cMath-Modul

Python-Anleitung

Listenduplikate entfernen Einen String umkehren Fügen Sie zwei Zahlen hinzu

Python-Beispiele

Python-Beispiele Python-Compiler Python-Übungen Python-Quiz Python-Zertifikat

Python-MySQL -Tabelle erstellen


Erstellen einer Tabelle

Verwenden Sie zum Erstellen einer Tabelle in MySQL die Anweisung "CREATE TABLE".

Stellen Sie sicher, dass Sie den Namen der Datenbank festlegen, wenn Sie die Verbindung erstellen

Beispiel

Erstellen Sie eine Tabelle mit dem Namen "Kunden":

import mysql.connector

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

mycursor = mydb.cursor()

mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")

Wenn der obige Code ohne Fehler ausgeführt wurde, haben Sie jetzt erfolgreich eine Tabelle erstellt.


Überprüfen Sie, ob die Tabelle existiert

Sie können überprüfen, ob eine Tabelle existiert, indem Sie alle Tabellen in Ihrer Datenbank mit der Anweisung "SHOW TABLES" auflisten:

Beispiel

Geben Sie eine Liste der Datenbanken Ihres Systems zurück:

import mysql.connector

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

mycursor = mydb.cursor()

mycursor.execute("SHOW TABLES")

for x in mycursor:
  print(x)


Primärschlüssel

Beim Erstellen einer Tabelle sollten Sie auch eine Spalte mit einem eindeutigen Schlüssel für jeden Datensatz erstellen.

Dies kann durch die Definition eines PRIMARY KEY erfolgen.

Wir verwenden die Anweisung "INT AUTO_INCREMENT PRIMARY KEY", die eine eindeutige Nummer für jeden Datensatz einfügt. Beginnend bei 1 und für jeden Datensatz um eins erhöht.

Beispiel

Primärschlüssel beim Erstellen der Tabelle erstellen:

import mysql.connector

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

mycursor = mydb.cursor()

mycursor.execute("CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))")

Wenn die Tabelle bereits vorhanden ist, verwenden Sie das Schlüsselwort ALTER TABLE:

Beispiel

Primärschlüssel für eine vorhandene Tabelle erstellen:

import mysql.connector

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

mycursor = mydb.cursor()

mycursor.execute("ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY")