• Rufat

    Here is how it works for me.

         import ui
         import sqlite3 as db
         import dialogs
    
         conn = db.connect('pythonsqlite.db')
         conn.row_factory = lambda cursor, row: row[0]
         c = conn.cursor()
        ids = c.execute('SELECT name FROM Names').fetchall()
        ids1 = c.execute('SELECT surname FROM Names').fetchall()
    
    
      class MyTableViewDataSource (object):
       def __init__(self):
        self.items = ids        
        self.items1 = ids1
        
    def tableview_number_of_sections(self, tableview):
        # Return the number of sections (defaults to 1)
        return 1
    
    def tableview_number_of_rows(self, tableview, section):
        # Return the number of rows in the section
        return len(self.items)
    
    def tableview_cell_for_row(self, tableview, section, row):
        # Create and return a cell for the given section/row
        cell = ui.TableViewCell('subtitle')
        cell.text_label.text = self.items[row]
        cell.detail_text_label.text = self.items1[row]
        return cell
    
    def tableview_title_for_header(self, tableview, section):
        # Return a title for the given section.
        # If this is not implemented, no section headers will be shown.
        return ('Name')
        
    
    def tableview_can_delete(self, tableview, section, row):
        # Return True if the user should be able to delete the given row.
        return True
    
    def tableview_can_move(self, tableview, section, row):
        # Return True if a reordering control should be shown for the given row (in editing mode).
        return True
    
    def tableview_delete(self, tableview, section, row):
        # Called when the user confirms deletion of the given row.
        #my = ("Name=""'"+tableview.data_source.data[row]+"'")
        #print(my)
        print ('Delete row ' + tableview.data_source.items[row])
        sqliteConnection = db.connect('pythonsqlite.db')
        cursor = sqliteConnection.cursor()
        print("Connected to SQLite")
        
        # Deleting single record now
        sql_delete_query = "DELETE from Names where Name=""'"+tableview.data_source.items[row]+"'"
        print(sql_delete_query)
        cursor.execute(sql_delete_query)
        sqliteConnection.commit()
        print("Record deleted successfully ")
        
        del tableview.data_source.items[row]
        del tableview.data_source.items1[row]
        cursor.close()
        tableview.reload()
        pass
    
    def tableview_move_row(self, tableview, from_section, from_row, to_section, to_row):
        # Called when the user moves a row with the reordering control (in editing mode).
        pass
        
    def add(self, sender):
        item = dialogs.input_alert('Add your name')
        item1 = dialogs.input_alert('Add your surname')
        if not item == None:
            self.items.append(item)
            self.items1.append(item1)
            view['tableview1'].reload()
        sqliteConnection = db.connect('pythonsqlite.db')
        cursor = sqliteConnection.cursor()
        print("Connected to SQLite")
        # adding single record now
        sql_add_data = "INSERT INTO Names (Name, Surname) VALUES "+"("+"'"+item+"'"+", "+"'"+item1+"'"+")"
        cursor.execute(sql_add_data)
        sqliteConnection.commit()
        cursor.close()
        
    
        view = ui.load_view('MyForm')
        source = MyTableViewDataSource()
        view.right_button_items = [ui.ButtonItem(title='add', action=source.add)]
        view['tableview1'].data_source = source
        view.present('sheet')

    posted in Pythonista read more
  • Rufat

    I did it, thank you so much!

    posted in Pythonista read more
  • Rufat

    Thank you so much, I’l try to figure it out. Just can’t find any live example in the web. But how it works for deleting? And same thing (System) doesn’t work for adding the rows.

    posted in Pythonista read more
  • Rufat

    This is the full code

    import ui
    import sqlite3 as db
    
    conn = db.connect('pythonsqlite.db')
    conn.row_factory = lambda cursor, row: row[0]
    c = conn.cursor()
    ids = c.execute('SELECT name FROM Names').fetchall()
    ids1 = c.execute('SELECT surname FROM Names').fetchall()
    
    class MyTableViewDataSource (object):           
            
    def tableview_number_of_sections(self, tableview):
        # Return the number of sections (defaults to 1)
      return 1
    
    def tableview_number_of_rows(self, tableview, section):
        # Return the number of rows in the section
      return len(ids)
    
    def tableview_cell_for_row(self, tableview, section, row):
        # Create and return a cell for the given section/row
        
        self.data = ids
        self.data1 = ids1
        self.cells = [ui.TableViewCell('value1') #'subtitle'
                      for _ in range(len(self.data))]
        cell = self.cells[row]
        cell.text_label.text = self.data[row]
        cell.detail_text_label.text = self.data1[row]
        return cell
    
    def tableview_title_for_header(self, tableview, section):
        # Return a title for the given section.
        # If this is not implemented, no section headers will be shown.
        return ('Name')
        
    def tableview_can_delete(self, tableview, section, row):
        # Return True if the user should be able to delete the given row.
        return True
    
    def tableview_can_move(self, tableview, section, row):
        # Return True if a reordering control should be shown for the given row (in editing mode).
        return True
    
    def tableview_delete(self, tableview, section, row):
        # Called when the user confirms deletion of the given row.
        print ('Delete row ' + tableview.data_source.data[row])
        sqliteConnection = db.connect('pythonsqlite.db')
        cursor = sqliteConnection.cursor()
        print("Connected to SQLite")
        
        # Deleting single record now
        sql_delete_query = "DELETE from Names where Name=""'"+tableview.data_source.data[row]+"'"
        print(sql_delete_query)
        cursor.execute(sql_delete_query)
        sqliteConnection.commit()
        print("Record deleted successfully ")
        
        del tableview.data_source.data[row]
        del tableview.data_source.data1[row]
        cursor.close()
        tableview.reload()
        pass
    
    def tableview_move_row(self, tableview, from_section, from_row, to_section, to_row):
        # Called when the user moves a row with the reordering control (in editing mode).
        pass
        
    def addBtn(sender):
        sqliteConnection = db.connect('pythonsqlite.db')
        cursor = sqliteConnection.cursor()
        print("Connected to SQLite")
        # adding single record now
        sql_add_data = "INSERT INTO Names (Name, Surname) VALUES ('?','?')"
        print(sql_add_data)
        cursor.execute(sql_add_data)
        sqliteConnection.commit()
        print("Record successfully Added ")
    
    v = ui.load_view('MyForm')
    v['tableview1'].data_source = MyTableViewDataSource()
    v.present('sheet')

    posted in Pythonista read more
  • Rufat

    Thank you, that is the problem I can’t let the tableview to know that it is a new rows available.

                view = ui.load_view('MyForm')
        view['tableview1'].reload_data()
        view['tableview1'].data_source = MyTableViewDataSource()

    posted in Pythonista read more
  • Rufat

    Hi, was wondering how can I reload tableview in my form, it does not give me any mistake, data inserts in database and I can see it after reloading the form. But can I reload tableview without reloading from? Thanks.

    data = MyTableViewDataSource()
    v = ui.TableView()
    
    def button_tapped(sender):
        sqliteConnection = db.connect('pythonsqlite.db')
        cursor = sqliteConnection.cursor()
        print("Connected to SQLite")
            
            # adding single record now
        sql_add_data = "INSERT INTO Names (Name, Surname) VALUES ('?','?')"
        print(sql_add_data)
        cursor.execute(sql_add_data)
        sqliteConnection.commit()
        print("Record successfully Added ")
        print('button tapped')
        v['tableview1'].reload()
        
    v['tableview1'].data_source=data
    
    v = ui.load_view('MyForm')
    v['tableview1'].data_source=MyTableViewDataSource()
    v.present('sheet')
    

    posted in Pythonista read more
  • Rufat

    Thank you!

    posted in Pythonista read more
  • Rufat

    Hi, I’m trying to add detail text label, and can’t to create it on each row, I guess problem is in this row type = {0: ‘subtitle, i: ‘subtitle’} Can’t create loop for each row
    import ui
    import sqlite3 as db

          conn = db.connect('pythonsqlite.db')
          conn.row_factory = lambda cursor, row: row[0]
          c = conn.cursor()
          ids = c.execute('SELECT name FROM Names').fetchall()
          ids1 = c.execute('SELECT surname FROM Names').fetchall()
          me = ids1
    
           for i in range (len(ids)):
            print(i)
    
           class MyTableViewDataSource (object):
    
    def tableview_number_of_sections(self, tableview):
        # Return the number of sections (defaults to 1)
        return 1
    
    def tableview_number_of_rows(self, tableview, section):
        # Return the number of rows in the section
        return len(ids)
    
    def tableview_cell_for_row(self, tableview, section, row):
        # Create and return a cell for the given section/row
    
                
        type = {0: 'subtitle', i: 'subtitle'}[row]
        
        self.data = ids
        self.data1 = me
        self.cells = [ui.TableViewCell(type)
                      for _ in range(len(self.data))]
        cell = self.cells[row]
        cell.text_label.text = self.data[row]
        cell.detail_text_label.text = self.data1[row]
        return cell
    
    def tableview_title_for_header(self, tableview, section):
        # Return a title for the given section.
        # If this is not implemented, no section headers will be shown.
        return ('Names')
    
    
    def tableview_delete(self, tableview, section, row):
        # Called when the user confirms deletion of the given row.
        pass
    
    def tableview_move_row(self, tableview, from_section, from_row, to_section, to_row):
        # Called when the user moves a row with the reordering control (in editing mode).
        pass
    

    v = ui.load_view('MyForm')
    #v['tableview1'].background_color = 'yellow'
    v['tableview1'].data_source=MyTableViewDataSource()
    v.present('sheet')

    posted in Pythonista read more
  • Rufat

    Thank you @mikael, this example a little bit different. Is it possible to put some labels in the row of tableview and then get reference that label1 is the name from database, label 2 surname as example.

    posted in Pythonista read more
  • Rufat

    Thank you so much, I got it. But how it works if I have multiple columns?

    posted in Pythonista read more
  • Rufat

    I did it with custom tableview, but how can I did the same with existing one in ui form?

    posted in Pythonista read more
  • Rufat

    Hi, I’m new to Pythonista, and I’m struggling with binding SQLite data to custom Tableview and then in UI form. Here is my code. What I’m doing wrong?

              import ui
              import sqlite3 as dB
           #tv = ui.TableView()
           conn = db.connect('pythonsqlite.db')
           conn.row_factory = lambda cursor, row: row[0]
           c = conn.cursor()
           ids = c.execute('SELECT name FROM projects').fetchall()
           print(len(ids))
    
           #tv.data_source = ui.ListDataSource(ids)
           #tv.present('fullscreen')
    
    
            class MyTableViewDataSource (object):
        def tableview_number_of_sections(self, tableview):
             # Return the number of sections (defaults to 1)
             return 1
    
         def tableview_number_of_rows(self, tableview, section):
        # Return the number of rows in the section
        return (len(ids))
    
        def tableview_cell_for_row(self, tableview, section, row):
        # Create and return a cell for the given section/row
        cell = ui.TableViewCell()
        data_source = ui.ListDataSource(ids)
        cell.data_source = cell.delegate = data_source
        print(ids)
        return cell
    
    def tableview_title_for_header(self, tableview, section):
        # Return a title for the given section.
        # If this is not implemented, no section headers will be shown.
        return 'Names'
    
    def tableview_can_delete(self, tableview, section, row):
        # Return True if the user should be able to delete the given row.
        return True
    
    def tableview_can_move(self, tableview, section, row):
        # Return True if a reordering control should be shown for the given row (in editing mode).
        return True
    
    def tableview_delete(self, tableview, section, row):
        # Called when the user confirms deletion of the given row.
        pass
    
    def tableview_move_row(self, tableview, from_section, from_row, to_section, to_row):
    
        # Called when the user moves a row with the reordering control (in editing mode).
        pass
                 t=ui.TableView()
                 t.frame=(0,0,200,480)
                 t.data_source=MyTableViewDataSource()
                 t.present('sheet')
    
                 #ui.load_view('MyForm').present('sheet')

    posted in Pythonista read more
Internal error.

Oops! Looks like something went wrong!