Tuesday, September 25, 2018

The Return of The Refcards-System

- As you know, I've been working on a project in Python called The Refcards-System, or "Refcard-Project" if you will;
- I have been incrementally improving the system;
- Now I have a working version that incorporates an SQLite database in Python;
- The next step is to have a kind of small text editor open up to view records in the database;


import sqlite3
import os
import time
# Choose the filepath you want the database to reside in.
os.chdir('C:\\FILEPATH')
# Creates the database on the first run, otherwise just connects to it.
conn = sqlite3.connect("refcards2019.db")
cursor = conn.cursor()
# To be used on the first "run" to create the first TABLE in the database.
# cursor.execute("""CREATE TABLE refcards(id INTEGER PRIMARY KEY AUTOINCREMENT,
# date TEXT, note TEXT)""")
# conn.commit()
# A timestamp in a useful format.
t = time.strftime('%Y%m%d%H%M')
# Prompt to write the actual note.
c = raw_input('Write note: ')
# Inserts timestamp and note into the refcards TABLE as a record.
cursor.execute("""INSERT INTO refcards(date, note)
VALUES(?,?)""", (t,c))
# Operations on database tables must be "committed".
conn.commit()
# I use this to print the contents of the TABLE,
# just to see that it worked properly.
# This section can be skipped.
sql = "SELECT * FROM refcards"
cursor.execute(sql)
print cursor.fetchall()
# Close connection to database.
conn.close()
view raw refcards2019.py hosted with ❤ by GitHub

No comments:

Post a Comment