How I can manage growing Python3 / SQLite3 / tkinter codes with right way?












0















I'm new in Python and I'm making a desktop application for myself. One of my windows is "Add Company". Here is screenshot: https://ibb.co/hgyFAL



Until now, I'm collecting data from users and I'm storing in SQLite3 with below code:



import sqlite3 as sql
import os


def create_new_company(
name,
address,
district,
city,
country,
general_email,
phone1,
phone2,
fax,
tax_number,
tax_administration,
activity_area,
contact,
contact_task,
is_client,
is_supplier,
is_in_mail_list,
is_in_sms_list,
is_in_black_list,
note,
is_active):

db = sql.connect(str(os.getcwd() + '\Databases\main_db.sqlite3'))
cursor = db.cursor()

cursor.execute("""CREATE TABLE IF NOT EXISTS `companies` (
`company_id` INTEGER NOT NULL DEFAULT 0000001 PRIMARY KEY AUTOINCREMENT UNIQUE,
`name` TEXT,
`address` TEXT,
`district` TEXT,
`city` TEXT,
`country` TEXT,
`general_email` TEXT,
`phone1` NUMERIC,
`phone2` NUMERIC,
`fax` NUMERIC,
`tax_number` TEXT,
`tax_administration` NUMERIC,
`activity_area` TEXT,
`contact` TEXT,
`contact_task` TEXT,
`is_client` INTEGER,
`is_supplier` INTEGER,
`is_in_mail_list` INTEGER,
`is_in_sms_list` NUMERIC,
`is_in_black_list` NUMERIC,
`note` TEXT,
`is_active` NUMERIC);""")

values = (
name,
address,
district,
city,
country,
general_email,
phone1,
phone2,
fax,
tax_number,
tax_administration,
activity_area,
contact,
contact_task,
is_client,
is_supplier,
is_in_mail_list,
is_in_sms_list,
is_in_black_list,
note,
is_active
)
script= """INSERT INTO companies VALUES (
NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"""

cursor.execute(script, values)
db.commit()
db.close()


Until last week, I didn't care the code is good or not. But application is growing and I'm afraid to can't manage the code in future. Actually even now I can't manage and its just have 3 windows. :/



So, I start asking myself "how it can be better" and I searched on the internet for my question. I found good sources and great advice and I updated my code with the following:



class SQLCommands:
sqlite_file = str(os.getcwd() + '\test.db')

def __init__(self):
if os.path.exists(self.sqlite_file):
pass
else:
self.create_new_database(self.sqlite_file)

def create_new_database(self, database_path):
with sqlite3.connect(database_path) as conn:
c = conn.cursor()

c.execute("""CREATE TABLE IF NOT EXISTS `companies` (
`company_id` INTEGER NOT NULL PRIMARY KEY UNIQUE,
`name` TEXT);""")

self.close(conn)

def connect(self, database_path):
with sqlite3.connect(database_path) as conn:
c = conn.cursor()
return conn, c

def close(self, conn):
conn.commit()
conn.close()


With the last update I think is better and basic functions are ok. But now I'm stuck here and I need a road map.



I imagine a function like below but I couldn't bring the end:



class SQLCommands:
sqlite_file = str(os.getcwd() + '\test.db')

# def other_fucntions
# ...some codes here...
# .....

def insert(self, table, column, value):
conn, c = self.connect(self.sqlite_file)
values = (value,)
script = """INSERT INTO {} VALUES (?)""".format(table) # I'm not sure if the correct way is this

c.execute(script, value)
conn.commit()
conn.close()

del value, script, conn, c


I imagine to use like this:



class Company(tk.Toplevel):
def __init__(self):
self.init_ui()

def init_ui(self):
company_name_lbl = ttk.Label(self, text="Company Name").grid(
row=1, column=0, sticky='w')
self.company_name_entry = ttk.Entry(self)
self.company_name_entry.grid(row=1, column=1)

company_address_lbl = ttk.Label(self, text="Company Address").grid(
row=1, column=0, sticky='w')
self.company_address_entry = ttk.Entry(self)
self.company_address_entry.grid(row=1, column=1)

