Jump to content
Sign in to follow this  
Centrally

SQL query question

Recommended Posts

Centrally

I'm trying to use autoIT script to populate database but I bumped on one problem.

What I need is query that would check if value in specific field exist (like serial name of product), if it exist than update selected fields or if not insert new value.

This is initial query I have made:

"INSERT INTO `Baza1` (`Datum Objave`,`Serija`,`Ime predmeta`,`Broj pregleda`,`cijena`,`Link do oglasa`,`Koeficijent` ) VALUES (" & $datum1 & "," & $serija & ", " & $ime1 & ", " & $pregledBroj1 & ", " & $cijena1 & ", " & $pathlink & ", " & $koeficient & ")"

This query would only insert new data without checking if it already exist.

I have tried to use replace query but I simply cannot understand it's format.

I don't know how to select what field to check for duplicate and how to update other fields if criteria is matched.

Any explanation or brief part of code example is highly appreciated. Thanks

 

Share this post


Link to post
Share on other sites
Zedna

Instead of

INSERT INTO table (columns) VALUES (values)

use

INSERT INTO table (columns) SELECT values WHERE NOT EXISTS(SELECT 1 FROM table WHERE id = your_id)

Then use your UPDATE

Share this post


Link to post
Share on other sites
MHz

REPLACE is an alias for INSERT for compatibility with other databases so I would not be concerned about using it.

This is how to use UPDATE to change the values.

Here is an example using the SQLite3.exe.

Create a table

CREATE TABLE "tab" VALUES ('col1', 'col2');

Insert values

INSERT INTO "tab" VALUES ('cat', 'dog');

Show output using select

SELECT * FROM "tab";
cat|dog

Update if "col1" = 'pig'. (pig does not exist in col1 so value in col2 is not set)

UPDATE "tab" SET "col2" = 'mouse' WHERE "col1" = 'pig';

Show output using select (no change)

SELECT * FROM "tab";
cat|dog

Update if "col1" = 'cat'

UPDATE "tab" SET "col2" = 'mouse' WHERE "col1" = 'cat';

Show output using select (changed)

SELECT * FROM "tab";
cat|mouse

This is probably a slow method as you are using UPDATE no matter if needs updating or not. You could use SELECT to get the column of interest with all of the values. Find in the values which exist that you have, and then UPDATE the values that are missing which is probably what Zedna is doing with his nested SELECT. Although if you knew the value did not exist in the column when using SELECT, then you could just use INSERT for the new row. :)

UPDATE with comments

UPDATE "tab" -- update this table
SET "col2" = 'dog' -- set value dog in this column
WHERE "col1" = 'cat' --only where cat is in this column
;

Share this post


Link to post
Share on other sites
Centrally

Thank you for your answers I will respond back to this topic when I test your examples.

Share this post


Link to post
Share on other sites
Xenobiologist

Using a "real" database like DB2 then the command Merge is what you are looking for.

(null)


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
jchd

BTW I suspect you use MySQL. While schema names (tables, columns, ...) may be enclosed in backquotes, literal strings should use single quotes and even then, should be escaped (have significant single quotes doubled).

Your question is perhaps more a DB design question.

BTW, there is no such thing as a "real" DB engine, just like for programmers.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Centrally

 

REPLACE is an alias for INSERT for compatibility with other databases so I would not be concerned about using it.

This is how to use UPDATE to change the values.

Here is an example using the SQLite3.exe.

Create a table

CREATE TABLE "tab" VALUES ('col1', 'col2');

Insert values

INSERT INTO "tab" VALUES ('cat', 'dog');

Show output using select

SELECT * FROM "tab";
cat|dog

Update if "col1" = 'pig'. (pig does not exist in col1 so value in col2 is not set)

UPDATE "tab" SET "col2" = 'mouse' WHERE "col1" = 'pig';

Show output using select (no change)

SELECT * FROM "tab";
cat|dog

Update if "col1" = 'cat'

UPDATE "tab" SET "col2" = 'mouse' WHERE "col1" = 'cat';

Show output using select (changed)

SELECT * FROM "tab";
cat|mouse

This is probably a slow method as you are using UPDATE no matter if needs updating or not. You could use SELECT to get the column of interest with all of the values. Find in the values which exist that you have, and then UPDATE the values that are missing which is probably what Zedna is doing with his nested SELECT. Although if you knew the value did not exist in the column when using SELECT, then you could just use INSERT for the new row. :)

UPDATE with comments

UPDATE "tab" -- update this table
SET "col2" = 'dog' -- set value dog in this column
WHERE "col1" = 'cat' --only where cat is in this column
;

 

First off all I am using MySQL and UDF >EzMySql

Now from your set of examples I have learned how to check if specific value in one field exist but I still don't know how to do next thing.

So if I use 

UPDATE "tab" SET "col2" = 'mouse' WHERE "col1" = 'pig';

It does check table for pig and if found is set to mouse, but now if it wasn't found how to add it in a new row. Something like ELSE statement is what I'm looking for 

that would add new row if existing value is not found.

@Zedna

I don't understand this part of query:

WHERE NOT EXISTS(SELECT 1 FROM table WHERE id = your_id)

What this 1 stand for and what id/your_id should be placed here. 

@jchd

I don't care about SQLi yet because this part of db should be manually uploaded to display website with charts few times a day, meaning no actual interaction with website itself.

EDIT:

What do you think about this approach?

INSERT INTO table1
  (`col1`, `col2`, `col3`, `col4`)
VALUES
  ('val1', 'val2', 'val3', 'val4')
ON DUPLICATE KEY UPDATE
  `col2`='val2',
  `col3`='val3', [...]
Edited by Centrally

Share this post


Link to post
Share on other sites
Centrally

UPDATE:

This is my end working query if someone is on same road in future:

"INSERT INTO `baza1` (`Datum Objave`,`Serija`,`Ime predmeta`,`Broj pregleda`,`cijena`,`Link do oglasa`,`Koeficijent`) VALUES (" & $datum1 & "," & $serija & ", " & $ime1 & ", " & $pregledBroj1 & ", " & $cijena1 & ", " & $pathlink & ", " & $koeficient & ") ON DUPLICATE KEY UPDATE  `Broj pregleda`=" & $pregledBroj1 & ",  `cijena`=" & $cijena1 & ", `Koeficijent`=" & $koeficient & ""

I removed primary key and set unique ones to id and `Ime predmeta` which is checked for duplicate and decided upon for either updating or inserting.

I would like to give big thanks for all of you here trying to help me find solution.

Share this post


Link to post
Share on other sites
MHz

The id column mentioned is a custom column that many seem to use to show the row number. In SQLite. this is created regardless and can be accessed as rowid, _rowid_ or oid. I cannot speak for all databases. I may consider they do something similar perhaps.

To access the rowid in SQLite is similar to

SELECT "rowid", * FROM "table";

which would output the rowid and the rest of the columns.
So you do not need to create the custom column id though someone may correct me based on better knowledge. Creating a integer primary key can be an alias for the rowid so the rowid is shown to you as say the custom column id.

@Centrally

If that statement solves your initial problem then you have done well. ;)

I am not MySQL tested nor heavily experienced with databases yet. :)

