Skip to content

Commit 17e56a2

Browse files
committed
Merge branch 'master' of github.com-pybites:pybites/100DaysOfCode
2 parents c6ff4ef + 7c3b432 commit 17e56a2

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

025/gen_testdb.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!python3
2+
#generator to create an sqlite3 test database file with the name of your choice
3+
4+
#TODO - expand functionality to allow user specification of table name, number of cols, col names and types.
5+
6+
from contextlib import contextmanager
7+
import sqlite3
8+
9+
name = ""
10+
11+
@contextmanager
12+
def create_db(name):
13+
try:
14+
conn = sqlite3.connect('%s.db' % name)
15+
cursor = conn.cursor()
16+
yield cursor
17+
finally:
18+
conn.close()
19+
20+
21+
def prompt_for_name():
22+
name = input("What would you like to name your test db file?: ")
23+
return name
24+
25+
if __name__ == "__main__":
26+
name = prompt_for_name()
27+
with create_db(name) as cursor:
28+
cursor.execute("""CREATE TABLE test_table
29+
(col1 TEXT, col2 TEXT, col3 TEXT, col4 INT)
30+
""")
31+
print('%s.db has been created' % name)

LOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
| 022 | Apr 20, 2017 | [create and paste #Amazon affiliation link to clipboard #pyperclip @AlSweigart](022) | nice little utility to copy an take Amazon link from clipboard, convert it into an affiliation link and paste it back to clipboard |
2727
| 023 | Apr 21, 2017 | [use Counter to count the most common words in a file](023) | we did some time ago, collections module is awesome |
2828
| 024 | Apr 22, 2017 | [generate color hex codes from random RGBs and color terminal text](024) | nice play with generators, RGB to hex with format, and colorizing the terminal with the [colored](https://pypi.python.org/pypi/colored) package. This could be useful for future cli apps |
29-
| 025 | Apr 23, 2017 | [TITLE](025) | LEARNING |
29+
| 025 | Apr 23, 2017 | [Simple test #database generator script #python #sqlite #contextmanager](025) | Script to generate a quick, test sqlite3 database with 1 table and 3 columns. Can be customised and expanded to suit your needs. Definitely useful for playing around with persistent data. |
3030
| 026 | Apr 24, 2017 | [TITLE](026) | LEARNING |
3131
| 027 | Apr 25, 2017 | [TITLE](027) | LEARNING |
3232
| 028 | Apr 26, 2017 | [TITLE](028) | LEARNING |

0 commit comments

Comments
 (0)