Hướng dẫn python sqlite3 not inserting - python sqlite3 không chèn

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.

Để 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.

def create_project[conn, project]: """ Create a new project into the projects table :param conn: :param project: :return: project id """ sql = ''' INSERT INTO projects[name,begin_date,end_date] VALUES[?,?,?] ''' cur = conn.cursor[] cur.execute[sql, project] conn.commit[] return cur.lastrowid

Code language: Python [python]

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.

def create_task[conn, task]: """ Create a new task :param conn: :param task: :return: """ sql = ''' INSERT INTO tasks[name,priority,status_id,project_id,begin_date,end_date] VALUES[?,?,?,?,?,?] ''' cur = conn.cursor[] cur.execute[sql, task] conn.commit[] return cur.lastrowid

Code language: Python [python]

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.

def main[]: database = r"C:\sqlite\db\pythonsqlite.db" # create a database connection conn = create_connection[database] with conn: # create a new project project = ['Cool App with SQLite & Python', '2015-01-01', '2015-01-30']; project_id = create_project[conn, project] # tasks task_1 = ['Analyze the requirements of the app', 1, 1, project_id, '2015-01-01', '2015-01-02'] task_2 = ['Confirm with user about the top requirements', 1, 1, project_id, '2015-01-03', '2015-01-05'] # create tasks create_task[conn, task_1] create_task[conn, task_2]

Code language: Python [python]

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:

if __name__ == '__main__': main[]

Code language: Python [python]

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

import sqlite3 from sqlite3 import Error 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 def create_project[conn, project]: """ Create a new project into the projects table :param conn: :param project: :return: project id """ sql = ''' INSERT INTO projects[name,begin_date,end_date] VALUES[?,?,?] ''' cur = conn.cursor[] cur.execute[sql, project] conn.commit[] return cur.lastrowid def create_task[conn, task]: """ Create a new task :param conn: :param task: :return: """ sql = ''' INSERT INTO tasks[name,priority,status_id,project_id,begin_date,end_date] VALUES[?,?,?,?,?,?] ''' cur = conn.cursor[] cur.execute[sql, task] conn.commit[] return cur.lastrowid def main[]: database = r"C:\sqlite\db\pythonsqlite.db" # create a database connection conn = create_connection[database] with conn: # create a new project project = ['Cool App with SQLite & Python', '2015-01-01', '2015-01-30']; project_id = create_project[conn, project] # tasks task_1 = ['Analyze the requirements of the app', 1, 1, project_id, '2015-01-01', '2015-01-02'] task_2 = ['Confirm with user about the top requirements', 1, 1, project_id, '2015-01-03', '2015-01-05'] # create tasks create_task[conn, task_1] create_task[conn, task_2] if __name__ == '__main__': main[]

Code language: Python [python]

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:

sqlite> .header on sqlite> .mode column

Code language: Shell Session [shell]

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ủ Đề