save_btn = ttk.Button(self, text="Save", command=self.insert_to_sql)


def insert_to_sql(self):
value_list = ["NULL", self.company_name_entry.get(), self.company_address_entry.get()]
column_header_list = ["company_id", "name", "adress"]

sql = SQLCommands()

for i in range(len(value_list)):
sql.insert(table="companies", column=column_header_list[i], value=value_list[i])


My future goal is learning Django and carrying my app to the web with Django. So, SQLCommands class must not depend on Tkinter.



Am I on the right track? Is this right way to do? If it is, how I can complete the code "SQLCommands.insert"



My English may not be clear but I try my best.



Sorry in advance and thanks a lot in advance.
Alperen










share|improve this question





























    0















    I'm new in Python and I'm making a desktop application for myself. One of my windows is "Add Company". Here is screenshot: https://ibb.co/hgyFAL



    Until now, I'm collecting data from users and I'm storing in SQLite3 with below code:



    import sqlite3 as sql
    import os


    def create_new_company(
    name,
    address,
    district,
    city,
    country,
    general_email,
    phone1,
    phone2,
    fax,
    tax_number,
    tax_administration,
    activity_area,
    contact,
    contact_task,
    is_client,
    is_supplier,
    is_in_mail_list,
    is_in_sms_list,
    is_in_black_list,
    note,
    is_active):

    db = sql.connect(str(os.getcwd() + '\Databases\main_db.sqlite3'))
    cursor = db.cursor()

    cursor.execute("""CREATE TABLE IF NOT EXISTS `companies` (
    `company_id` INTEGER NOT NULL DEFAULT 0000001 PRIMARY KEY AUTOINCREMENT UNIQUE,
    `name` TEXT,
    `address` TEXT,
    `district` TEXT,
    `city` TEXT,
    `country` TEXT,
    `general_email` TEXT,
    `phone1` NUMERIC,
    `phone2` NUMERIC,
    `fax` NUMERIC,
    `tax_number` TEXT,
    `tax_administration` NUMERIC,
    `activity_area` TEXT,
    `contact` TEXT,
    `contact_task` TEXT,
    `is_client` INTEGER,
    `is_supplier` INTEGER,
    `is_in_mail_list` INTEGER,
    `is_in_sms_list` NUMERIC,
    `is_in_black_list` NUMERIC,
    `note` TEXT,
    `is_active` NUMERIC);""")

    values = (
    name,
    address,
    district,
    city,
    country,
    general_email,
    phone1,
    phone2,
    fax,
    tax_number,
    tax_administration,
    activity_area,
    contact,
    contact_task,
    is_client,
    is_supplier,
    is_in_mail_list,
    is_in_sms_list,
    is_in_black_list,
    note,
    is_active
    )
    script= """INSERT INTO companies VALUES (
    NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"""

    cursor.execute(script, values)
    db.commit()
    db.close()


    Until last week, I didn't care the code is good or not. But application is growing and I'm afraid to can't manage the code in future. Actually even now I can't manage and its just have 3 windows. :/



    So, I start asking myself "how it can be better" and I searched on the internet for my question. I found good sources and great advice and I updated my code with the following:



    class SQLCommands:
    sqlite_file = str(os.getcwd() + '\test.db')

    def __init__(self):
    if os.path.exists(self.sqlite_file):
    pass
    else:
    self.create_new_database(self.sqlite_file)

    def create_new_database(self, database_path):
    with sqlite3.connect(database_path) as conn:
    c = conn.cursor()

    c.execute("""CREATE TABLE IF NOT EXISTS `companies` (
    `company_id` INTEGER NOT NULL PRIMARY KEY UNIQUE,
    `name` TEXT);""")

    self.close(conn)

    def connect(self, database_path):
    with sqlite3.connect(database_path) as conn:
    c = conn.cursor()
    return conn, c

    def close(self, conn):
    conn.commit()
    conn.close()


    With the last update I think is better and basic functions are ok. But now I'm stuck here and I need a road map.



    I imagine a function like below but I couldn't bring the end:



    class SQLCommands:
    sqlite_file = str(os.getcwd() + '\test.db')

    # def other_fucntions
    # ...some codes here...
    # .....

    def insert(self, table, column, value):
    conn, c = self.connect(self.sqlite_file)
    values = (value,)
    script = """INSERT INTO {} VALUES (?)""".format(table) # I'm not sure if the correct way is this

    c.execute(script, value)
    conn.commit()
    conn.close()

    del value, script, conn, c


    I imagine to use like this:



    class Company(tk.Toplevel):
    def __init__(self):
    self.init_ui()

    def init_ui(self):
    company_name_lbl = ttk.Label(self, text="Company Name").grid(
    row=1, column=0, sticky='w')
    self.company_name_entry = ttk.Entry(self)
    self.company_name_entry.grid(row=1, column=1)

    company_address_lbl = ttk.Label(self, text="Company Address").grid(
    row=1, column=0, sticky='w')
    self.company_address_entry = ttk.Entry(self)
    self.company_address_entry.grid(row=1, column=1)

    save_btn = ttk.Button(self, text="Save", command=self.insert_to_sql)


    def insert_to_sql(self):
    value_list = ["NULL", self.company_name_entry.get(), self.company_address_entry.get()]
    column_header_list = ["company_id", "name", "adress"]

    sql = SQLCommands()

    for i in range(len(value_list)):
    sql.insert(table="companies", column=column_header_list[i], value=value_list[i])


    My future goal is learning Django and carrying my app to the web with Django. So, SQLCommands class must not depend on Tkinter.



    Am I on the right track? Is this right way to do? If it is, how I can complete the code "SQLCommands.insert"



    My English may not be clear but I try my best.



    Sorry in advance and thanks a lot in advance.
    Alperen










    share|improve this question



























      0












      0








      0








      I'm new in Python and I'm making a desktop application for myself. One of my windows is "Add Company". Here is screenshot: https://ibb.co/hgyFAL



      Until now, I'm collecting data from users and I'm storing in SQLite3 with below code:



      import sqlite3 as sql
      import os


      def create_new_company(
      name,
      address,
      district,
      city,
      country,
      general_email,
      phone1,
      phone2,
      fax,
      tax_number,
      tax_administration,
      activity_area,
      contact,
      contact_task,
      is_client,
      is_supplier,
      is_in_mail_list,
      is_in_sms_list,
      is_in_black_list,
      note,
      is_active):

      db = sql.connect(str(os.getcwd() + '\Databases\main_db.sqlite3'))
      cursor = db.cursor()

      cursor.execute("""CREATE TABLE IF NOT EXISTS `companies` (
      `company_id` INTEGER NOT NULL DEFAULT 0000001 PRIMARY KEY AUTOINCREMENT UNIQUE,
      `name` TEXT,
      `address` TEXT,
      `district` TEXT,
      `city` TEXT,
      `country` TEXT,
      `general_email` TEXT,
      `phone1` NUMERIC,
      `phone2` NUMERIC,
      `fax` NUMERIC,
      `tax_number` TEXT,
      `tax_administration` NUMERIC,
      `activity_area` TEXT,
      `contact` TEXT,
      `contact_task` TEXT,
      `is_client` INTEGER,
      `is_supplier` INTEGER,
      `is_in_mail_list` INTEGER,
      `is_in_sms_list` NUMERIC,
      `is_in_black_list` NUMERIC,
      `note` TEXT,
      `is_active` NUMERIC);""")

      values = (
      name,
      address,
      district,
      city,
      country,
      general_email,
      phone1,
      phone2,
      fax,
      tax_number,
      tax_administration,
      activity_area,
      contact,
      contact_task,
      is_client,
      is_supplier,
      is_in_mail_list,
      is_in_sms_list,
      is_in_black_list,
      note,
      is_active
      )
      script= """INSERT INTO companies VALUES (
      NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"""

      cursor.execute(script, values)
      db.commit()
      db.close()


      Until last week, I didn't care the code is good or not. But application is growing and I'm afraid to can't manage the code in future. Actually even now I can't manage and its just have 3 windows. :/



      So, I start asking myself "how it can be better" and I searched on the internet for my question. I found good sources and great advice and I updated my code with the following:



      class SQLCommands:
      sqlite_file = str(os.getcwd() + '\test.db')

      def __init__(self):
      if os.path.exists(self.sqlite_file):
      pass
      else:
      self.create_new_database(self.sqlite_file)

      def create_new_database(self, database_path):
      with sqlite3.connect(database_path) as conn:
      c = conn.cursor()

      c.execute("""CREATE TABLE IF NOT EXISTS `companies` (
      `company_id` INTEGER NOT NULL PRIMARY KEY UNIQUE,
      `name` TEXT);""")

      self.close(conn)

      def connect(self, database_path):
      with sqlite3.connect(database_path) as conn:
      c = conn.cursor()
      return conn, c

      def close(self, conn):
      conn.commit()
      conn.close()


      With the last update I think is better and basic functions are ok. But now I'm stuck here and I need a road map.



      I imagine a function like below but I couldn't bring the end:



      class SQLCommands:
      sqlite_file = str(os.getcwd() + '\test.db')

      # def other_fucntions
      # ...some codes here...
      # .....

      def insert(self, table, column, value):
      conn, c = self.connect(self.sqlite_file)
      values = (value,)
      script = """INSERT INTO {} VALUES (?)""".format(table) # I'm not sure if the correct way is this

      c.execute(script, value)
      conn.commit()
      conn.close()

      del value, script, conn, c


      I imagine to use like this:



      class Company(tk.Toplevel):
      def __init__(self):
      self.init_ui()

      def init_ui(self):
      company_name_lbl = ttk.Label(self, text="Company Name").grid(
      row=1, column=0, sticky='w')
      self.company_name_entry = ttk.Entry(self)
      self.company_name_entry.grid(row=1, column=1)

      company_address_lbl = ttk.Label(self, text="Company Address").grid(
      row=1, column=0, sticky='w')
      self.company_address_entry = ttk.Entry(self)
      self.company_address_entry.grid(row=1, column=1)

      save_btn = ttk.Button(self, text="Save", command=self.insert_to_sql)


      def insert_to_sql(self):
      value_list = ["NULL", self.company_name_entry.get(), self.company_address_entry.get()]
      column_header_list = ["company_id", "name", "adress"]

      sql = SQLCommands()

      for i in range(len(value_list)):
      sql.insert(table="companies", column=column_header_list[i], value=value_list[i])


      My future goal is learning Django and carrying my app to the web with Django. So, SQLCommands class must not depend on Tkinter.



      Am I on the right track? Is this right way to do? If it is, how I can complete the code "SQLCommands.insert"



      My English may not be clear but I try my best.



      Sorry in advance and thanks a lot in advance.
      Alperen










      share|improve this question
















      I'm new in Python and I'm making a desktop application for myself. One of my windows is "Add Company". Here is screenshot: https://ibb.co/hgyFAL



      Until now, I'm collecting data from users and I'm storing in SQLite3 with below code:



      import sqlite3 as sql
      import os


      def create_new_company(
      name,
      address,
      district,
      city,
      country,
      general_email,
      phone1,
      phone2,
      fax,
      tax_number,
      tax_administration,
      activity_area,
      contact,
      contact_task,
      is_client,
      is_supplier,
      is_in_mail_list,
      is_in_sms_list,
      is_in_black_list,
      note,
      is_active):

      db = sql.connect(str(os.getcwd() + '\Databases\main_db.sqlite3'))
      cursor = db.cursor()

      cursor.execute("""CREATE TABLE IF NOT EXISTS `companies` (
      `company_id` INTEGER NOT NULL DEFAULT 0000001 PRIMARY KEY AUTOINCREMENT UNIQUE,
      `name` TEXT,
      `address` TEXT,
      `district` TEXT,
      `city` TEXT,
      `country` TEXT,
      `general_email` TEXT,
      `phone1` NUMERIC,
      `phone2` NUMERIC,
      `fax` NUMERIC,
      `tax_number` TEXT,
      `tax_administration` NUMERIC,
      `activity_area` TEXT,
      `contact` TEXT,
      `contact_task` TEXT,
      `is_client` INTEGER,
      `is_supplier` INTEGER,
      `is_in_mail_list` INTEGER,
      `is_in_sms_list` NUMERIC,
      `is_in_black_list` NUMERIC,
      `note` TEXT,
      `is_active` NUMERIC);""")

      values = (
      name,
      address,
      district,
      city,
      country,
      general_email,
      phone1,
      phone2,
      fax,
      tax_number,
      tax_administration,
      activity_area,
      contact,
      contact_task,
      is_client,
      is_supplier,
      is_in_mail_list,
      is_in_sms_list,
      is_in_black_list,
      note,
      is_active
      )
      script= """INSERT INTO companies VALUES (
      NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"""

      cursor.execute(script, values)
      db.commit()
      db.close()


      Until last week, I didn't care the code is good or not. But application is growing and I'm afraid to can't manage the code in future. Actually even now I can't manage and its just have 3 windows. :/



      So, I start asking myself "how it can be better" and I searched on the internet for my question. I found good sources and great advice and I updated my code with the following:



      class SQLCommands:
      sqlite_file = str(os.getcwd() + '\test.db')

      def __init__(self):
      if os.path.exists(self.sqlite_file):
      pass
      else:
      self.create_new_database(self.sqlite_file)

      def create_new_database(self, database_path):
      with sqlite3.connect(database_path) as conn:
      c = conn.cursor()

      c.execute("""CREATE TABLE IF NOT EXISTS `companies` (
      `company_id` INTEGER NOT NULL PRIMARY KEY UNIQUE,
      `name` TEXT);""")

      self.close(conn)

      def connect(self, database_path):
      with sqlite3.connect(database_path) as conn:
      c = conn.cursor()
      return conn, c

      def close(self, conn):
      conn.commit()
      conn.close()


      With the last update I think is better and basic functions are ok. But now I'm stuck here and I need a road map.



      I imagine a function like below but I couldn't bring the end:



      class SQLCommands:
      sqlite_file = str(os.getcwd() + '\test.db')

      # def other_fucntions
      # ...some codes here...
      # .....

      def insert(self, table, column, value):
      conn, c = self.connect(self.sqlite_file)
      values = (value,)
      script = """INSERT INTO {} VALUES (?)""".format(table) # I'm not sure if the correct way is this

      c.execute(script, value)
      conn.commit()
      conn.close()

      del value, script, conn, c


      I imagine to use like this:



      class Company(tk.Toplevel):
      def __init__(self):
      self.init_ui()

      def init_ui(self):
      company_name_lbl = ttk.Label(self, text="Company Name").grid(
      row=1, column=0, sticky='w')
      self.company_name_entry = ttk.Entry(self)
      self.company_name_entry.grid(row=1, column=1)

      company_address_lbl = ttk.Label(self, text="Company Address").grid(
      row=1, column=0, sticky='w')
      self.company_address_entry = ttk.Entry(self)
      self.company_address_entry.grid(row=1, column=1)

      save_btn = ttk.Button(self, text="Save", command=self.insert_to_sql)


      def insert_to_sql(self):
      value_list = ["NULL", self.company_name_entry.get(), self.company_address_entry.get()]
      column_header_list = ["company_id", "name", "adress"]

      sql = SQLCommands()

      for i in range(len(value_list)):
      sql.insert(table="companies", column=column_header_list[i], value=value_list[i])


      My future goal is learning Django and carrying my app to the web with Django. So, SQLCommands class must not depend on Tkinter.



      Am I on the right track? Is this right way to do? If it is, how I can complete the code "SQLCommands.insert"



      My English may not be clear but I try my best.



      Sorry in advance and thanks a lot in advance.
      Alperen







      python python-3.x tkinter sqlite3 ttk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 18:00









      lgwilliams

      503417




      503417










      asked Nov 14 '18 at 13:31









      AlperenAlperen

      13




      13
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301421%2fhow-i-can-manage-growing-python3-sqlite3-tkinter-codes-with-right-way%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301421%2fhow-i-can-manage-growing-python3-sqlite3-tkinter-codes-with-right-way%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values