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. General Programming
  3. C#
  4. What is the syntax error in this SQLite query?

What is the syntax error in this SQLite query?

Scheduled Pinned Locked Moved C#
questionhelpdatabasesqlite
14 Posts 7 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.
  • OriginalGriffO OriginalGriff

    First off, don't INSERT like that - always list the columns into which you want to ISNERT values - if someone (very sensibly) adds an ID column for example, it can really mess up your code, even if the ID column is of IDENTITY type so you can't INSERT to it! Listing the columns in the order you supply their values makes your code safer and easier to maintain in future. Secondly, you can't add a WHERE clause to an INSERT operation because it doesn't affect any existing rows - it only ever adds new ones. If you want to do an insert if no matching rows exist use IF[^] or CASE[^] instead. I'd probably use COUNT(...) = 0 instead of EXISTS(...) as well.

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

    A Offline
    A Offline
    Alex Dunlop
    wrote on last edited by
    #3

    Do you mean this: INSERT INTO FileInfo (EqpCode, EqpName, FileName) VALUES ('123', 'rty', 'iuo') WHERE NOT EXISTS (SELECT 1 FROM FileInfo IF EqpCode='123') It has the same Error.

    D 1 Reply Last reply
    0
    • OriginalGriffO OriginalGriff

      First off, don't INSERT like that - always list the columns into which you want to ISNERT values - if someone (very sensibly) adds an ID column for example, it can really mess up your code, even if the ID column is of IDENTITY type so you can't INSERT to it! Listing the columns in the order you supply their values makes your code safer and easier to maintain in future. Secondly, you can't add a WHERE clause to an INSERT operation because it doesn't affect any existing rows - it only ever adds new ones. If you want to do an insert if no matching rows exist use IF[^] or CASE[^] instead. I'd probably use COUNT(...) = 0 instead of EXISTS(...) as well.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      A Offline
      A Offline
      Alex Dunlop
      wrote on last edited by
      #4

      I had used a similar query in C# and it was working well (SQL server CE):

      mycommand.CommandText = $"INSERT INTO MyData([Wo], [EqN], [Code], [Work], [Cost]) SELECT '{DataGridView3.Rows[i].Cells[0].Value}', '{DataGridView3.Rows[i].Cells[1].Value}', '{DataGridView3.Rows[i].Cells[2].Value}', '{DataGridView3.Rows[i].Cells[3].Value}', '{DataGridView3.Rows[i].Cells[4].Value}' WHERE NOT EXISTS (SELECT 1 FROM MyData WHERE [Wo]='{DataGridView3.Rows[i].Cells[0].Value}' AND [Code]='{DataGridView3.Rows[i].Cells[2].Value}')";

      L 1 Reply Last reply
      0
      • A Alex Dunlop

        Do you mean this: INSERT INTO FileInfo (EqpCode, EqpName, FileName) VALUES ('123', 'rty', 'iuo') WHERE NOT EXISTS (SELECT 1 FROM FileInfo IF EqpCode='123') It has the same Error.

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #5

        Again, there is no such thing as a WHERE clause on an INSERT statement. Remove everything from WHERE to the end of the statement.

        Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
        Dave Kreskowiak

        1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          First off, don't INSERT like that - always list the columns into which you want to ISNERT values - if someone (very sensibly) adds an ID column for example, it can really mess up your code, even if the ID column is of IDENTITY type so you can't INSERT to it! Listing the columns in the order you supply their values makes your code safer and easier to maintain in future. Secondly, you can't add a WHERE clause to an INSERT operation because it doesn't affect any existing rows - it only ever adds new ones. If you want to do an insert if no matching rows exist use IF[^] or CASE[^] instead. I'd probably use COUNT(...) = 0 instead of EXISTS(...) as well.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

          A Offline
          A Offline
          Alex Dunlop
          wrote on last edited by
          #6

          I found an easy way. I changed EqpCode column type to UNIQUE and Unique Conflict Clause to REPLACE. :) Then:

          INSERT INTO FileInfo(EqpCode, EqpName, FileName) VALUES ('123', 'test01', 'test02')
          ON CONFLICT(EqpCode) DO UPDATE
          SET EqpName = EXCLUDED.EqpName,
          FileName = EXCLUDED.FileName;

          1 Reply Last reply
          0
          • A Alex Dunlop

            I had used a similar query in C# and it was working well (SQL server CE):

            mycommand.CommandText = $"INSERT INTO MyData([Wo], [EqN], [Code], [Work], [Cost]) SELECT '{DataGridView3.Rows[i].Cells[0].Value}', '{DataGridView3.Rows[i].Cells[1].Value}', '{DataGridView3.Rows[i].Cells[2].Value}', '{DataGridView3.Rows[i].Cells[3].Value}', '{DataGridView3.Rows[i].Cells[4].Value}' WHERE NOT EXISTS (SELECT 1 FROM MyData WHERE [Wo]='{DataGridView3.Rows[i].Cells[0].Value}' AND [Code]='{DataGridView3.Rows[i].Cells[2].Value}')";

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

            Yes, but the WHERE clause is connected to the SELECT, not the INSERT.

            1 Reply Last reply
            0
            • A Alex Dunlop

              query is:

              INSERT INTO FileInfo VALUES ('2021', 'ALex', 'Dunlop') WHERE NOT EXISTS (SELECT 1 FROM FileInfo WHERE EqpCode='2021')

              The table has 3 columns. SQLite says: syntax error near WHERE How can I fix it?

              S Offline
              S Offline
              SeeSharp2
              wrote on last edited by
              #8

              Some have given you the answer but not really shown you what they mean. You can't use WHERE without a SELECT. So, you do something like this:

              INSERT INTO Table1(Field1, Field2, ...)
              SELECT '2021', 'Alex', ...
              WHERE NOT EXISTS (...)

              OriginalGriffO 1 Reply Last reply
              0
              • S SeeSharp2

                Some have given you the answer but not really shown you what they mean. You can't use WHERE without a SELECT. So, you do something like this:

                INSERT INTO Table1(Field1, Field2, ...)
                SELECT '2021', 'Alex', ...
                WHERE NOT EXISTS (...)

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #9

                DevParty wrote:

                You can't use WHERE without a SELECT.

                Um ... you sure?

                UPDATE MyTable SET MyColumn = @MyValue WHERE MyOtherColumn = @MyOtherValue

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                V S 2 Replies Last reply
                0
                • OriginalGriffO OriginalGriff

                  DevParty wrote:

                  You can't use WHERE without a SELECT.

                  Um ... you sure?

                  UPDATE MyTable SET MyColumn = @MyValue WHERE MyOtherColumn = @MyOtherValue

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

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

                  OriginalGriff wrote:

                  DevParty wrote:

                  You can't use WHERE without a SELECT.

                  Um ... you sure?

                  SQL

                  Copy Code

                  UPDATE MyTable SET MyColumn = @MyValue WHERE MyOtherColumn = @MyOtherValue

                  I'm pretty sure he meant an INSERT, clause, not an UPDATE one! :omg:

                  OriginalGriffO 1 Reply Last reply
                  0
                  • V Victor Nijegorodov

                    OriginalGriff wrote:

                    DevParty wrote:

                    You can't use WHERE without a SELECT.

                    Um ... you sure?

                    SQL

                    Copy Code

                    UPDATE MyTable SET MyColumn = @MyValue WHERE MyOtherColumn = @MyOtherValue

                    I'm pretty sure he meant an INSERT, clause, not an UPDATE one! :omg:

                    OriginalGriffO Offline
                    OriginalGriffO Offline
                    OriginalGriff
                    wrote on last edited by
                    #11

                    What he may have meant and what he said are not necessarily the same thing! :laugh: Particularly with beginners, you have to be accurate - a bald statement like "WHERE only works with SELECT" is not accurate and can confuse.

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                    Richard Andrew x64R 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      What he may have meant and what he said are not necessarily the same thing! :laugh: Particularly with beginners, you have to be accurate - a bald statement like "WHERE only works with SELECT" is not accurate and can confuse.

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                      Richard Andrew x64R Offline
                      Richard Andrew x64R Offline
                      Richard Andrew x64
                      wrote on last edited by
                      #12

                      OriginalGriff wrote:

                      a bald statement like "WHERE only works with SELECT" is not accurate and can confuse.

                      Would that be a statement without any hair? :-D

                      The difficult we do right away... ...the impossible takes slightly longer.

                      OriginalGriffO 1 Reply Last reply
                      0
                      • Richard Andrew x64R Richard Andrew x64

                        OriginalGriff wrote:

                        a bald statement like "WHERE only works with SELECT" is not accurate and can confuse.

                        Would that be a statement without any hair? :-D

                        The difficult we do right away... ...the impossible takes slightly longer.

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #13

                        Wouldn't know - I still have a ponytail. :-D

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        1 Reply Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          DevParty wrote:

                          You can't use WHERE without a SELECT.

                          Um ... you sure?

                          UPDATE MyTable SET MyColumn = @MyValue WHERE MyOtherColumn = @MyOtherValue

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                          S Offline
                          S Offline
                          SeeSharp2
                          wrote on last edited by
                          #14

                          For inserting data. Stay in context. :doh:

                          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