Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Can't Insert data into Access Table from Python - Solved

Can't Insert data into Access Table from Python - Solved

Scheduled Pinned Locked Moved Database
sqlitepythondatabasehelpquestion
14 Posts 4 Posters 0 Views 1 Watching
  • 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.
  • P Offline
    P Offline
    Peter R Fletcher
    wrote on last edited by
    #1

    The following short Python program is supposed to read data from a SQLite datatable, convert some very large integer values in one field to Doubles, and write the data to an appropriately configured Access Table. It runs happily with no error, but no data appears in the Access Table. What am I missing?

    import sqlite3
    import pyodbc

    conn1 = sqlite3.connect('data.db')
    cur1 = conn1.cursor()

    conn2 = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=G:\My_Data_Folder\NewEnergy.accdb;')
    cur2 = conn2.cursor()

    data=cur2.execute('SELECT * FROM RawValues')
    print (cur2.fetchall()) # this correctly displays two dummy records from the Table I am trying to fill

    data=cur1.execute('SELECT * FROM RTPValues ORDER BY TimeSerial ASC')
    rows=cur1.fetchall()
    for row in rows:
    cur2.execute('INSERT INTO RawValues (Millitime, PowerCost) VALUES (?,?)',(float(row[0]),row[1]))
    # If I insert print statements here, they confirm that the right data is being processed
    conn2.commit
    conn2.close
    conn1.close

    [EDIT]The error, as pointed out by Richard MacCutchan at the end of the thread, is that the conn2.commit call (as well as the subsequent .close calls) are not function calls at all, because they are missing the terminal empty parentheses that are required for Python to treat them as such. The last three lines of the code should be:

    conn2.commit()
    conn2.close()
    conn1.close()

    As it stands, the program does what it is supposed to do, but the data is never written to the file.[/EDIT]

    L 1 Reply Last reply
    0
    • P Peter R Fletcher

      The following short Python program is supposed to read data from a SQLite datatable, convert some very large integer values in one field to Doubles, and write the data to an appropriately configured Access Table. It runs happily with no error, but no data appears in the Access Table. What am I missing?

      import sqlite3
      import pyodbc

      conn1 = sqlite3.connect('data.db')
      cur1 = conn1.cursor()

      conn2 = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=G:\My_Data_Folder\NewEnergy.accdb;')
      cur2 = conn2.cursor()

      data=cur2.execute('SELECT * FROM RawValues')
      print (cur2.fetchall()) # this correctly displays two dummy records from the Table I am trying to fill

      data=cur1.execute('SELECT * FROM RTPValues ORDER BY TimeSerial ASC')
      rows=cur1.fetchall()
      for row in rows:
      cur2.execute('INSERT INTO RawValues (Millitime, PowerCost) VALUES (?,?)',(float(row[0]),row[1]))
      # If I insert print statements here, they confirm that the right data is being processed
      conn2.commit
      conn2.close
      conn1.close

      [EDIT]The error, as pointed out by Richard MacCutchan at the end of the thread, is that the conn2.commit call (as well as the subsequent .close calls) are not function calls at all, because they are missing the terminal empty parentheses that are required for Python to treat them as such. The last three lines of the code should be:

      conn2.commit()
      conn2.close()
      conn1.close()

      As it stands, the program does what it is supposed to do, but the data is never written to the file.[/EDIT]

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The float built-in only takes a single parameter, you are passing two, so that code will fail. Also you have specified two values in your insert statement but you are trying to pass only one.Victor pointed out my mistake. [edit] Having looked at the documentation, I guess that the extra parentheses round your values may be the problem. The example is:

      cursor.execute("insert into products(id, name) values (?, ?)", 'pyodbc', 'awesome library')

      So perhaps your code should be:

      cur2.execute('INSERT INTO RawValues (Millitime, PowerCost) VALUES (?,?)', float(row[0]), row[1])

      [/edit]

      V P 2 Replies Last reply
      0
      • L Lost User

        The float built-in only takes a single parameter, you are passing two, so that code will fail. Also you have specified two values in your insert statement but you are trying to pass only one.Victor pointed out my mistake. [edit] Having looked at the documentation, I guess that the extra parentheses round your values may be the problem. The example is:

        cursor.execute("insert into products(id, name) values (?, ?)", 'pyodbc', 'awesome library')

        So perhaps your code should be:

        cur2.execute('INSERT INTO RawValues (Millitime, PowerCost) VALUES (?,?)', float(row[0]), row[1])

        [/edit]

        V Offline
        V Offline
        Victor Nijegorodov
        wrote on last edited by
        #3

        Richard MacCutchan wrote:

        The float built-in only takes a single parameter, you are passing two, so that code will fail. Also you have specified two values in your insert statement but you are trying to pass only one.

        Hmmm... It seems to me there are two values here:

        Quote:

        cur2.execute('INSERT INTO RawValues (Millitime, PowerCost) VALUES (?,?)',(float(row[0]),row[1]))

        The first is

        Quote:

        float(row[0])

        , the second -

        Quote:

        row[1]

        L 1 Reply Last reply
        0
        • V Victor Nijegorodov

          Richard MacCutchan wrote:

          The float built-in only takes a single parameter, you are passing two, so that code will fail. Also you have specified two values in your insert statement but you are trying to pass only one.

          Hmmm... It seems to me there are two values here:

          Quote:

          cur2.execute('INSERT INTO RawValues (Millitime, PowerCost) VALUES (?,?)',(float(row[0]),row[1]))

          The first is

          Quote:

          float(row[0])

          , the second -

          Quote:

          row[1]

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Thanks, my eyesight missed that.

          1 Reply Last reply
          0
          • L Lost User

            The float built-in only takes a single parameter, you are passing two, so that code will fail. Also you have specified two values in your insert statement but you are trying to pass only one.Victor pointed out my mistake. [edit] Having looked at the documentation, I guess that the extra parentheses round your values may be the problem. The example is:

            cursor.execute("insert into products(id, name) values (?, ?)", 'pyodbc', 'awesome library')

            So perhaps your code should be:

            cur2.execute('INSERT INTO RawValues (Millitime, PowerCost) VALUES (?,?)', float(row[0]), row[1])

            [/edit]

            P Offline
            P Offline
            Peter R Fletcher
            wrote on last edited by
            #5

            Richard MacCutchan wrote:

            Having looked at the documentation, I guess that the extra parentheses round your values may be the problem. The example is:

            cursor.execute("insert into products(id, name) values (?, ?)", 'pyodbc', 'awesome library')

            So perhaps your code should be:

            cur2.execute('INSERT INTO RawValues (Millitime, PowerCost) VALUES (?,?)', float(row[0]), row[1])

            No, the 'extra' parentheses turn the two values to be passed into a single (2-element) Python Tuple, which is what the call wants passed. If this were the problem, Python should be throwing an error. I have written a fair bit of other code, adding data to SQLite tables, which is exactly parallel and works as it should.

            L 1 Reply Last reply
            0
            • P Peter R Fletcher

              Richard MacCutchan wrote:

              Having looked at the documentation, I guess that the extra parentheses round your values may be the problem. The example is:

              cursor.execute("insert into products(id, name) values (?, ?)", 'pyodbc', 'awesome library')

              So perhaps your code should be:

              cur2.execute('INSERT INTO RawValues (Millitime, PowerCost) VALUES (?,?)', float(row[0]), row[1])

              No, the 'extra' parentheses turn the two values to be passed into a single (2-element) Python Tuple, which is what the call wants passed. If this were the problem, Python should be throwing an error. I have written a fair bit of other code, adding data to SQLite tables, which is exactly parallel and works as it should.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              You will have to do some debugging as it is impossible to guess what is happening on your system. Does the execute command return a value, such as the number of rows inserted?

              P 1 Reply Last reply
              0
              • L Lost User

                You will have to do some debugging as it is impossible to guess what is happening on your system. Does the execute command return a value, such as the number of rows inserted?

                P Offline
                P Offline
                Peter R Fletcher
                wrote on last edited by
                #7

                It returns a cursor. Fetching the contents of that gets the following:

                pyodbc.ProgrammingError: No results. Previous SQL was not a query.

                That does make a certain amount of sense, given that nothing is happening :), but doesn't really help, particularly since the syntax I am using appears valid and works with Sqlite. I was hoping that someone else had run into and solved this specific problem, since I am out of ideas. [EDIT] Further research suggests that this 'error' is even less helpful, and may well be the normal result when executing something that is not a SELECT query. [/EDIT]

                L 2 Replies Last reply
                0
                • P Peter R Fletcher

                  It returns a cursor. Fetching the contents of that gets the following:

                  pyodbc.ProgrammingError: No results. Previous SQL was not a query.

                  That does make a certain amount of sense, given that nothing is happening :), but doesn't really help, particularly since the syntax I am using appears valid and works with Sqlite. I was hoping that someone else had run into and solved this specific problem, since I am out of ideas. [EDIT] Further research suggests that this 'error' is even less helpful, and may well be the normal result when executing something that is not a SELECT query. [/EDIT]

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Sorry, I am out of ideas, and the documentation is not a lot of help. Although a number of similar questions suggest that it should return a row count.

                  1 Reply Last reply
                  0
                  • P Peter R Fletcher

                    It returns a cursor. Fetching the contents of that gets the following:

                    pyodbc.ProgrammingError: No results. Previous SQL was not a query.

                    That does make a certain amount of sense, given that nothing is happening :), but doesn't really help, particularly since the syntax I am using appears valid and works with Sqlite. I was hoping that someone else had run into and solved this specific problem, since I am out of ideas. [EDIT] Further research suggests that this 'error' is even less helpful, and may well be the normal result when executing something that is not a SELECT query. [/EDIT]

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    I have been playing with pyodbc and have successfully added records to my pre-built access database. Unfortunately that does not give any clues as to why your code fails. [edit] Created a RawValues table and successfully added a record with 2 values. [/edit]

                    P 1 Reply Last reply
                    0
                    • L Lost User

                      I have been playing with pyodbc and have successfully added records to my pre-built access database. Unfortunately that does not give any clues as to why your code fails. [edit] Created a RawValues table and successfully added a record with 2 values. [/edit]

                      P Offline
                      P Offline
                      Peter R Fletcher
                      wrote on last edited by
                      #10

                      Can you post your working Python code, and what version/'bitness' of Access are you running?

                      L 1 Reply Last reply
                      0
                      • P Peter R Fletcher

                        Can you post your working Python code, and what version/'bitness' of Access are you running?

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #11

                        No need, I just discovered I am blind (and maybe you are). The following calls are missing the parentheses that make them into function calls:

                        conn2.commit
                        conn2.close
                        conn1.close

                        should be:

                        conn2.commit()
                        conn2.close()
                        conn1.close()

                        P M 2 Replies Last reply
                        0
                        • L Lost User

                          No need, I just discovered I am blind (and maybe you are). The following calls are missing the parentheses that make them into function calls:

                          conn2.commit
                          conn2.close
                          conn1.close

                          should be:

                          conn2.commit()
                          conn2.close()
                          conn1.close()

                          P Offline
                          P Offline
                          Peter R Fletcher
                          wrote on last edited by
                          #12

                          Of course! Thanks!!

                          1 Reply Last reply
                          0
                          • L Lost User

                            No need, I just discovered I am blind (and maybe you are). The following calls are missing the parentheses that make them into function calls:

                            conn2.commit
                            conn2.close
                            conn1.close

                            should be:

                            conn2.commit()
                            conn2.close()
                            conn1.close()

                            M Offline
                            M Offline
                            Mycroft Holmes
                            wrote on last edited by
                            #13

                            :laugh: I see it happens to the best of us.

                            Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

                            L 1 Reply Last reply
                            0
                            • M Mycroft Holmes

                              :laugh: I see it happens to the best of us.

                              Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              It happens to me embarrassingly too often. :-O

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups