SQLite Forum

how to open or create db in sqlite3
Login

how to open or create db in sqlite3

(1) By anonymous on 2021-03-08 05:27:57 [link] [source]

I have code in python to create or open db, but it is throwing error. Any suggestions
Error:" 'tuple' object does not support item assignment" 

import sqlite3
import os


class sqlite1():

    
    def __init__(self, pa, na, rq):

        
        self.dbname = None
        self.name = None
        self.id1 = 0



        if rq == 'open':
          
            print(pa, na)
            self.hdl = (os.path.join(pa, na),'open')
            if not b'id1' in self.hdl or not b'ns' in self.hdl:
                print("open")

            try:
                self.id1 = int(self.hdl[b'id1'])
                self.name = self.hdl[b'ns']
            except:
                pass

        else:
           
            self.hdl = (os.path.join(pa, na),'c')
            self.id1 = 0
            print("File created")

            self.hdl[b'id1'] = str(self.id1)
            


    def db_connect(self):
       
        con = sqlite3.connect(self.na)
        return con

    def getID(self):
        return self.hdl[b'id1']
    def setID(self, id1):
        self.hdl[b'id1'] = id1



Testscript:

from sqlite123 import *
from sqlite123 import sqlite1

class Test1:
    def __init__(self, pa, na, rq):
        self.pa = pa
        self.na = na
        self.id1 = 0
        self.scm1 = sqlite1(pa, na, rq)
        


    def fileopen(self, pa, na, rq):
        self.scm1 = sqlite1(pa, na, rq)
        



if __name__ == '__main__':
   
    tst = Test1('', 'file.db', 'c')
    tst.fileopen('', 'file.db', 'open')
   
    con1 = sqlite1.db_connect(tst)
    #cur = con1.cursor()

(2) By Larry Brasfield (larrybr) on 2021-03-08 12:18:26 in reply to 1 [link] [source]

It appears that you are using Python's SQLite adaptor according to its exposed methods and their signatures. (This not saying much.)

Your Python code, exclusive of its SQLite adaptor aspects, is failing. That makes this a Python question rather than a SQLite question, hence off-topic here.

Tip: The name 'na' appears undefined where used.

(3) By David Raymond (dvdraymond) on 2021-03-08 14:35:43 in reply to 1 [source]

self.hdl = (os.path.join(pa, na),'c')
self.id1 = 0
print("File created")
self.hdl[b'id1'] = str(self.id1)

You're setting self.hdl to a 2 element tuple. Then 3 lines later it looks like you're trying to use it as a dictionary. Looks like there's a similar confusion in the other half of that init