Trong phần hướng dẫn này, chúng ta sẽ tạo bảng mới Nhân viên. Chúng ta phải đề cập đến tên cơ sở dữ liệu trong khi thiết lập đối tượng kết nối
Chúng ta có thể tạo bảng mới bằng cách sử dụng câu lệnh CREATE TABLE của SQL. Trong cơ sở dữ liệu PythonDB của chúng tôi, bảng Nhân viên sẽ có bốn cột, tôi. e. , tên, id, lương và ban_id ban đầu
Truy vấn sau đây được sử dụng để tạo bảng mới Nhân viên
Thí dụ
Bây giờ, chúng tôi có thể kiểm tra xem bảng Nhân viên có trong cơ sở dữ liệu không
Bảng thay thế
Đôi khi, chúng tôi có thể quên tạo một số cột hoặc chúng tôi có thể cần cập nhật lược đồ bảng. Câu lệnh thay đổi được sử dụng để thay đổi lược đồ bảng nếu được yêu cầu. Ở đây, chúng ta sẽ thêm cột branch_name vào bảng Employee. Truy vấn SQL sau đây được sử dụng cho mục đích này
Tóm lược. trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách tạo bảng trong cơ sở dữ liệu SQLite từ chương trình Python bằng mô-đun sqlite3
Để tạo một bảng mới trong cơ sở dữ liệu SQLite từ chương trình Python, bạn làm theo các bước sau
- Đầu tiên, tạo một đối tượng
3 bằng hàm
Code language: Python [python]def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
4 của mô-đun sqlite3
Code language: Python [python]def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
- Thứ hai, tạo một đối tượng
0 bằng cách gọi phương thức
Code language: Python [python]def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
1 của đối tượng
Code language: Python [python]def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
3
Code language: Python [python]def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
- Thứ ba, truyền câu lệnh
3 cho phương thức
Code language: Python [python]def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
4 của đối tượngdef create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
0 và thực thi phương thức này
Code language: Python [python]def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
Để trình diễn, chúng tôi sẽ tạo hai bảng.
6 vàCode language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
7 như thể hiện trong sơ đồ cơ sở dữ liệu sauCode language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
Các câu lệnh
8 sau đây tạo hai bảng này________số 8Code language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
Hãy xem cách tạo bảng mới trong Python
Trước tiên, hãy phát triển một hàm có tên là
9 để trả về một đối tượngCode language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
3 đại diện cho một cơ sở dữ liệu SQLite được chỉ định bởi tham số tệp cơ sở dữ liệu db_fileCode language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
Code language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
Thứ hai, phát triển một hàm có tên là
11 chấp nhận một đối tượngCode language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
3 và một câu lệnh SQL. Bên trong hàm, chúng ta gọi phương thứcCode language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
4 của đối tượng
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
Code language: Python [python]
0 để thực thi câu lệnhCode language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
8Code language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
1Code language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
Thứ ba, tạo một hàm
16 để tạo các bảngCode language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
6 vàCode language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
7Code language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
9Code language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
Thứ tư, thực hiện chức năng
16Code language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
1Code language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
Đây là chương trình đầy đủ
2
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
Code language: Python [python]
Hãy xác minh xem chương trình đã tạo thành công các bảng đó trong cơ sở dữ liệu
90 chưaCode language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
Trước tiên, khởi chạy dòng lệnh và kết nối với cơ sở dữ liệu
90Code language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
5Code language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
Sau đó, sử dụng lệnh
92 để hiển thị các bảng trong cơ sở dữ liệu.Code language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
7Code language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
Như bạn có thể thấy rõ ràng từ đầu ra, chúng tôi đang có các bảng
6 vàCode language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
7 trong cơ sở dữ liệuCode language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
90. Và chương trình hoạt động như mong đợi.Code language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
Trong hướng dẫn này, bạn đã học cách tạo các bảng mới trong cơ sở dữ liệu SQLite bằng cách sử dụng phương thức
4 của đối tượngCode language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn
0Code language: Python [python]
def create_connection[db_file]: """ create a database connection to the SQLite database specified by db_file :param db_file: database file :return: Connection object or None """ conn = None try: conn = sqlite3.connect[db_file] return conn except Error as e: print[e] return conn