Created
June 5, 2019 20:38
-
-
Save dheles/cfa493c77ac5a98ee64a16246db25a59 to your computer and use it in GitHub Desktop.
example python script inserting into a sqlite db
This file contains 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 characters
#!/usr/bin/env python | |
import sqlite3 | |
def main(): | |
conn = sqlite3.connect('example.db') | |
c = conn.cursor() | |
# Create table | |
c.execute('''CREATE TABLE example | |
(id integer, key text, value text)''') | |
# Larger example that inserts many records at a time | |
data = [(1, 'first', 'this'), | |
(2, 'then', 'that'), | |
(3, 'finally', 'the other') | |
] | |
c.executemany('INSERT INTO example VALUES (?,?,?)', data) | |
for row in c.execute('SELECT * FROM example ORDER BY id'): | |
print(row) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment