Python MySQLLesson 113Free preview
MySQL Create Database
This lesson is part of Python · All levels
Summary
This lesson shows you how to create, select, view, and delete a MySQL database directly from Python code. The SQL CREATE DATABASE command is executed through a cursor, and can then be checked with the SHOW DATABASE. Once the database is created, you need to select it with the USE or add it to the connection parameters.
Key points
- CREATE DATABASE name_db creates a new database, and IF NOT EXISTS prevents errors if they already exist.
- USE name_db selects the database to be used by the current connection session.
- SHOW DATABASES returns a list of database names that can be retrieved with fetchall().
- DROP DATABASE name_db deletes the database and all content in it.
- SQL commands are executed via cursor.execute() and structural changes are applied directly by the server.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.