1
Python Programming Blogger's Practical Notes on Database Operations
thon database operation

2024-11-08 03:06:02

Database Basics Hands-on

Hi everyone! Today we're going to talk about the basics of database operations in Python. As Python programming enthusiasts, we often need to work with databases, whether it's storing data, querying data, or updating and deleting data. So mastering the basic skills of database operations is essential. Follow along with me step by step, and I believe you'll be able to get started in no time!

Creating a Simple Database

Before we start using real databases, let's simulate one. You can use Python's built-in data structure - dictionary to represent user information. For example:

user = {
    'name': 'John Doe',
    'age': 28,
    'address': 'Haidian District, Beijing',
    'credit_cards': [
        {'type': 'visa', 'number': '1234567890'},
        {'type': 'master', 'number': '9876543210'}
    ]
}

Here we use a dictionary to store basic user information such as name, age, and address. The user's credit card information is stored in a list, where each credit card is also a dictionary containing the card type and number. This implements a one-to-many relationship - one user can have multiple credit cards.

You can try adding more users to this "database" by using nested dictionaries:

database = {
    '001': user,
    '002': {...},
    '003': {...}
}

Executing External Commands

Sometimes we need to execute external commands in Python, such as running SQL scripts or backing up databases. We can use Python's os module and subprocess module for this.

Use os.system() to directly execute commands:

import os
os.system('mysql -u root -p password < backup.sql')

While subprocess.Popen() allows better control over command input and output:

import subprocess

process = subprocess.Popen(['mysql', '-u', 'root', '-p', 'password'],
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE)
output = process.communicate(input='SELECT * FROM users;'.encode())[0]
print(output.decode())

Here we passed in the parameters for the mysql command and piped in an SQL query statement. See how convenient it is to operate databases using Python?

Data Operation Techniques

Besides directly interacting with databases, we also need some techniques when handling data, such as manipulating dictionaries and processing files.

Dictionary Operations

Dictionaries are widely used in Python, and we often need to perform operations on them, such as merging and sorting.

To merge two dictionaries, you can use the dict.update() method:

dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
dict1.update(dict2)
print(dict1)  # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}

You can also use the unpacking operator ** to merge dictionaries:

dict3 = {**dict1, **dict2}

To sort a dictionary by value, you can use the sorted() function along with the items() method:

dict4 = {'c': 3, 'a': 1, 'b': 2}
sorted_dict = sorted(dict4.items(), key=lambda x: x[1])
print(sorted_dict)  # Output: [('a', 1), ('b', 2), ('c', 3)]

Here we used a lambda function as the key parameter to specify sorting based on the size of the dictionary values.

File System Operations

When working with databases, we may need to check if a directory exists or create a new directory. Python's os module and pathlib module can help us accomplish these tasks.

Use os.path.exists() to check if a directory exists:

import os

dir_path = '/path/to/dir'
if not os.path.exists(dir_path):
    os.makedirs(dir_path)

If the directory doesn't exist, we use os.makedirs() to create the directory and its parent directories.

The pathlib module provides a more Pythonic way to operate on the file system:

from pathlib import Path

dir_path = Path('/path/to/dir')
if not dir_path.exists():
    dir_path.mkdir(parents=True)

Here we created a Path object, then used its exists() method to check if the directory exists, and if not, used the mkdir() method to create the directory and its parent directories.

You see, learning Python database operations isn't difficult. Once you've mastered some basic concepts and techniques, you can apply them well in your projects. However, hands-on practice is the best way to learn. You can find some open-source projects and see how they operate databases. The journey of programming is long and challenging, but if you maintain your enthusiasm for learning, you'll definitely go further! Keep it up!

Recommended