omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    SQLite connecting to tableview

    Pythonista
    tableviewcell pythonista sqlite tableview
    4
    8
    3519
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Rufat
      Rufat last edited by

      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')
      
      1 Reply Last reply Reply Quote 0
      • Rufat
        Rufat last edited by

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

        mikael 1 Reply Last reply Reply Quote 0
        • mikael
          mikael @Rufat last edited by

          @Rufat, if I understand you correctly, give the tableview a name in the UI designer, then get a reference to it by using the loaded_view['my_tableview_name'] syntax, then set the data source you have above.

          1 Reply Last reply Reply Quote 0
          • Rufat
            Rufat last edited by

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

            mikael 1 Reply Last reply Reply Quote 0
            • mikael
              mikael @Rufat last edited by

              @Rufat, it is a bit more complicated, but certainly possible. See @cvp’s example here.

              1 Reply Last reply Reply Quote 0
              • Rufat
                Rufat last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • TutorialDoctor
                  TutorialDoctor last edited by TutorialDoctor

                  import ui,sqlite3
                  
                  database = "database.db"
                  
                  con = sqlite3.connect(database)
                  cursor = con.cursor()
                  
                  users_query = "select * from users"
                  user_sections ="SELECT * FROM users INNER JOIN post on post.user_id = user.id"
                  
                  user_names = [x[1] for x in cursor.execute(users_query)]
                  
                  v = ui.load_view()
                  table = v['table']
                  table.data_source.items = user_names
                  v.present() 
                  

                  To add accessories:

                  user_names = [{"title":x[1],"accessory_type":"disclosure_indicator"} for x in cursor.execute(users_query)]

                  1 Reply Last reply Reply Quote 0
                  • Mobsteroid
                    Mobsteroid last edited by

                    This post is deleted!
                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post
                    Powered by NodeBB Forums | Contributors