Created
March 21, 2016 02:20
-
-
Save ilovejs/e472738cee768a4614e5 to your computer and use it in GitHub Desktop.
add word to database in command line.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import sqlite3 | |
conn = sqlite3.connect('saved_words.db') | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-a", "--add", help="add word") | |
parser.add_argument("-c", help="compiling table", action='store_true') | |
parser.add_argument("-l", help="list words", action="store_true") | |
parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2], help="increase output verbosity") | |
args = parser.parse_args() | |
def create_table(): | |
c = conn.cursor() | |
c.execute('''CREATE TABLE words (word varchar(50), difficulty int, review bool)''') | |
c.execute('''CREATE UNIQUE INDEX w1 ON words(word)''') | |
# Save (commit) the changes | |
conn.commit() | |
# We can also close the connection if we are done with it. | |
# Just be sure any changes have been committed or they will be lost. | |
conn.close() | |
def upsert_word(): | |
w = args.add | |
c = conn.cursor() | |
# Caveat: 1. pass tuple | |
# 2. REPLACE bypass 'unique' constraint error message. | |
c.execute('INSERT or REPLACE into words (word, difficulty, review)' | |
'VALUES (?,?,?)', (w,0,1)) | |
print 'word upsert.' | |
conn.commit() | |
conn.close() | |
# print args | |
if args.add: | |
upsert_word() | |
if args.l: | |
c = conn.cursor() | |
records = c.execute('select * from words where review=1').fetchall() | |
for r in records: | |
print r[0] | |
conn.commit() | |
conn.close() | |
if args.c: | |
print 'creating table' | |
create_table() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment