Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sqlite 递归查找?_Sqlite - Fatal编程技术网

Sqlite 递归查找?

Sqlite 递归查找?,sqlite,Sqlite,这是我想要的一个例子。我有一个id,我想选择*该id及其所有父项。可能吗 cn.Execute("create table if not exists Link( id INTEGER PRIMARY KEY AUTOINCREMENT , `parent` INT , `val` INT NOT NULL , FOREIGN KEY (parent) REFERENCES Link(id));", new { }); cn.Execute("insert into Link(val) v

这是我想要的一个例子。我有一个id,我想
选择*
该id及其所有父项。可能吗

cn.Execute("create table if not exists Link( id INTEGER PRIMARY KEY AUTOINCREMENT , `parent`  INT , `val`  INT  NOT NULL , FOREIGN KEY (parent) REFERENCES Link(id));", new { });
cn.Execute("insert into Link(val) values(3)");
cn.Execute("insert into Link(parent, val) values(last_insert_rowid(), 5)");
cn.Execute("insert into Link(parent, val) values(last_insert_rowid(), 9)");
var id = cn.Query<long>("select last_insert_rowid()", new{});
cn.Execute("insert into Link(parent, val) values(last_insert_rowid(), 8)");
cn.Execute("insert into Link(val) values(4)");
cn.Execute(“如果不存在创建表链接(id整型主键自动递增,`parent`INT,`val`INT非空,外键(父项)引用链接(id));”,new{});
执行(“插入链接(val)值(3)”;
cn.Execute(“插入链接(父项,val)值(最后插入的rowid(),5)”;
cn.Execute(“插入链接(父项,val)值(最后一次插入rowid(),9)”;
var id=cn.Query(“选择last\u insert\u rowid()”,new{});
cn.Execute(“插入链接(父项,val)值(最后一次插入rowid(),8)”;
执行(“插入链接(val)值(4)”;

递归sql函数可能会有所帮助。我遇到了这个非常特殊的需求,非常感谢,因为我使用了SQLAlchemy(ORM),我通过以下方式实现了这一点:

class Link(Base):
    def parents(self):
        # print self.parent
        # print self.parent_id
        if not self.parent:
            return None
        else:
            return self.parent, self.parent.parents
返回中嵌套的
元组
s必须展平

PS:AFAIK only PostgreSQL提供了递归查询的功能,我猜这里需要这个功能。

@bsa使用按钮“flag”链接将其标记为重复