- Peewee - Discussion
- Peewee - Useful Resources
- Peewee - Quick Guide
- Peewee - Using CockroachDB
- Peewee - PostgreSQL & MySQL Extensions
- Peewee - SQLite Extensions
- Peewee - Integration with Web Frameworks
- Peewee - Query Builder
- Peewee - Database Errors
- Peewee - Atomic Transactions
- Peewee - User defined Operators
- Peewee - Retrieving Row Tuples/Dictionaries
- Peewee - SQL Functions
- Peewee - Counting & Aggregation
- Peewee - Sorting
- Peewee - Subqueries
- Peewee - Relationships & Joins
- Peewee - Connection Management
- Peewee - Defining Database Dynamically
- Peewee - Using PostgreSQL
- Peewee - Using MySQL
- Peewee - Constraints
- Peewee - Create Index
- Peewee - Delete Records
- Peewee - Update Existing Records
- Peewee - Primary & Composite Keys
- Peewee - Filters
- Peewee - Select Records
- Peewee - Insert a New Record
- Peewee - Field Class
- Peewee - Model
- Peewee - Database Class
- Peewee - Overview
- Peewee - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Peewee - Using MySQL
As mentioned earper, Peewee supports MySQL database through MySQLDatabase class. However, unpke SQLite database, Peewee can’t create a MySql database. You need to create it manually or using functionapty of DB-API comppant module such as pymysql.
First, you should have MySQL server installed in your machine. It can be a standalone MySQL server installed from
You can also work on Apache bundled with MySQL (such as XAMPP downloaded and installed from
).Next, we install pymysql module, DB-API compatible Python driver.
pip install pymysql
The create a new database named mydatabase. We shall use phpmyadmin interface available in XAMPP.
If you choose to create database programmatically, use following Python script −
import pymysql conn = pymysql.connect(host= localhost , user= root , password= ) conn.cursor().execute( CREATE DATABASE mydatabase ) conn.close()
Once a database is created on the server, we can now declare a model and thereby, create a mapped table in it.
The MySQLDatabase object requires server credentials such as host, port, user name and password.
from peewee import * db = MySQLDatabase( mydatabase , host= localhost , port=3306, user= root , password= ) class MyUser (Model): name=TextField() city=TextField(constraints=[SQL("DEFAULT Mumbai ")]) age=IntegerField() class Meta: database=db db_table= MyUser db.connect() db.create_tables([MyUser])
The Phpmyadmin web interface now shows myuser table created.
Advertisements