python - Read first row in loop using sqlite's fetchone() -


i using loop contents of fetchone(), every time code reads fetchone() cursor moves next row. understand need careful. using example found here https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-fetchone.html works when have more 1 row when there single row error

    k, v in cur.fetchone().items(): attributeerror: 'nonetype' object has no attribute 'items' 

the 2 relevant functions

def dict_factory(cursor, row):     d = {}     idx, col in enumerate(cursor.description):         d[col[0]] = row[idx]     return d  def open_sql(sql_folder, sql_name, sql_table):     # databases located @ /work/jmjohnso1/db_project       path_name = os.path.join(sql_folder,sql_name).strip()     con = lite.connect(path_name)     con.row_factory = dict_factory     cur = con.cursor()     cur.execute('select * ' + sql_table)       dict_contents = defaultdict(list)       row in cur:         k, v in cur.fetchone().items():             dict_contents[k].append(v)      con.close()     print(dict_contents)     return dict_contents 

in case want entire code:

# python3.5 # pymongo version 3.2.2 # mongodb shell version: 3.0.11  import os import pymongo pymongo import mongoclient import sqlite3 lite import pyewf import hashlib itertools import chain collections import defaultdict import pprint  def list_sql_db(folder):     # need list multiprocessing made file.      file_name = os.path.join(folder, 'sql_db')     if not os.path.isfile(file_name):         open (file_name, 'w') line:             (dirpath, dirs, files) in os.walk(folder):                 name in files:                     line.write(name + '\n')     return file_name      def dict_factory(cursor, row):     d = {}     idx, col in enumerate(cursor.description):         d[col[0]] = row[idx]     return d  def open_sql(sql_folder, sql_name, sql_table):     # databases located @ /work/jmjohnso1/db_project       path_name = os.path.join(sql_folder,sql_name).strip()     con = lite.connect(path_name)     con.row_factory = dict_factory     cur = con.cursor()     cur.execute('select * ' + sql_table)       dict_contents = defaultdict(list)       row in cur:         k, v in cur.fetchone().items():             dict_contents[k].append(v)      con.close()     print(dict_contents)     return dict_contents  def insert_tsk_mongo(sql_folder, sql_name, sql_table):     client = mongoclient() # connect mongodb     db = client.nus # make or use db called nus     contents = open_sql(sql_folder, sql_name, sql_table)     collection = sql_name.strip().replace('-','_') # because mongo write not read collection -      # document_id = db[collection].insert({ # sql_name hard drive name          # sql_table:             # contents                # })  ###############################################################################  sql_folder = '/work/jmjohnso1/db_project'     # sql_tables = ['tsk_fs_info', 'tsk_image_info',               # 'tsk_db_info ', 'tsk_image_names',               # 'tsk_file_layout', 'tsk_objects',               # 'tsk_files', 'tsk_vs_info', 'tsk_vs_parts']  sql_tables = ['tsk_fs_info']                sql_folder_name = list_sql_db(sql_folder)  open (sql_folder_name, 'r') read:      sql_names = read.readlines()  sql_name in sql_names:     sql_table in sql_tables:         insert_tsk_mongo(sql_folder, sql_name, sql_table)     break     

you fetching rows twice, once for row in cur: loop, in loop cur.fetchone(). @ last row, have run out of rows fetch, none returned.

there no need use cur.fetchone() when looping on cursor. drop cur.fetchone() call , use row fetched for:

for row in cur:     k, v in row.items():         dict_contents[k].append(v) 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -