making sqlite3 as multi obj as possible

This is the place for queries that don't fit in any of the other categories.

making sqlite3 as multi obj as possible

Postby metulburr » Mon Mar 25, 2013 1:03 pm

So pretty much the idea is to minimze the code needed to recode when there needs to be a change in the amount of indexes in the obj.

As currently i will only be using one table, at least i think. I have only one obj to insert into the database. So everything is reflecting on that obj (self.data)

So i tried making executemany execute as many as there are indexes in self.data, however i could not figure out the same thing to do with self. table_form. I am not sure if it is even possible? ACtually i have came up with something that might implement into it?
Code: Select all
lister = [
   [1,'test',1.1]
]

for index in lister[0]:
   a = type(index).__name__.upper()
   if a == 'STR':
      a = 'TEXT'
   print(a)


and here is the sqlite3 code:
Code: Select all
import sqlite3

class Database:
   def __init__(self, filename):
      self.filename = filename
      
      self.con = None
      self.con = sqlite3.connect(self.filename)
      self.cur = self.con.cursor()
      
      self.table_name = 'test2'
      self.table_form = 'CREATE TABLE {}(Id INT, Name TEXT, Price INT)'.format(self.table_name)
      
      self.data = [
         [8, 'testing', 123]
      ]



   def update(self):
      with self.con:
         self.cur.execute("DROP TABLE IF EXISTS {}".format(self.table_name))
         self.cur.execute(self.table_form)
         self.cur.executemany(
            "INSERT INTO {} VALUES({})".format(
               self.table_name, ','.join(['?'] * len(self.data[0]))),
             self.data)
         
   def test(self):
      with self.con:
         self.cur.execute("SELECT * FROM {}".format(self.table_name))

         rows = self.cur.fetchall()
         print(rows)
         print(type(rows))
         print(rows[0])
         print(rows[0][0])
         for row in rows:
            print(row)
            
DB = Database('class_test_db.db')
DB.update()
DB.test()


New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1418
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: making sqlite3 as multi obj as possible

Postby setrofim » Mon Mar 25, 2013 1:24 pm

metulburr wrote:So i tried making executemany execute as many as there are indexes in self.data, however i could not figure out the same thing to do with self. table_form. I am not sure if it is even possible? ACtually i have came up with something that might implement into it?

I'm not sure I get what you mean. If you're talking about specifying the columns dynamically, then no, you wouldn't use executemany for that; instead, you'd generate the CREATE TABLE string and execute that once (as I think you're talking about doing). Normally though, you wouldn't dynamically generate tables anyway -- you'd specify a schema and use it (much as you are doing now).
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 1 guest