Hướng dẫn python dataclass sqlite - python dataclass sqlite

Tôi đang tạo một chương trình đăng nhập và đăng ký chính xác và hiện tại tôi đang gặp một chút rắc rối trong việc chèn một hàng theo giá trị đã cho là tên người dùng và đầu vào mật khẩu.

Tôi có hai tệp python một là chính.

Pycharm không gây ra lỗi nên tôi muốn hỏi mã của mình bị sao? Tại sao nó không chèn một hàng?

Main.py

    while 1:
        s = SignUp 
        username = input["Username: "]
        password = input["Password: "]

        s[username, password]

        print["Done!"]
        break

functions.py

c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

Và đây là lớp sẽ chấp nhận giá trị tên người dùng và mật khẩu đã cho từ Main.py

Đây là nơi bảng được tạo

c = sqlite3.connect['registration.db']

c.execute['''CREATE TABLE IF NOT EXISTS info [username     TEXT    NOT NULL, password      TEXT    NOT NULL];''']

Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu cách chèn các hàng vào bảng trong cơ sở dữ liệu SQLite từ chương trình Python bằng mô -đun SQLite3.: in this tutorial, you will learn how to insert rows into a table in the SQLite database from a Python program using the sqlite3 module.: in this tutorial, you will learn how to insert rows into a table in the SQLite database from a Python program using the sqlite3 module.

Để chèn các hàng vào bảng trong cơ sở dữ liệu SQLite, bạn sử dụng các bước sau:

  1. Đầu tiên, kết nối với cơ sở dữ liệu SQLite bằng cách tạo đối tượng kết nối.
  2. Thứ hai, tạo đối tượng
    c = sqlite3.connect['main.db']
    class SignUp:
        def __init__[self, username, password]:
            self.username = username
            self.password = password
    
        def signu[self]:
            c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
            c.commit[]
    
    
    2 & nbsp; bằng cách gọi phương thức con trỏ của đối tượng
    c = sqlite3.connect['main.db']
    class SignUp:
        def __init__[self, username, password]:
            self.username = username
            self.password = password
    
        def signu[self]:
            c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
            c.commit[]
    
    
    3.
  3. Thứ ba, thực hiện một câu lệnh chèn. Nếu bạn muốn chuyển các đối số cho câu lệnh
    c = sqlite3.connect['main.db']
    class SignUp:
        def __init__[self, username, password]:
            self.username = username
            self.password = password
    
        def signu[self]:
            c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
            c.commit[]
    
    
    4, bạn sẽ sử dụng dấu câu hỏi [?] Làm trình giữ chỗ cho mỗi đối số.

SQLite Python - Chèn ví dụ hàng

Hãy cùng chèn một dự án mới vào bảng

c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

5 và một số tác vụ vào bảng
c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

6 mà chúng tôi đã tạo trong các bảng tạo từ một hướng dẫn chương trình Python.

Đầu tiên, hãy tạo một chức năng mới để thiết lập kết nối cơ sở dữ liệu với cơ sở dữ liệu SQLitte được chỉ định bởi tệp cơ sở dữ liệu.

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] except Error as e: print[e] return conn

Code language: Python [python]

Tiếp theo, phát triển một chức năng để chèn một dự án mới vào bảng

c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

5.
c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

0

Trong chức năng này, chúng tôi đã sử dụng thuộc tính & nbsp; ________ 18 của đối tượng

c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

2 để lấy lại ID được tạo.

Sau đó, phát triển một chức năng khác để chèn các hàng vào bảng

c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

6.
c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

3

Sau đó, phát triển chức năng

c = sqlite3.connect['registration.db']

c.execute['''CREATE TABLE IF NOT EXISTS info [username     TEXT    NOT NULL, password      TEXT    NOT NULL];''']

1 tạo ra một dự án mới và hai nhiệm vụ liên quan đến dự án.
c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

5

Và gọi hàm

c = sqlite3.connect['registration.db']

c.execute['''CREATE TABLE IF NOT EXISTS info [username     TEXT    NOT NULL, password      TEXT    NOT NULL];''']

1:
c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

7

Đây là chương trình đầy đủ:

c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

8

Cuối cùng, kết nối với cơ sở dữ liệu thông qua SQLITE3 shell và dữ liệu truy vấn từ các bảng

c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

5 và
c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

6 để kiểm tra xem dữ liệu có được chèn thành công không.

Sử dụng các lệnh này để định dạng đầu ra:

c = sqlite3.connect['registration.db']

c.execute['''CREATE TABLE IF NOT EXISTS info [username     TEXT    NOT NULL, password      TEXT    NOT NULL];''']

1

Và sử dụng câu lệnh

c = sqlite3.connect['registration.db']

c.execute['''CREATE TABLE IF NOT EXISTS info [username     TEXT    NOT NULL, password      TEXT    NOT NULL];''']

5 này để truy vấn dữ liệu từ bảng
c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

5:
c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

0 và sử dụng câu lệnh
c = sqlite3.connect['registration.db']

c.execute['''CREATE TABLE IF NOT EXISTS info [username     TEXT    NOT NULL, password      TEXT    NOT NULL];''']

5 sau để truy vấn dữ liệu từ bảng
c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

6:

And use the following

c = sqlite3.connect['registration.db']

c.execute['''CREATE TABLE IF NOT EXISTS info [username     TEXT    NOT NULL, password      TEXT    NOT NULL];''']

5 statement to query data from the
c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

6 table:
c = sqlite3.connect['main.db']
class SignUp:
    def __init__[self, username, password]:
        self.username = username
        self.password = password

    def signu[self]:
        c.execute[f'''INSERT INTO info [username, password] VALUES ['{self.username}', '{self.password}']; ''']
        c.commit[]

1 Trong hướng dẫn này, bạn đã học được cách chèn các hàng vào các bảng trong cơ sở dữ liệu SQLite từ chương trình Python.

In this tutorial, you have learned how to insert rows into tables in the SQLite database from a Python program.

Hướng dẫn này có hữu ích không?

Bài Viết Liên Quan

Chủ Đề