[RISOLTO]Macro per eseguire comando sql

Discussioni sulle caratteristiche di database
Rispondi
Gaetanopr
Volontario
Volontario
Messaggi: 3300
Iscritto il: mercoledì 21 novembre 2012, 20:07

[RISOLTO]Macro per eseguire comando sql

Messaggio da Gaetanopr »

Salve vi chiedo se è possibile eseguire un comando sql tramite macro? vorrei associarla ad un pulsante presente nel formulario Clienti del database di prova allegato.
Il comando sql aggiorna la tipologia cliente presente nella tabella cliente da standard a premium al superamento di una certa soglia di spesa.
Il comando sql è il seguente

Codice: Seleziona tutto

UPDATE "CLIENTI"
     SET "TipologiaCliente" = 'Premium'
  WHERE "IDCliente" IN (SELECT "IDCliente" FROM "MOVIMENTI" GROUP BY "IDCliente" HAVING  SUM( "ImportoPagato" )  >= 2000)
Avevo pure allegato una macro ma l'ho tolta ricontrollandola mi sono accorto che non faceva al caso in questione
scusate


Grazie
Allegati
dbupdate (1).odb
(14.3 KiB) Scaricato 189 volte
Ultima modifica di Gaetanopr il martedì 22 gennaio 2013, 19:28, modificato 3 volte in totale.
LibreOffice 7.2.2.2 windows 10
Openoffice 4.1.13 su windows 10
Gaetanopr
Volontario
Volontario
Messaggi: 3300
Iscritto il: mercoledì 21 novembre 2012, 20:07

Re: Macro per eseguire comando sql

Messaggio da Gaetanopr »

Salve ricontrollando la macro che avevo allegato mi sono reso conto che non era pertinente al quesito quindi ho modificato il messaggio iniziale eliminando la macro anche dal database che avevo allegato
Scusate per l'errore e a chi magari aveva scaricato il db non la tenga in considerazione.

Grazie
LibreOffice 7.2.2.2 windows 10
Openoffice 4.1.13 su windows 10
robce64
Messaggi: 177
Iscritto il: giovedì 12 gennaio 2012, 20:05

Re: Macro per eseguire comando sql

Messaggio da robce64 »

Questo quesito è nteressante anche per una applicazione che stò progettando
LibreOffice 4.1.4.2 su Windows 8.1 64bit
robce64
Messaggi: 177
Iscritto il: giovedì 12 gennaio 2012, 20:05

Re: Macro per eseguire comando sql

Messaggio da robce64 »

prova a guardare questo file esempio, qui vengono utilizzate delle macro con dentro codice sql per eseguire dei filtri, ma essendoci quindi la possibilità di inserire comandi sql nel codice macro magari modificandola un pò si riesce anche a fare quello che vuoi tu.
Fai sapere se ci riesci.
Allegati
EXPDB_macro_filtering.odb
(21.89 KiB) Scaricato 457 volte
LibreOffice 4.1.4.2 su Windows 8.1 64bit
Gaetanopr
Volontario
Volontario
Messaggi: 3300
Iscritto il: mercoledì 21 novembre 2012, 20:07

Re: Macro per eseguire comando sql

Messaggio da Gaetanopr »

Grazie robce64 per la risposta
lo prendero' in considerazione
LibreOffice 7.2.2.2 windows 10
Openoffice 4.1.13 su windows 10
Avatar utente
Mizio1961
Volontario
Volontario
Messaggi: 841
Iscritto il: mercoledì 5 ottobre 2011, 22:55
Località: Roma

Re: Macro per eseguire comando sql

Messaggio da Mizio1961 »

Ciao Gaetano
Ho messo la stessa risposta in macro
Di seguito il link a un mio argomento in estensioni dove trovi il progetto che sto traslando in OO che si basa proprio su questo collegamento SQL
http://forum.openoffice.org/it/forum/vi ... 264#p17264
E' un pò complesso ma se ci perdi un pò di tempo trovi una buona dose di risposte un pò su tutto
Saluti
OpenOffice Windows - Libre su Ubuntu Desktop e Notebook WiFi
Per cortesia, inserisci [Risolto] nel Titolo iniziale se il tuo problema è stato risolto. ;-)
Gaetanopr
Volontario
Volontario
Messaggi: 3300
Iscritto il: mercoledì 21 novembre 2012, 20:07

