Skip to main content
 首页 » 编程设计

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

2025年05月04日11lovecherry

我正在使用 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") 

请您参考如下方法:

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

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

获得表名后,您可以使用字符串格式来形成 CREATE INDEX 命令。