在 table 创建后插入数据时 python3 sqlite table 的自动递增 id 的占位符是什么?

What will be the placeholder for python3 sqlite table's auto incremented id while inserting data after table creation?

我创建了一个 python3 sqlite table 如果它不存在于 createTable() 方法中。之后说我想使用以下方法插入数据 (insertVaribleIntoTable()).

现在对于此方法,我不知道新行的 ID,之后我将插入数据,因为它是自动递增的。

所以我没有将 id 传递给 insertVaribleIntoTable() 方法。我应该为 id 变量做什么?谁能告诉我应该将什么作为 id 占位符传递给 insertVaribleIntoTable() 方法,因为它会在我插入数据时自动递增?谁能告诉我该怎么做?可以看到少了一个“?”在 insertVaribleIntoTable() 方法中的“VALUES”关键字之后。谢谢。

    def createTable(id, name, company, address, postal, country, home, business, mobile, fax, notes):
            
        try:
            sqliteConnection = sqlite3.connect('SQLite_Python.db');
            cursor = sqliteConnection.cursor();
            print("Connected to SQLite");
    
            sqlite_insert_with_param = """CREATE TABLE IF NOT EXISTS 'SqliteDb_Addresser' (id INTEGER PRIMARY KEY AUTOINCREMENT, name, company, address, postal, country, home, business, mobile, fax, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);""";
    
            data_tuple = (id, name, company, address, postal, country, home, business, mobile, fax, notes);
            cursor.execute(sqlite_insert_with_param, data_tuple);
            sqliteConnection.commit();
            print("Python Variables inserted successfully into SqliteDb_developers table");
    
            cursor.close();
    
        except sqlite3.Error as error:
            print("Failed to insert Python variable into sqlite table", error);
        finally:
            if (sqliteConnection):
                sqliteConnection.close();
                print("The SQLite connection is closed");  
    
    def insertVaribleIntoTable(name, company, address, postal, country, home, business, mobile, fax, notes):
            
        try:
            sqliteConnection = sqlite3.connect('SQLite_Python.db');
            cursor = sqliteConnection.cursor();
            print("Connected to SQLite");
    
            sqlite_insert_with_param = """INSERT INTO 'SqliteDb_Addresser'
                            (name, company, address, postal, country, home, business, mobile, fax, notes) 
                            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);""";
    
            data_tuple = (name, company, address, postal, country, home, business, mobile, fax, notes);
            cursor.execute(sqlite_insert_with_param, data_tuple);
            sqliteConnection.commit();
            print("Python Variables inserted successfully into SqliteDb_developers table");
    
            cursor.close();
    
        except sqlite3.Error as error:
            print("Failed to insert Python variable into sqlite table", error);
        finally:
            if (sqliteConnection):
                sqliteConnection.close();
                print("The SQLite connection is closed");

如果该列是自动递增的,则不要插入该列。它将由数据库创建。

如果要查看最后插入的ID,可以使用last_insert_rowid()

CREATE TABLE Person (
     ID integer primary key AUTOINCREMENT, 
     Name varchar(20), 
     Age Integer 
)

INSERT INTO Person (Name, Age) VALUES ('Tim', 12);  -- Don't include ID

SELECT last_insert_rowid()    -- 1

同样的想法也适用于通过代码插入。