Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sqlite3
- from sqlite3 import Error
- def create_connection(db_file):
- conn = None
- try:
- # Create file on the disk if database doesn't exist
- conn = sqlite3.connect(db_file)
- return conn
- except Error as e:
- print(e)
- return conn
- def create_table(conn, create_table_sql):
- try:
- c = conn.cursor()
- c.execute(create_table_sql)
- except Error as e:
- print(e)
- def insert_temperature(conn, temp):
- sql = ''' INSERT INTO temperatures(id,temp)
- VALUES(?,?) '''
- cur = conn.cursor()
- cur.execute(sql, temp)
- conn.commit()
- return cur.lastrowid
- def main():
- database = r"./database.db"
- table_for_temperatures_sql = """ CREATE TABLE IF NOT EXISTS temperatures (
- id integer PRIMARY KEY AUTOINCREMENT,
- temp text NOT NULL
- ); """
- # create a database connection
- conn = create_connection(database)
- # create tables
- if conn is not None:
- # create projects table
- create_table(conn, table_for_temperatures_sql)
- else:
- print("Error! cannot create the database connection.")
- aa = (null, 2)
- insert_temperature(conn, aa)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement