Avi
Avi

Reputation: 1208

How to correctly set AUTO INCREMENT fo a column in SQLite, using Python?

I have been trying with the below code:

import sqlite3
data_person_name = [('Michael', 'Fox'),
                    ('Adam', 'Miller'),
                    ('Andrew', 'Peck'),
                    ('James', 'Shroyer'),
                    ('Eric', 'Burger')]
con = sqlite3.connect(":memory:")
c = con.cursor()
c.execute('''CREATE TABLE q1_person_name
             (name_id integer auto_increment primary key,
              first_name varchar(20) NOT NULL,
              last_name varchar(20) NOT NULL)''')
c.executemany('INSERT INTO q1_person_name VALUES (?,?,?)', data_person_name)
for row in c.execute('SELECT * FROM q1_person_name'):
    print row

Can somebody help me in making the name_id automatically incremented ?

Upvotes: 25

Views: 88240

Answers (4)

Jan Bodnar
Jan Bodnar

Reputation: 11637

In SQLite, INTEGER PRIMARY KEY column is auto-incremented. There is also an AUTOINCREMENT keyword. When used in INTEGER PRIMARY KEY AUTOINCREMENT, a slightly different algorithm for Id creation is used.

#!/usr/bin/python

import sqlite3
data_person_name = [('Michael', 'Fox'),
                    ('Adam', 'Miller'),
                    ('Andrew', 'Peck'),
                    ('James', 'Shroyer'),
                    ('Eric', 'Burger')]

con = sqlite3.connect(":memory:")

with con:

    c = con.cursor()

    c.execute('''CREATE TABLE q1_person_name
                 (name_id INTEGER PRIMARY KEY,
                  first_name varchar(20) NOT NULL,
                  last_name varchar(20) NOT NULL)''')
    c.executemany('INSERT INTO q1_person_name(first_name, last_name) VALUES (?,?)', data_person_name)

    for row in c.execute('SELECT * FROM q1_person_name'):
        print(row)

This code now works OK.

c.executemany('INSERT INTO q1_person_name(first_name, last_name) VALUES (?,?)', data_person_name)

When using auto-increment, we have to explicitly state the column names, omitting the one that is auto-incremented.

$ ./test.py 
(1, u'Michael', u'Fox')
(2, u'Adam', u'Miller')
(3, u'Andrew', u'Peck')
(4, u'James', u'Shroyer')
(5, u'Eric', u'Burger')

This is the output of the code example.

Upvotes: 51

Animesh Jaipurkar
Animesh Jaipurkar

Reputation: 49

Replace the first ? in executemany statement with null.

So the following line can be rewritten:

c.executemany('INSERT INTO q1_person_name VALUES (?,?,?)', data_person_name)

as

c.executemany('INSERT INTO q1_person_name VALUES (null,?,?)', data_person_name)

Upvotes: 3

Daniel Roseman
Daniel Roseman

Reputation: 599610

It seems that you've done it already. So there's no need to reference that field when you insert.

INSERT INTO q1_person_name (first_name, last_name) VALUES (?,?)

Upvotes: 1

Naveed
Naveed

Reputation: 19

Try like this:

c.execute('''CREATE TABLE q1_person_name
         (name_id integer primary key AUTOINCREMENT,
          first_name varchar(20) NOT NULL,
          last_name varchar(20) NOT NULL)''')

Upvotes: -1

Related Questions