1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import sqlite3
import click
click.disable_unicode_literals_warning = True
sql = """create table if not exists users(
id integer primary key autoincrement,
name varchar(30)
)
"""
@click.group()
def cli():
pass
@cli.command(short_help="initialize database and tables")
def init_db():
conn = sqlite3.connect("test.db")
cur = conn.cursor()
cur.execute(sql)
conn.commit()
conn.close()
@cli.command(short_help="fill records to database")
@click.option("--total", "-t", default=300, help="fill data for example")
def fill_data(total):
conn = sqlite3.connect("test.db")
cur = conn.cursor()
for i in range(total):
cur.execute("insert into users (name) values (?)", ["name" + str(i)])
conn.commit()
conn.close()
if __name__ == "__main__":
cli()
|