Skip to content

Instantly share code, notes, and snippets.

@MrEdinLaw
Last active September 19, 2019 19:22
Show Gist options
  • Save MrEdinLaw/b556b9268696e04b37d0ea370d867b25 to your computer and use it in GitHub Desktop.
Save MrEdinLaw/b556b9268696e04b37d0ea370d867b25 to your computer and use it in GitHub Desktop.
Randomization of a table with a backup table on the side, using python and sqlite3
import sqlite3
sql_config = sqlite3.connect('Tunes.db')
sql_con = sql_config.cursor()
sql_con.execute("CREATE TABLE IF NOT EXISTS 'temp' (Title TEXT, Length TEXT)") # Create temp table
sql_con.execute("DELETE FROM 'temp'") # Clean temp
# sql_con.execute("vacuum") # Commit clea
sql_con.execute("INSERT INTO 'temp' SELECT * FROM Tunes") # Copy Data to temp
sql_con.execute("DELETE FROM 'Tunes'") # Clean Tunes
sql_con.execute("INSERT INTO 'Tunes' SELECT * FROM temp ORDER BY RANDOM()") # Copy Randomized data to Tunes
sql_con.execute("DROP TABLE IF EXISTS 'temp'")
sql_config.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment