Python Contact Book — Tkinter and SQL Server

A collection of desktop GUI applications built with Python. It currently contains Contact Book, a CRUD contact manager that shows how to connect a Tkinter / ttkbootstrap interface to Microsoft SQL Server using pyodbc and stored procedures.

Python projects · Desktop interfaces


Start here

Read the requirements and any configuration below, then follow the usage example. Source paths below are relative to the repository root; run commands from the working directory stated in Usage. This example’s source is in GUI_Apps/ContactBook/V1/. Install any third-party packages in an isolated Python environment.

Applications

Contact Book

A desktop contact management application with a graphical interface backed by a Microsoft SQL Server database. It is a practical example of a Python GUI that saves, searches, updates, and deletes records in SQL Server through parameterized stored-procedure calls.

Location: ContactBook/V1/Contact.py

Features

  • Add new contacts with first name, last name, mobile number, and email address
  • Search contacts by mobile number or email address
  • Update existing contact details
  • Delete contacts from the database
  • Input validation to prevent empty submissions
  • Duplicate detection enforced at the database level (unique mobile number and email)
  • Centered, fixed-size window with a clean sandstone theme

Screenshots / UI

The application presents a simple form with four input fields and four action buttons:

Field Description
First Name Contact’s first name
Last Name Contact’s last name
Mobile # Contact’s mobile phone number (must be unique)
Email Contact’s email address (must be unique)
Button Action
Save Insert a new contact or update an existing one
Search Find a contact by mobile number or email
Delete Remove the currently loaded contact
Reset Clear all fields and return to the default state

Requirements

pyodbc
ttkbootstrap

Install dependencies:

python -m pip install pyodbc ttkbootstrap

Database Setup

The application uses Microsoft SQL Server via the ODBC Driver 17. Run the provided SQL script to create the required table, constraints, indexes, and stored procedures:

Open SQL_Script/ContactBook.sql in your SQL Server client, select your intended development database, review the script, and execute it there. A SQL file path is not a shell command.

The script creates:

  • Contact table with id, fname, lname, mnumber, maddress columns
  • Primary key and unique constraints on mnumber and maddress
  • SaveContact stored procedure (insert or update)
  • DeleteContact stored procedure
  • SearchContact stored procedure

Configuration

The SQL Server connection string is defined as a class variable in Contact.py. Update it to match your environment:

__sql_connection = 'DRIVER={ODBC Driver 17 for SQL Server};SERVER=<your-server>;DATABASE=<your-db>;UID=<username>;PWD=<password>'

Security note: do not commit real server names, user names, or passwords. Prefer Windows authentication (Trusted_Connection=yes) or read the connection string from an environment variable in your own copy.

Usage

From the GUI_Apps folder:

python ContactBook/V1/Contact.py

FAQ

Which ODBC driver do I need? The connection string uses ODBC Driver 17 for SQL Server. If you have a newer driver installed, such as ODBC Driver 18, change the DRIVER value to match.

Why does saving fail for a contact that looks new? The database enforces unique mobile numbers and email addresses, so a save is rejected if either value already belongs to another contact.


Architecture

GUI_Apps/
├── ContactBook/
│   └── V1/
│       └── Contact.py       # Main application
└── SQL_Script/
    └── ContactBook.sql      # Database schema and stored procedures

Search combines supplied mobile and email values with AND: when both are entered, both must match the same record. Leaving both blank matches all contacts, but the GUI uses fetchone() and displays only one result; there is no ordered result list.


Read all Python Projects and Code Examples guides online · Project overview · All developer projects