# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Insert no SQLite usando Variaveis

import sqlite3
import random
import time
import datetime

# +
# criando conexão
conn = sqlite3.connect('dsa.db')

# cursor
c = conn.cursor()

# criar tabela
def create_table():
    c.execute('CREATE TABLE IF NOT EXISTS produtos(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, date TEXT, '\
              'prod_name TEXT, valor REAL)')
    
# inserir linha
def data_insert():
    c.execute("INSERT INTO produtos VALUES('2018-05-02 12:34:45', 'Teclado', 130.00 )")
    conn.commit()
    c.close()
    conn.close()
    
# usando variáveis para inserir dados
def data_insert_var():
    new_date = datetime.datetime.now()
    new_prod_name = 'Monitor'
    new_valor = random.randrange(50,100)
    c.execute("INSERT INTO produtos (date, prod_name, valor) VALUES (?, ?, ?)", (new_date, new_prod_name, new_valor))
    conn.commit()


# -

# gerando valores e inserindo na tabela
for i in range(10):
    data_insert_var()
    time.sleep(1)

c.close()
conn.close()



StarcoderdataJupyterScriptsDedupFiltered