mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-24 09:54:31 +02:00
Added create_index(..., if_not_exists=True), refs #14
This commit is contained in:
parent
0c6745b70f
commit
7784b924a1
3 changed files with 18 additions and 3 deletions
|
|
@ -236,19 +236,20 @@ class Table:
|
|||
self.exists = True
|
||||
return self
|
||||
|
||||
def create_index(self, columns, index_name=None, unique=False):
|
||||
def create_index(self, columns, index_name=None, unique=False, if_not_exists=False):
|
||||
if index_name is None:
|
||||
index_name = "idx_{}_{}".format(
|
||||
self.name.replace(" ", "_"), "_".join(columns)
|
||||
)
|
||||
sql = """
|
||||
CREATE {unique}INDEX {index_name}
|
||||
CREATE {unique}INDEX {if_not_exists}{index_name}
|
||||
ON {table_name} ({columns});
|
||||
""".format(
|
||||
index_name=index_name,
|
||||
table_name=self.name,
|
||||
columns=", ".join(columns),
|
||||
unique="UNIQUE " if unique else "",
|
||||
if_not_exists="IF NOT EXISTS " if if_not_exists else "",
|
||||
)
|
||||
self.db.conn.execute(sql)
|
||||
return self
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue