0

I am trying to get data from JSON. Thereafter, I want to insert it into a new table. In my class DB I have 2 methods: create_connect and add_records. In create_connect method I had created variable 'cur' - cursor. Now, I want to use this variable in add_records method. How I can do that?

I tried the following code but it throws error:

def create_connect(self, dbname):

    cur = con.cursor()

def add_records(self, id, date, open_p, close_p, high_p, low_p, dbname):

    cur.execute(sql_insert_query, recordTuple)

NameError: name 'cur' is not defined

ZF007
  • 3,708
  • 8
  • 29
  • 48
Krispekowy
  • 19
  • 7

1 Answers1

2

use self.cur.

import sqlite3

class ClassName:

    def create_connect(self, dbname):

        con = sqlite3.connect(dbname)
        cur = con.cursor()
        return cur

    def add_records(self, id, date, open_p, close_p, high_p, low_p, dbname):
        cur = self.create_connect(dbname)
        cur.execute(sql_insert_query, recordTuple)
ksouravdas
  • 124
  • 7