主键值显示无,而不是python sqlite3中的整数值

时间:2018-12-18 09:17:38

标签: python sqlite

这是我的代码

import sqlite3

def connect():
    conn=sqlite3.connect("books.db")
    c=conn.cursor()
    c.execute("CREATE TABLE IF NOT EXISTS book (ID INTEGER PRIMARY KEY AUTOINCREMENT,title text ,author text,year integer ,isbn integer)")
    conn.commit()
    conn.close()

def insert(title,author,year,isbn):
    conn=sqlite3.connect("books.db")
    c=conn.cursor()
    c.execute("INSERT INTO book(title,author,year,isbn) VALUES(?,?,?,?)",(title,author,year,isbn))
    conn.commit()
    conn.close()

def views():
    conn=sqlite3.connect("books.db")
    c=conn.cursor()
    c.execute("SELECT * FROM book")
    rows=c.fetchall()
    conn.close()
    return rows

connect()
insert("MAHA","Madan",34,90909)
print (views())

我的输出是

[(None, 'MAHA', 'Madan', 34, 90909), (None, 'MAHA', 'Madan', 34, 90909), 
(None, 'MAHA', 'Madan', 34, 90909), (None, 'MAHA', 'Madan', 34,
 90909), (None, 'MAHA', 'Madan', 34, 90909), (None, 'MAHA', 'Madan', 34, 90909), (None, 'MAHA', 'Madan', 34, 90909), (None, 'MAHA', 'Mad
an', 34, 90909), (None, 'MAHA', 'Madan', 34, 90909), (None, 'MAHA', 'Madan', 34, 90909), (None, 'MAHA', 'Madan', 34, 90909), (None, 'MAH
A', 'Madan', 34, 90909), (None, 'MAHA', 'Madan', 34, 90909)]

1 个答案:

答案 0 :(得分:0)

c.execute("CREATE TABLE IF NOT EXISTS book (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, title text, author text, year integer, isbn integer)")

您必须添加NOT NULL属性,以便它将以递增顺序返回整数。