如何在数据库中的表本身上创建索引?

2024-04-26 00:33:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用APSW将我的SQL代码包装成python,如下所示:

connection=apsw.Connection("dbfile"); cursor=connection.cursor()
cursor.execute("create table foo(x,y,z)")
cursor.execute("create table bar(a,b,c)")
cursor.execute("create table baz(one,two,three)")

我计划使用gui框架来显示这些表名(以及随后的列和行)。我还想按不同的标准对这些表名进行排序。你知道吗

有没有一种方法可以在表本身上创建一个用于排序的索引--例如:

cursor.execute("CREATE INDEX index_name ON foo")

提前谢谢。你知道吗


Tags: 代码executesqlfoo排序createtablebar
2条回答

You can list all the tables在sqlite数据库中

cursor.execute(
    "SELECT tbl_name FROM sqlite_master WHERE type='table'")

table_names = cursor.fetchall()

一旦有了表名,就可以使用字符串格式来形成CREATE INDEX命令。你知道吗

除了@unutbu answer,还有^{} function

PRAGMA table_info(table-name);

它返回单个表的信息。你知道吗

相关问题 更多 >