Created
November 17, 2017 09:14
-
-
Save RobinL/c27fef037cbd2b24c909d5275b01adfb to your computer and use it in GitHub Desktop.
Revisions
-
RobinL created this gist
Nov 17, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,46 @@ import re def get_create_table_string(tablename, connection): sql = """ select * from sqlite_master where name = "{}" and type = "table" """.format(tablename) result = connection.execute(sql) create_table_string = result.fetchmany()[0][4] return create_table_string def add_pk_to_create_table_string(create_table_string, colname): regex = "(\n.+{}[^,]+)(,)".format(colname) return re.sub(regex, "\\1 PRIMARY KEY,", create_table_string) def add_pk_to_sqlite_table(tablename, index_column, connection): cts = get_create_table_string(tablename, connection) cts = add_pk_to_create_table_string(cts, index_column) template = """ BEGIN TRANSACTION; ALTER TABLE {tablename} RENAME TO {tablename}_old_; {cts}; INSERT INTO {tablename} SELECT * FROM {tablename}_old_; DROP TABLE {tablename}_old_; COMMIT TRANSACTION; """ create_and_drop_sql = template.format(tablename = tablename, cts = cts) connection.executescript(create_and_drop_sql) # Example: # import pandas as pd # import sqlite3 # df = pd.DataFrame({"a": [1,2,3], "b": [2,3,4]}) # con = sqlite3.connect("deleteme.db") # df.to_sql("df", con, if_exists="replace") # add_pk_to_sqlite_table("df", "index", con) # r = con.execute("select sql from sqlite_master where name = 'df' and type = 'table'") # print(r.fetchone()[0])