Working with sqlite in Python
#!/bin/python
import datetime
import timedelta
import os.path
import sqlite3
import sys
#Global Variables
DBPath='test.db'
#from datetime import datetime, timedelta
#then = datetime.now() - timedelta(hours = 2)
#now = datetime.now()
#(now - then) > timedelta(days = 1)
#False
#>>> (now - then) > timedelta(hours = 1)
#True
StuffToLoad = [
(0 , 'example.domain.com' , 'Maintenance' ,'2014-08-10 15:07:15.067766'),
(1 , 'example2.doman.com' , 'Maintenance' ,'2014-08-10 15:07:15.067766'),
(2 , 'exmaple3.domain.com' , 'Maintenance' ,'2014-08-10 15:07:15.067766'),
]
def CreateDB():
print "Creating Database....."
conn = sqlite3.connect(DBPath)
conn.execute('''CREATE TABLE MInMant (ID INT PRIMARY KEY NOT NULL, Machine TEXT NOT NULL, HostState INT NOT NULL, date CHAR(50))''')
conn.commit()
conn.close()
def InsertDB(x):
conn = sqlite3.connect(DBPath)
c = conn.cursor()
t = ('Maintenance',)
c.execute('SELECT * FROM MInMant WHERE HostState=?', t)
c.executemany('INSERT INTO MInMant VALUES (?,?,?,?)', StuffToLoad)
conn.commit()
conn.close()
def PrintDBContents():
print "DEBUG PrintDB Contents"
conn = sqlite3.connect(DBPath)
c = conn.cursor()
for row in c.execute('SELECT * FROM MInMant'):
print(row)
#We start out program
#if os.path.isfile(DBPath) == 'True':
CreateDB()
#Load data into our DB
InsertDB(StuffToLoad)
#Print data from our DB
PrintDBContents()