0

Here is my code:

import sqlite3

def insert(fields=(), values=()):
    connection = sqlite3.connect('database.db')
    # g.db is the database connection
    cur = connection.cursor()
    query = 'INSERT INTO  this_database (%s) VALUES (%s)' % (
        ', '.join(fields),
        ', '.join(['?'] * len(values))
    )
    cur.execute(query, values)
    connection.commit()
    id = cur.lastrowid
    cur.close()
    print (id)

test example:

insert(fields = ("id", "file_name", "url", "time", "type", "description"), values = (2, "file1", "wwww.test.com", "1", "photo", "my first database test"))

I don't want to give the id manually. I want it to add it+1 automatically.

How can I do that?

Son Truong
  • 13,661
  • 5
  • 32
  • 58
Allen
  • 5
  • 5

1 Answers1

1

You have an INTEGER PRIMARY KEY column, which, if you leave it out when inserting items, automatically increments:

INSERT INTO this_database(file_name, url, time, type, description)
            VALUES (?,?,?,?,?)

Since id is omitted, every time you insert a value using the above statement, it's automatically assigned a number by sqlite.

The documentation explaining this.

Shawn
  • 47,241
  • 3
  • 26
  • 60