MCPcopy Create free account
hub / github.com/ActiveState/code / main

Function main

recipes/Python/578285_Saving_snippets_SQLite3/recipe-578285.py:19–163  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

17fileN = open('database.db', 'a+')
18
19def main():
20
21 # add all arguments needed
22 # for argument that need FILE use [ type=argparse.FileType('r') ]
23 parser = argparse.ArgumentParser(description=None, usage='python %(prog)s -h --help -f file -s search -a \'title\' \'code here\' -e id -d id -v --version')
24 parser.add_argument('-f', metavar='filename', type=argparse.FileType('r'), dest='filename', help='File for database')
25 parser.add_argument('-s', metavar='string', dest='search', help='Search for string in database')
26 parser.add_argument('-a', metavar='string', dest='add', nargs=2, help='Add snippet. You should use \'\' for long string')
27 parser.add_argument('-e', metavar='id', type=int, dest='edit', help='Edit snippet')
28 parser.add_argument('-d', metavar='id', type=int, dest='delete', help='Delete from database')
29 parser.add_argument('-S', dest='show', action='store_true', help='Show all records')
30 parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0', help='Print version')
31
32 # parse all arguments to 'args'
33 args = parser.parse_args()
34
35 # database connection
36 conn = sqlite3.connect('database.db')
37 cur = conn.cursor()
38
39 def createTable():
40 cmd = 'CREATE TABLE IF NOT EXISTS snippets (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR(50), code VARCHAR NOT NULL)'
41 cur.execute(cmd)
42 conn.commit()
43
44 def insertSnippets():
45 # convert the string to lowercase and then execute
46 lst = [args.add[0].lower() , args.add[1].lower()]
47 cmd = 'INSERT INTO snippets VALUES (NULL, ?, ?)'
48 cur.execute(cmd, lst)
49 conn.commit()
50
51 print('\nNew Snippet...')
52 print('Snippet Title: \t%s' % args.add[0])
53 print('Code: \t\t%s' % args.add[1])
54
55 def editSnippets():
56 cur.execute('SELECT * FROM snippets where id=%s' % str(args.edit))
57 res = cur.fetchone()
58 if res is None:
59 print('No record to edit!')
60 else:
61 resl = [result for result in res]
62 print('Current title >> ' + resl[1])
63 ed1 = input('Title (Leave black, same title): ')
64 print('Current snippet >> ' + resl[2])
65 ed2 = input('Code: ')
66 if ed1 is '':
67 ed1 = resl[1]
68 cur.executemany('UPDATE snippets SET title=\'%s\', code=\'%s\' WHERE id=?' % (ed1.lower(), ed2.lower()), str(resl[0]))
69 conn.commit()
70 print('Done!\n')
71
72 def deleteSnippets():
73 print('\nDeleting record with ID %s ...' % str(args.delete))
74
75 # first find if record exists and return false if not found
76 cur.execute('SELECT * FROM snippets where id=%s' % str(args.delete))

Callers 1

recipe-578285.pyFile · 0.70

Calls 13

createTableFunction · 0.85
printFunction · 0.85
showOrSearchFunction · 0.85
editSnippetsFunction · 0.85
insertSnippetsFunction · 0.85
deleteSnippetsFunction · 0.85
cursorMethod · 0.80
print_helpMethod · 0.80
exitFunction · 0.50
parse_argsMethod · 0.45
connectMethod · 0.45
commitMethod · 0.45

Tested by

no test coverage detected