def gen_db(path=expand_pj('@/raw/yarxi.db')): with sqlite3.connect(path) as conn: # if not fs.exists(path) or os.stat(path).st_size == 0: with open(expand_pj(':/db-schema.sql'), 'rt') as f: # TRY:FIND: use external arguments/variables when executing db-schema.sql conn.executescript(f.read()) tbl = 'Kanji' # for obj in parse_kan('@/jr_kan.txt'): # print(obj) SQL_kan = "insert into {} ({}) values ({})".format( tbl, ', '.join(Kan_t._fields), ', '.join([':' + s for s in Kan_t._fields])) cursor = conn.cursor() cursor.executemany(SQL_kan, parse_kan('@/raw/jr_kan.txt'))
def read_jr(nm): with open(expand_pj(nm), encoding='cp1251') as f: next(f) # Skip date num = int(next(f)) # Line count (check corrupted file) for line in f: line = line.strip() if line == '.': # End of file break num -= 1 yield line if num != 0 or line != '.': # Checksum raise IOError("Corrupted file '" + nm + "'")
def read_db(path): with sqlite3.connect(expand_pj(path)) as conn: cursor = conn.cursor() tbl = 'Kanji' col = '*' row = '12' row = "{} LIMIT {}-1,5".format(tbl, row) if row else tbl req = "SELECT {} FROM {}".format(col, row) cursor.execute(req) for row in cursor.fetchall(): print(row)