MySQL-Tabellen auflisten
Eine Liste der verfügbaren Tabellen innerhalb einer MySQL-Datenbank liefert der MySQL-Befehl SHOW TABLES. Das folgende Beispiel soll Ihnen zeigen wie Sie in Python eine Verbindung zum MySQL-Server aufbauen, eine MySQL-Datenbank selektieren, die darin verfügbaren MySQL-Tabellen abfragen und ausgeben können und anschließend die MySQL-Verbindung wieder trennen. Es wird hier vorausgesetzt, dass Sie die Python-Erweiterung python-mysqldb installiert haben.
Erstellen Sie mit einem Texteditor die Datei mysql_show_tables.py.
pi@raspberrypi ~ $ vi mysql_show_tables.py
Fügen Sie den folgenden Python-Code in diese Datei ein. Ändern Sie anschließend die Einstellungen (host, username, password und database) entsprechend Ihrer Konfiguration ab.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Creation: 15.06.2015
# Last Update: 15.06.2015
#
# Copyright (c) 2015 by Georg Kainzbauer <http://www.gtkdb.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# import required modules
import MySQLdb
import sys
# declaration of default mysql settings
host = 'localhost'
username = 'root'
password = 'StrengGeheim'
database = 'MyDatabase'
# main function
def main():
try:
# connect to MySQL database
mysql = MySQLdb.connect(host, username, password, database)
# create cursor to access the database
cursor = mysql.cursor()
# execute MySQL command to get all available tables
cursor.execute("SHOW TABLES;")
# get response of the MySQL command
tables = cursor.fetchall()
# print available tables
for table in tables:
print table[0]
except MySQLdb.Error, e:
# print error message
print "Error %d: %s" % (e.args[0], e.args[1])
# quit python script
sys.exit(1)
finally:
# close open MySQL connection
if mysql:
mysql.close()
if __name__ == '__main__':
main()
# -*- coding: utf-8 -*-
#
# Creation: 15.06.2015
# Last Update: 15.06.2015
#
# Copyright (c) 2015 by Georg Kainzbauer <http://www.gtkdb.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# import required modules
import MySQLdb
import sys
# declaration of default mysql settings
host = 'localhost'
username = 'root'
password = 'StrengGeheim'
database = 'MyDatabase'
# main function
def main():
try:
# connect to MySQL database
mysql = MySQLdb.connect(host, username, password, database)
# create cursor to access the database
cursor = mysql.cursor()
# execute MySQL command to get all available tables
cursor.execute("SHOW TABLES;")
# get response of the MySQL command
tables = cursor.fetchall()
# print available tables
for table in tables:
print table[0]
except MySQLdb.Error, e:
# print error message
print "Error %d: %s" % (e.args[0], e.args[1])
# quit python script
sys.exit(1)
finally:
# close open MySQL connection
if mysql:
mysql.close()
if __name__ == '__main__':
main()
Nachdem Sie das Python-Skript angepasst und abgespeichert haben, können Sie dieses wie folgt aufrufen.
pi@raspberrypi ~ $ python mysql_show_tables.py
MyTable
MyTable
Dieser Eintrag wurde am 17.08.2015 erstellt.
Direkter Link zu dieser Seite: http://www.gtkdb.de/index_31_2711.html
[ Zur Startseite ] [ Zur Kategorie ]
© 2004-2021 by Georg Kainzbauer