Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- # print(dir(sqlite3))
- # conn = sqlite3.connect(r'C:\Users\User\Documents\Python_October\strongbad_emails.sqlite')
- # cur = conn.cursor()
- # data = cur.execute('SELECT title, message FROM email').fetchall()
- # conn.close()
- # print(data[:10])
- def insert_data(data, table, path=r'C:\Users\User\Documents\Python_October\test.sqlite'):
- with sqlite3.connect(path) as conn:
- cur = conn.cursor()
- for row in data:
- cur.execute(f'INSERT INTO {table} VALUES (?, ?, ?, ?, ?)', row)
- data = cur.execute(f'SELECT * FROM {table}').fetchall()
- print(data)
- def drop_table(table, path=r'C:\Users\User\Documents\Python_October\test.sqlite'):
- with sqlite3.connect(path) as conn:
- cur = conn.cursor()
- cur.execute(f'DROP TABLE {table}')
- print(f'Table {table} was succesfully destroyed')
- def create_table(table, path=r'C:\Users\User\Documents\Python_October\test.sqlite'):
- with sqlite3.connect(path) as conn:
- cur = conn.cursor()
- cur.execute(f'CREATE TABLE IF NOT EXISTS {table} (id integer primary key, name text unique, age integer, gender varchar(2), hobby text)')
- data = cur.execute(f'SELECT * FROM {table}').fetchall()
- print(data)
- drop_table(table='people')
- create_table(table='people')
- insert_data([(1, "Anton", 27, "M", "Audiobookmaker/Voice Acting"),
- (2, "Timofei", 24, "M", "Sleeping"),
- (3, "Lex", 26, "M", "Skyrunning"),
- (4, "Alena", 26, "F", "Avokado Growing")], 'people')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement