Python Sqlite3-如何实现一对多关系的存储
我目前正在开发一个应用程序,将网站信息及其URL存储在SQLite3数据库中 我有两个类,Python Sqlite3-如何实现一对多关系的存储,python,database,sqlite,one-to-many,Python,Database,Sqlite,One To Many,我目前正在开发一个应用程序,将网站信息及其URL存储在SQLite3数据库中 我有两个类,Website和Url,它们都存储我的应用程序所需的数据 网站类初始化如下: class Website: """ Class that represents a project's website. """ def __init__( self, domain, l
Website
和Url
,它们都存储我的应用程序所需的数据
网站
类初始化如下:
class Website:
"""
Class that represents a project's website.
"""
def __init__(
self,
domain,
language,
urls,
):
self.domain = domain
self.language = language
self.urls = []
而Url
类是这样的:
class Url:
"""
Class that represents an URL associated to a Website instance
"""
def __init__(
self,
url,
title,
):
self.url = url
self.title = title
我用于创建表的查询如下所示:
CREATE TABLE IF NOT EXISTS websites (
name text NOT NULL UNIQUE,
domain text NOT NULL,
language text NOT NULL);
及
如果url
参数是第二个表中应该列出的url,那么如何存储w=Website(“test.com”,“en”,url)
并在数据库中保留关系
我是否应该保存不带URL的网站
实例,并使用另一个命令单独保存它们?我真的不知道该怎么做。谢谢
CREATE TABLE IF NOT EXISTS urls (
website text NOT NULL,
url text NOT NULL UNIQUE,
title text NOT NULL,
FOREIGN KEY (website) REFERENCES websites(name));