Skip to content

Instantly share code, notes, and snippets.

@eslerm
Created October 27, 2018 02:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eslerm/bb71bad31c0a2ecda222bf48d1033be4 to your computer and use it in GitHub Desktop.
Save eslerm/bb71bad31c0a2ecda222bf48d1033be4 to your computer and use it in GitHub Desktop.
python3 sqlite3 cheat
# python3 sqlite3 cheat
# [0] https://docs.python.org/3/library/sqlite3.html
import sqlite3
# create a Connection
con = sqlite3.connect('users.db')
# create a Cursor
c = con.cursor()
# create a table
c.execute('''CREATE TABLE users
(username text, password text)''')
# create rows
c.execute("INSERT INTO users VALUES ('root', 's3cr3t')")
c.execute("INSERT INTO users VALUES ('guest', 'secret')")
# do not use unsanitized values
user_password = ('user',
"secret_'); UPDATE users SET password = 'foobar' WHERE username = 'root'; --",)
c.executescript("INSERT INTO users VALUES ('%s', '%s')" % user_password)
# pass variable with ? to sanitize
c.execute("DELETE FROM users WHERE username = 'user'") # clean up
try:
c.executescript('INSERT INTO users VALUES (?, ?)' % user_password)
except TypeError as e:
print('TypeError: ', e)
# execute list
users_passwords = [('user', 'secret_'),
('www', None), # password = Null
]
c.executemany('INSERT INTO users VALUES (?, ?)', users_passwords)
# print rows
for row in c.execute('SELECT * from users'):
print(row)
# save state
con.commit()
# close connection
con.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment