BottlePy, SQLite3 - Storage of request form to database - KeyError 0










0















I am trying to get user inputs from a HTML form, and store them into a SQlite3 database.



HTML form has 1 radio button set (with 4 choices), and 7 text inputs. It looks like this (without all the formatting) :



<input type="radio" name="ProducteurType" value="Prod" checked="checked">
<input type="radio" name="ProducteurType" value="Coll">
<input type="radio" name="ProducteurType" value="Traitement">
<input type="radio" name="ProducteurType" value="Autre">

<input type="text" name="ProducteurSIRET">
<input type="text" name="ProducteurNOM">
<input type="text" name="ProducteurAdresse">
<input type="text" name="ProducteurTel">
<input type="text" name="ProducteurFax">
<input type="text" name="ProducteurCourriel">
<input type="text" name="ProducteurContact">


I use the following code to store these inputs into a database :



@post('/ajoutClient')
def ajoutClient():
conn = sqlite3.connect('db/bsd.db')
create_if_need = """CREATE TABLE IF NOT EXISTS Clients (id INTEGER PRIMARY KEY AUTOINCREMENT,
ProducteurType TEXT,
ProducteurSIRET TEXT,
ProducteurNOM TEXT,
ProducteurAdresse TEXT,
ProducteurTel TEXT,
ProducteurFax TEXT,
ProducteurCourriel TEXT,
ProducteurContact TEXT)"""
conn.execute(create_if_need)
rf = request.forms
insert_client_query = """
INSERT INTO
Clients(ProducteurType,ProducteurSIRET,ProducteurNOM,ProducteurAdresse,ProducteurTel,ProducteurFax,ProducteurCourriel,ProducteurContact)
VALUES (?,?,?,?,?,?,?,?)"""
conn.execute(insert_client_query,rf)
conn.commit


I get the following error :




 File "/usr/lib/python3/dist-packages/bottle.py", line 1826, in __getitem__
def __getitem__(self, key): return self.dict[key][-1]
KeyError: 0



I understand that "KeyError 0" means that, somehow, I am giving too much or not enough fields to the database.



I have filled all fields in the form, so there is no empty value.
I have checked the number of fields : there are 8 of them, in the form and in the database.
I have checked the field names : they are identical.



Did I do wrong with the "id" primary key ?
Is HTML "radio" input not compatible with database insertion, or should I store it as 4 booleans (one would be true, the three others beeing false) ?










share|improve this question















migrated from serverfault.com Nov 15 '18 at 16:22


This question came from our site for system and network administrators.






















    0















    I am trying to get user inputs from a HTML form, and store them into a SQlite3 database.



    HTML form has 1 radio button set (with 4 choices), and 7 text inputs. It looks like this (without all the formatting) :



    <input type="radio" name="ProducteurType" value="Prod" checked="checked">
    <input type="radio" name="ProducteurType" value="Coll">
    <input type="radio" name="ProducteurType" value="Traitement">
    <input type="radio" name="ProducteurType" value="Autre">

    <input type="text" name="ProducteurSIRET">
    <input type="text" name="ProducteurNOM">
    <input type="text" name="ProducteurAdresse">
    <input type="text" name="ProducteurTel">
    <input type="text" name="ProducteurFax">
    <input type="text" name="ProducteurCourriel">
    <input type="text" name="ProducteurContact">


    I use the following code to store these inputs into a database :



    @post('/ajoutClient')
    def ajoutClient():
    conn = sqlite3.connect('db/bsd.db')
    create_if_need = """CREATE TABLE IF NOT EXISTS Clients (id INTEGER PRIMARY KEY AUTOINCREMENT,
    ProducteurType TEXT,
    ProducteurSIRET TEXT,
    ProducteurNOM TEXT,
    ProducteurAdresse TEXT,
    ProducteurTel TEXT,
    ProducteurFax TEXT,
    ProducteurCourriel TEXT,
    ProducteurContact TEXT)"""
    conn.execute(create_if_need)
    rf = request.forms
    insert_client_query = """
    INSERT INTO
    Clients(ProducteurType,ProducteurSIRET,ProducteurNOM,ProducteurAdresse,ProducteurTel,ProducteurFax,ProducteurCourriel,ProducteurContact)
    VALUES (?,?,?,?,?,?,?,?)"""
    conn.execute(insert_client_query,rf)
    conn.commit


    I get the following error :




     File "/usr/lib/python3/dist-packages/bottle.py", line 1826, in __getitem__
    def __getitem__(self, key): return self.dict[key][-1]
    KeyError: 0



    I understand that "KeyError 0" means that, somehow, I am giving too much or not enough fields to the database.



    I have filled all fields in the form, so there is no empty value.
    I have checked the number of fields : there are 8 of them, in the form and in the database.
    I have checked the field names : they are identical.



    Did I do wrong with the "id" primary key ?
    Is HTML "radio" input not compatible with database insertion, or should I store it as 4 booleans (one would be true, the three others beeing false) ?










    share|improve this question















    migrated from serverfault.com Nov 15 '18 at 16:22


    This question came from our site for system and network administrators.




















      0












      0








      0








      I am trying to get user inputs from a HTML form, and store them into a SQlite3 database.



      HTML form has 1 radio button set (with 4 choices), and 7 text inputs. It looks like this (without all the formatting) :



      <input type="radio" name="ProducteurType" value="Prod" checked="checked">
      <input type="radio" name="ProducteurType" value="Coll">
      <input type="radio" name="ProducteurType" value="Traitement">
      <input type="radio" name="ProducteurType" value="Autre">

      <input type="text" name="ProducteurSIRET">
      <input type="text" name="ProducteurNOM">
      <input type="text" name="ProducteurAdresse">
      <input type="text" name="ProducteurTel">
      <input type="text" name="ProducteurFax">
      <input type="text" name="ProducteurCourriel">
      <input type="text" name="ProducteurContact">


      I use the following code to store these inputs into a database :



      @post('/ajoutClient')
      def ajoutClient():
      conn = sqlite3.connect('db/bsd.db')
      create_if_need = """CREATE TABLE IF NOT EXISTS Clients (id INTEGER PRIMARY KEY AUTOINCREMENT,
      ProducteurType TEXT,
      ProducteurSIRET TEXT,
      ProducteurNOM TEXT,
      ProducteurAdresse TEXT,
      ProducteurTel TEXT,
      ProducteurFax TEXT,
      ProducteurCourriel TEXT,
      ProducteurContact TEXT)"""
      conn.execute(create_if_need)
      rf = request.forms
      insert_client_query = """
      INSERT INTO
      Clients(ProducteurType,ProducteurSIRET,ProducteurNOM,ProducteurAdresse,ProducteurTel,ProducteurFax,ProducteurCourriel,ProducteurContact)
      VALUES (?,?,?,?,?,?,?,?)"""
      conn.execute(insert_client_query,rf)
      conn.commit


      I get the following error :




       File "/usr/lib/python3/dist-packages/bottle.py", line 1826, in __getitem__
      def __getitem__(self, key): return self.dict[key][-1]
      KeyError: 0



      I understand that "KeyError 0" means that, somehow, I am giving too much or not enough fields to the database.



      I have filled all fields in the form, so there is no empty value.
      I have checked the number of fields : there are 8 of them, in the form and in the database.
      I have checked the field names : they are identical.



      Did I do wrong with the "id" primary key ?
      Is HTML "radio" input not compatible with database insertion, or should I store it as 4 booleans (one would be true, the three others beeing false) ?










      share|improve this question
















      I am trying to get user inputs from a HTML form, and store them into a SQlite3 database.



      HTML form has 1 radio button set (with 4 choices), and 7 text inputs. It looks like this (without all the formatting) :



      <input type="radio" name="ProducteurType" value="Prod" checked="checked">
      <input type="radio" name="ProducteurType" value="Coll">
      <input type="radio" name="ProducteurType" value="Traitement">
      <input type="radio" name="ProducteurType" value="Autre">

      <input type="text" name="ProducteurSIRET">
      <input type="text" name="ProducteurNOM">
      <input type="text" name="ProducteurAdresse">
      <input type="text" name="ProducteurTel">
      <input type="text" name="ProducteurFax">
      <input type="text" name="ProducteurCourriel">
      <input type="text" name="ProducteurContact">


      I use the following code to store these inputs into a database :



      @post('/ajoutClient')
      def ajoutClient():
      conn = sqlite3.connect('db/bsd.db')
      create_if_need = """CREATE TABLE IF NOT EXISTS Clients (id INTEGER PRIMARY KEY AUTOINCREMENT,
      ProducteurType TEXT,
      ProducteurSIRET TEXT,
      ProducteurNOM TEXT,
      ProducteurAdresse TEXT,
      ProducteurTel TEXT,
      ProducteurFax TEXT,
      ProducteurCourriel TEXT,
      ProducteurContact TEXT)"""
      conn.execute(create_if_need)
      rf = request.forms
      insert_client_query = """
      INSERT INTO
      Clients(ProducteurType,ProducteurSIRET,ProducteurNOM,ProducteurAdresse,ProducteurTel,ProducteurFax,ProducteurCourriel,ProducteurContact)
      VALUES (?,?,?,?,?,?,?,?)"""
      conn.execute(insert_client_query,rf)
      conn.commit


      I get the following error :




       File "/usr/lib/python3/dist-packages/bottle.py", line 1826, in __getitem__
      def __getitem__(self, key): return self.dict[key][-1]
      KeyError: 0



      I understand that "KeyError 0" means that, somehow, I am giving too much or not enough fields to the database.



      I have filled all fields in the form, so there is no empty value.
      I have checked the number of fields : there are 8 of them, in the form and in the database.
      I have checked the field names : they are identical.



      Did I do wrong with the "id" primary key ?
      Is HTML "radio" input not compatible with database insertion, or should I store it as 4 booleans (one would be true, the three others beeing false) ?







      python sqlite






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 17:34









      Ali AzG

      7111717




      7111717










      asked Nov 15 '18 at 16:02









      BOUHL R.BOUHL R.

      133




      133




      migrated from serverfault.com Nov 15 '18 at 16:22


      This question came from our site for system and network administrators.









      migrated from serverfault.com Nov 15 '18 at 16:22


      This question came from our site for system and network administrators.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Ok I got it. I badly used the request.forms dictionnary. This code works :



          rf = request.forms
          conn.execute('INSERT INTO Clients values (NULL,?,?,?,?,?,?,?,?)', [
          rf['ProducteurType'],
          rf['ProducteurSIRET'],
          rf['ProducteurNOM'],
          rf['ProducteurAdresse'],
          rf['ProducteurTel'],
          rf['ProducteurFax'],
          rf['ProducteurCourriel'],
          rf['ProducteurContact'] ])
          conn.commit





          share|improve this answer






















            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%2f53323753%2fbottlepy-sqlite3-storage-of-request-form-to-database-keyerror-0%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Ok I got it. I badly used the request.forms dictionnary. This code works :



            rf = request.forms
            conn.execute('INSERT INTO Clients values (NULL,?,?,?,?,?,?,?,?)', [
            rf['ProducteurType'],
            rf['ProducteurSIRET'],
            rf['ProducteurNOM'],
            rf['ProducteurAdresse'],
            rf['ProducteurTel'],
            rf['ProducteurFax'],
            rf['ProducteurCourriel'],
            rf['ProducteurContact'] ])
            conn.commit





            share|improve this answer



























              0














              Ok I got it. I badly used the request.forms dictionnary. This code works :



              rf = request.forms
              conn.execute('INSERT INTO Clients values (NULL,?,?,?,?,?,?,?,?)', [
              rf['ProducteurType'],
              rf['ProducteurSIRET'],
              rf['ProducteurNOM'],
              rf['ProducteurAdresse'],
              rf['ProducteurTel'],
              rf['ProducteurFax'],
              rf['ProducteurCourriel'],
              rf['ProducteurContact'] ])
              conn.commit





              share|improve this answer

























                0












                0








                0







                Ok I got it. I badly used the request.forms dictionnary. This code works :



                rf = request.forms
                conn.execute('INSERT INTO Clients values (NULL,?,?,?,?,?,?,?,?)', [
                rf['ProducteurType'],
                rf['ProducteurSIRET'],
                rf['ProducteurNOM'],
                rf['ProducteurAdresse'],
                rf['ProducteurTel'],
                rf['ProducteurFax'],
                rf['ProducteurCourriel'],
                rf['ProducteurContact'] ])
                conn.commit





                share|improve this answer













                Ok I got it. I badly used the request.forms dictionnary. This code works :



                rf = request.forms
                conn.execute('INSERT INTO Clients values (NULL,?,?,?,?,?,?,?,?)', [
                rf['ProducteurType'],
                rf['ProducteurSIRET'],
                rf['ProducteurNOM'],
                rf['ProducteurAdresse'],
                rf['ProducteurTel'],
                rf['ProducteurFax'],
                rf['ProducteurCourriel'],
                rf['ProducteurContact'] ])
                conn.commit






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 18:17









                BOUHL R.BOUHL R.

                133




                133





























                    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%2f53323753%2fbottlepy-sqlite3-storage-of-request-form-to-database-keyerror-0%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







                    這個網誌中的熱門文章

                    What does pagestruct do in Eviews?

                    Dutch intervention in Lombok and Karangasem

                    Channel Islands