Re: Macro per eseguire comando sql

Messaggio da Gaetanopr »

Ciao Maurizio
Inizierò a studiarci sopra
Grazie ancora
LibreOffice 7.2.2.2 windows 10
Openoffice 4.1.13 su windows 10
robce64
Messaggi: 177
Iscritto il: giovedì 12 gennaio 2012, 20:05

Re: Macro per eseguire comando sql

Messaggio da robce64 »

Ciao, dato che ho anche io bisogno per il mio database di avviare il codice SQL con una macro, ho provato a chiedere aiuto anche sul forum inglese, ti incollo la risposta che mi hanno appena dato se tu ci capisci qualcosa di più.....

I do not write your macro but I hope the instruction here are clear. I think the only thing you have to do is place a lot of doubles quote in your SQL command. When your SQL command does not work then it does also not work in the macro. Do not mix question about the working of SQL commands with MACRO commands. Be also aware BASIC does not understand SQL and SQL does not understand BASIC. In other words BASIC and SQL are complete different worlds.

When you need to execute SQL code inside a macro then be sure that the SQL code is working. You can test the SQL code in the SQL tool of OOo. Making the macro and the SQL code at the same time more difficult. Starters do not know where is the problems.

After you have test the query inside the SQL you can start with building the macro

There you want start the macro with a button in a form it is more easy to execute the SQL command. With a button in a form what must have a connection to the database it is easy to execute a SQL command all you need is.
oForm.Activeconnection.CreateStatement.execute(sSqlStatement)

Modifying the Original SQL command to the command what is working in a BASIC macro.
When you have surround the variables in the SQL commandwith double quotes then they must be surround in macro with double double quotes
working is SQL tool
select "Name" from "Table"
Becomes in the Macro
"select ""Name"" from ""Table"""

When you have a long SQL line then it is better to split it over several BASIC lines. This can be done when you split the SQL command in several strings and add them with the Ampersamp(&). Each basic line must be end with an underscore. When you have done this do not trust what you have done but test it with a print statement. When you see the same result as what you have done with the test in the SQL tool then it is good

example

Codice: Seleziona tutto

 Select all   Expand view
sStatementorg="select ""Name""" &_
   " from ""Table"""
print sStatementorg

For executing SQL commands there are several methods.
The interface XStatement does have three different methods
executeQuery
executeUpdate
execute
I think the method execute is the best one for what you need.

I hope this detailed instruction does help you and does also help other people.

Codice: Seleziona tutto

REM  *****  BASIC  *****
option explicit 
Sub ExecuteSQLStatement(oEvent as object)
dim oButton
dim oForm
dim oConnection
dim oStatement

dim sStatementorg 'This is the statement how it is working in the SQL Tool
dim sSTatementWorking ' This is the statement how it is working
sStatementorg="Place here the command"
if sStatementorg="Place here the command" then
   print "Add your own SQL command"
   end
end if
'Example code for making one code line over more lines
'sStatementorg="select ""Name""" &_
'   " from ""Table"""
'print sStatementorg
oButton=oEvent.source.model
oForm=oButton.parent ' Go from the button to the form
oConnection=oForm.Activeconnection
select case isnull(oConnection)
   case true
      print "There is no Active connection to the database so we cannot work"
      print "We end"
      end
   case False
      oStatement=oConnection.createStatement
      oStatement.execute(sStatementorg)
end select
End Sub
Fammi sapere se riesci.

Ciao
LibreOffice 4.1.4.2 su Windows 8.1 64bit
Gaetanopr
Volontario
Volontario
Messaggi: 3300
Iscritto il: mercoledì 21 novembre 2012, 20:07

Re: Macro per eseguire comando sql

Messaggio da Gaetanopr »

Grazie
vediamo cosa si riesce a fare
LibreOffice 7.2.2.2 windows 10
Openoffice 4.1.13 su windows 10
Gaetanopr
Volontario
Volontario
Messaggi: 3300
Iscritto il: mercoledì 21 novembre 2012, 20:07

Re: Macro per eseguire comando sql

Messaggio da Gaetanopr »

Finalmente è risolto
ringrazio robce64 e Maurizio(Mizio1961)
Allegati
dbupdate.odb
(14.17 KiB) Scaricato 553 volte
LibreOffice 7.2.2.2 windows 10
Openoffice 4.1.13 su windows 10
Rispondi