Edited by MHz

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • Blois
      By Blois
      Hi Guys,
      Fine?
      I have this code and I use it to perform the query, however when I change the query to INSERT it is not working return error.
       
      #include <GUIConstants.au3> #include <MsgBoxConstants.au3> #include <Array.au3> Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") Example() Func Example()     Local $dbname = FileOpenDialog("Choose Access Database", @ScriptDir, "Access files (*.accdb)", 1)     If @error then Return SetError(@error, @extended, 0)     $adoCon = ObjCreate("ADODB.Connection")     $adoCon.Open("Driver={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=" & $dbname & ";Uid=;Pwd=;")     $adoRs = ObjCreate("ADODB.Recordset")     GUICreate("listview items", 550, 250, 100, 200, -1, $WS_EX_ACCEPTFILES)     Local $idListview = GUICtrlCreateListView("Codigo    |Nome         |Valor  ", 10, 10, 520, 150) ;,$LVS_SORTDESCENDING     $queryInsert = INSERT INTO TABLENAME VALUES (''aaaaa'', ''bbbbbb'', ''cccccc'')     Local $aResult     With $adoRs         .CursorType = 2         .LockType = 3         .Open($queryInsert, $adoCon)         If @error Then             ; deal with Probable SQL error             Return SetError(1)         EndIf         If Not .EOF Then $aResult = .GetRows()         .Close()     EndWith     $adoRs = 0     _ArrayDisplay($aResult, 'UBound($aResult)=' & UBound($aResult))     For $iRow_idx = 0 To UBound($aResult) - 1         GUICtrlCreateListViewItem($aResult[$iRow_idx][0], $idListview)     Next     $adoCon.Close     GUISetState()     ; Loop until the user exits.     While 1         Switch GUIGetMsg()             Case $GUI_EVENT_CLOSE                 ExitLoop ;~             Case $idButton ;~                 MsgBox($MB_SYSTEMMODAL, "listview item", GUICtrlRead(GUICtrlRead($idListview)), 2)             Case $idListview                 MsgBox($MB_SYSTEMMODAL, "listview", "clicked=" & GUICtrlGetState($idListview), 2)         EndSwitch     WEnd EndFunc   ;==>Example ; User's COM error function. Will be called if COM error occurs Func _ErrFunc($oError)     ; Do anything here.     ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _             @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _             @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _             @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _             @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _             @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _             @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _             @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _             @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _             @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF) EndFunc   ;==>_ErrFunc Help, Please!
       
    • TheSaint
      By TheSaint
      Only early days at this point, but I have been pondering such a program for a while.
      As good as calibre is (thank you Kovid Goyal), which is a great and wonderful ebook suite of tools and a fair database, it does have its limitations. One of which, is how it deals with multiple libraries, another is the views you get. CalibBrowser will seek to address those.
      What CalibBrowser is not going to be, is an editor for existing calibre libraries. That will be left up to calibre, which is very much needed still, and covers many aspects I will never look at. Unlike calibre, which is quite a complex program, CalibBrowser also seeks to be simple. It is mainly a viewer, at this point, but will later be able to create its own libraries. However, it does not and will not export them to calibre, especially as calibre employs a far different method and structure to what CalibBrowser will employ.
      When CalibBrowser starts, it looks for calibre executables and the main Calibre Library. Whatever isn't found, you get prompted for with a browse option.
      A calibre library, is a set of ebook folders (Author\Ebooks) and a database file, always named metadata.db, and which causes an issue when it comes to multiple libraries, but makes life a bit easier when reconstructing any corrupted libraries. However, there are better ways to deal with that, as my program will show.
      The metadata.db file is an SQL database, so I am having a learning curve right now, as I have only ever dealt with an SQL database previously, codewise, when I created my INItoSQL program some time last year, as an exercise to prove a point.
      At the moment, things are pretty basic, and not everything works 100%. Here is a screenshot, to give an idea of it, but keep in mind, I intend to expand the current GUI for other stuff I will be adding.

      Older Screenshots
      Gawd, I just noticed the '3|7' in the Book Input field (original screenshot). I was using that during troubleshooting for the multiple images scrolling and forgot to disable it ... not that it impacts anything. When it comes to maths, I struggle a bit, especially when tired. Right scrolling was easy, with a continuous loop, was easy to implement. Left scrolling was significantly harder for my poor brain ... until I realized I needed to see them as Min and Max.
      As you can see the program is usable, and all the buttons, aside from the Program Information one, work. You can even load different calibre libraries, and even reload after making changes to one with calibre. The calibre program does not need to be running, even to view an ebook in the Calibre Reader. The combo selector for a library and the ADD button are only temporarily placed where they are, until I expand the GUI.
      My intention at this point, is to add another five thumbnail images, directly below existing. Currently they aren't clickable, but I may add that.
      Here is another screenshot, of what you see when you click the larger Cover image.

      If you want to have a play with the program as is, then you will need to also get the 'sqlite3.dll' file from some online source. When CalibBrowser starts successfully with the selected calibre library, it copies its metadata.db file to a sub-folder of the program called 'Backups'. It also creates a sub-folder in that, based on the library name, to house it. That copied file, is the one the program uses, though it does not even edit that, and file modification is checked every time the program starts with a particular library, or when you Reload or select a library. If the original source file has been modified, then the program copy is overwritten. The Reload Database button does nothing, if there is no change detected, and reports such.
      Place the required 'sqlite3.dll' file in the CalibBrowser root folder. Download includes source files (sqlite3.dll excepted).
      Also required of course, is an install of calibre, plus some ebooks in a created library - Calibre Library is the default when you first add ebooks to calibre. The Mobile Read Forums, is a great source for all things ebook, and calibre can be found there in the E-Book Software section.
      CalibBrowser.zip  - Upload 4  (previous downloads: 1 + 12 + 5)
      My apologies for the program being created in AutoIt v3.3.0.0. It is the first one I have done in a while, with an older AutoIt version. Basically my Win 7 Netbook, which has a current version of AutoIt, was busy and is always busy doing something, and not suitable for doing big projects for several reasons. My older but more powerful Win XP Laptop, has a better programming environment, better computer chair (most important for my knees etc), better external monitor (wider and newish), full size external keyboard & mouse, and a great suite of setup tools to assist me. I run several older versions of AutoIt already on my Laptop, but haven't yet determined what I need to adjust to add a current version of AutoIt to the mix. This also applies to my hugely beneficial Toolbar For Any program (one of those tools), which I constantly use with SciTE. At some point, when finished, I may update the program to the current AutoIt version ... especially as I believe I am now proficient enough to do so, having become quite familiar with it in the last year or so, making many programs with it.
      Enjoy!
       
    • 9252Survive
      By 9252Survive
      Hello guys! 

      I am having some difficulty in achieving a very simple task here. I have gone through the forums and tried some examples and UDFs but I can't seem to work it out. I would really appreciate if someone could help me out. 

      Problem:

      Currently, I am logging the required feedback from the script into a log file in a simple way ... get the info in the var >> write it in the file 

      But now I am in need to perform some analysis and need some of the values to go into an MSSQL table  in the attached format
      Also, I need to be able to use Integrated Security" or "Trusted_Connection set as true or use the logged in windows credentials to connect to the server/database




      any help will be much appreciated!!!  

      Thanks! 
    • dangr82
      By dangr82
       
      I have created this function for a database, but I can not make it work. I always have two error messages: "not an error" ... and the file created, in the script directory, does not contain anything.
      Global $sDBName = "Hen.db" Func DatabaseTable() Local $sConnDB _SQLite_Startup() If @error Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", "SQLite.dll Can't be Loaded!") Exit -1 EndIf $sConnDB = _SQLite_Open($sDBName) If @error Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", "Can't Load Database!") Exit -1 EndIf If Not _SQLite_Exec($sDBName, 'CREATE TABLE Animal ("Name", "Age");') = $SQLITE_OK Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", _SQLite_ErrMsg()) If Not _SQLite_Exec($sDBName, 'INSERT INTO Animale VALUES ("Charlie","5");') = $SQLITE_OK Then MsgBox($MB_SYSTEMMODAL, "SQLite Error", _SQLite_ErrMsg()) _SQLite_Close($sConnDB) _SQLite_Shutdown() EndFunc  
    • argumentum
      By argumentum
      ..let's say I store in a mysql column "[1,2,11,12,123]".
      How can I make s query to get all those rows that is 1, or is 12, etc ?
      I'm using  mysqlnd 5.0.11-dev - 20120503 
      Any advise on what technique I should use is welcomed
      Thanks
      PS: ..I'm adding a comma to each end, as in  [,1,2,11,12,123,] and will search with like %,12,% .
      Not pretty but I'm in a hurry and can't come up with a better solution =/
×