0

我创建了 4 个表并使用 id 将它们链接在一起。现在我如何确定使用什么作为 id?

表创建

import sqlite3 as lite

queries = []

queries.append([''.join('CREATE TABLE Movie',
            '(id INTEGER PRIMARY KEY,',
            'title INTEGER,',
            'genre INTEGER,',
            'cost INTEGER)')])

queries.append([''.join('CREATE TABLE Title',
            '(id INTEGER,',
            'English TEXT,',
            'Spansih TEXT,',
            'French TEXT)')])

queries.append([''.join('CREATE TABLE Genre',
            '(id INTEGER,',
            'Action INTEGER,',
            'Comedy INTEGER,',
            'Drama INTEGER',
            'Romance INTEGER)')])

queries.append([''.join('CREATE TABLE Cost',
            '(id INTEGER,',
            'Base INTEGER,',
            'Shipping INTEGER,',
            'Total INTEGER)')])

添加行

title_id = None # How do i determines these?
genre_id = None # Should I just use Integer Primary Key for each table id? 
cost_id = None # But then how do I get those id values to put into 



queries.append('''INSERT INTO Movies 
            (title, genre, cost) VALUES(%d, %d, %d)''' %
            (titile_id, genre_id, cost_id))

queries.append('''INSERT INTO Title
            (id, English) VALUES(%d, %s)''' %
            (title_id, 'Movie 1'))

queries.append('''INSERT INTO Genre
            (id, Action, Comedy) VALUES(%d, %d. %d)''' %
            (genre_id, 1, 1,))

queries.append('''INSERT INTO Cost
            (id, Base, Shipping, Total) VALUES(%d, %d, %d, %d)''' %
            (cost_id, 50, 8, 58))


for q in queries:
    print q
4

1 回答 1

1
genre_id = None # Should I just use Integer Primary Key for each table id?

每个表的id应该是INTEGER PRIMARY KEY.

title_id = None # How do i determines these?
cost_id = None # But then how do I get those id values to put into

在电影之前插入标题和成本。省略手动设置 id(即INSERT INTO Cost(Base, Shipping, Total) VALUES (..., ..., ...)改为)。运行插入后立即使用cursor.lastrowid获取最新的自动生成的 id。

于 2013-03-22T06:14:58.047 回答