The steps for this recipe are as follows:
- Import the pyodbc library:
import pyodbc
- Connect to the database:
conn = pyodbc.connect('Driver={SQL Server};'
'Server=localhost;'
'Database=MLTracking;'
'uid=sa;'
'pwd=Password!;')
- Create a database connection cursor so that you can run queries:
cursor = conn.cursor()
- Insert the product and manufacturing date into the Device table and commit the transaction:
cursor.execute('''
INSERT INTO MLTracking.dbo.Product (Product,BeginLife)
VALUES
('Smoke Detector 9000',GETDATE()),
''')
conn.commit()
- Once you've calculated the remaining useful life of that product, add that information to the database:
cursor.execute('''
INSERT INTO MLTracking.dbo.RUL (ProductId,TotalRULDays,DateCalculated )
VALUES
(1,478,GETDATE()),
''')
conn.commit()