Topic: Get username in triggers

I have a very specific question. Is it possible to retrieve the Windows username using the application and use it to populate a table via a SQLite trigger?

I managed to populate a log table for any change occuring in any table via trigger but I struggle in getting the connected username. Any suggestion?

Re: Get username in triggers

You can retrieve the Windows username

example:

procedure Form1_Button2_OnClick (Sender: string; var Cancel: boolean);
begin
    ShowMessage(GetUserName);
end;

Then using function SQLExecute and SQL, you can create the trigger.
More info
https://www.sqlite.org/lang_createtrigger.html

Dmitry.

Re: Get username in triggers

Thanks Dmitry


and to populate the column "username", is it as follow?


insert into ...blablabla (column_names, blablabla...., username)
values (blablabla, values, blablabla..., '+sUsername+') 


or like this?

insert into ...blablabla (column_names, blablabla...., username)
values (blablabla, values, blablabla..., $Username)

Re: Get username in triggers

Unfortunately I've never worked with triggers, I do not know smile

Dmitry.

Re: Get username in triggers

What a shame.... it is way easier to populate a logging table without all the scripting

Here I have a simple example (I use SqliteStudio to create the triggers):

--  Create an insert trigger
CREATE TRIGGER insert_Maint_log AFTER INSERT  ON Maintenance
BEGIN

  INSERT INTO logs  (    
    Change_date,
    Maint_PcName,            
    Maint_id_Typ           ,
    sqlAction         ,
    Maint_details       ,
    Maint_date,
    src,
    username
    
      
        
    )

          values (
DATETIME('NOW'),
new.PCName,
new.id_Pc_Typ,
'INSERT',
new.Maint_Details,
new.Maint_date,Maintenance,
????????);

END;

Re: Get username in triggers

Please example project show


tcoton wrote:

What a shame.... it is way easier to populate a logging table without all the scripting

Here I have a simple example (I use SqliteStudio to create the triggers):

--  Create an insert trigger
CREATE TRIGGER insert_Maint_log AFTER INSERT  ON Maintenance
BEGIN

  INSERT INTO logs  (    
    Change_date,
    Maint_PcName,            
    Maint_id_Typ           ,
    sqlAction         ,
    Maint_details       ,
    Maint_date,
    src,
    username
    
      
        
    )

          values (
DATETIME('NOW'),
new.PCName,
new.id_Pc_Typ,
'INSERT',
new.Maint_Details,
new.Maint_date,Maintenance,
????????);

END;

Re: Get username in triggers

@r00t3d777


Actually it could be any project since I am using internal sqlite triggers, I just need to know how to retrieve the windows account currently connected to insert it in the log table via the trigger.

Re: Get username in triggers

I finally managed to get it working with a trick smile

I use a table to store the username captured by Dmitry's script and then I added a select in my trigger to get the last username_id.

Into MVDB scripts:


Setting the variables

// Global variables declarations
var

   sUserName: string;

Saving the username with current date into Table "WindowsID" when starting the application

// Load automatically information into grids in form1 + operator tracking
procedure Form1_OnShow (Sender: string; Action: string);
var
sCurrentDate: string;
begin
    sCurrentDate := '"' + FormatDateTime('yyyy-MM-DD hh:nn:ss.000', now) + '"';
    SQLExecute ('INSERT INTO WindowsID (DateTime, AccountID)' + 'VALUES (' +sCurrentDate+', '+ sUserName +')');
    Form1.LastUser_Grid.dbUpdate;
  

end;

Getting the current Windows username

Begin
// get current windows user login 

    sUserName := '"'+GetUserName+'"';

End.

And then, using SQLiteStudio, I create my triggers to log as much information I want:

Insert trigger:

INSERT INTO logs  (    
    Change_date,
    New_Id_user,            
    sqlAction         ,
    New_usr_Name       ,
    New_firstname     ,
    new_UsrID    ,
    new_dpt            ,
    New_Room      ,
    new_id_costcenter   ,
    New_id_computer ,
    src,
    Username      
        
    )

          values (DATETIME('NOW','localtime'),new.id,'INSERT',new.Name,
                  new.Firstname, new.UserID,
                  new.Dpt,new.Room,new.id_CostCenter,
                   new.id_Computers, 'Users',(select AccountID from WindowsID where id= (select max(id) from windowsID))
                 );


Update trigger:

INSERT INTO logs  (    
    Change_date,
    New_Id_user,            
    old_ID_user            ,
    sqlAction         ,
    Old_usr_Name       ,
    New_usr_Name       ,
    Old_firstname     ,
    New_firstname     ,
    old_UsrID   ,
    new_UsrID    ,
    old_dpt            ,
    new_dpt            ,
    old_Room     ,
    New_Room      ,
    old_id_costcenter   ,
    new_id_costcenter   ,
    Old_id_Computer       ,
    New_id_computer  ,
    src ,
    Username    
        
    )

          values (DATETIME('NOW','localtime'),new.id,old.id,'UPDATE',old.Name,new.Name,old.Firstname,
                  new.Firstname,old.UserID, new.UserID,old.Dpt,
                  new.Dpt,old.Room,new.Room,old.id_CostCenter,new.id_CostCenter,
    old.id_Computers,new.id_Computers,'Users',(select AccountID from WindowsID where id= (select max(id) from windowsID))
                 );

Delete trigger:


INSERT INTO logs  (    
    Change_date,            
    old_ID_user            ,
    sqlAction         ,
    Old_usr_Name       ,
    Old_firstname     ,
    old_UsrID   ,
    old_dpt            ,
    old_Room     ,
    old_id_costcenter   ,
    Old_id_Computer ,
    src ,
    Username        
        
    )

          values (DATETIME('NOW','localtime'),old.id,'DELETE',old.Name,old.Firstname,
                 old.UserID,old.Dpt,
                  old.Room,old.id_CostCenter,
    old.id_Computers, 'Users',(select AccountID from WindowsID where id= (select max(id) from windowsID))
                 );

Re: Get username in triggers

tcoton, Please example project sent me smile


tcoton wrote:

I finally managed to get it working with a trick smile

I use a table to store the username captured by Dmitry's script and then I added a select in my trigger to get the last username_id.

Into MVDB scripts:


Setting the variables

// Global variables declarations
var

   sUserName: string;

Saving the username with current date into Table "WindowsID" when starting the application

// Load automatically information into grids in form1 + operator tracking
procedure Form1_OnShow (Sender: string; Action: string);
var
sCurrentDate: string;
begin
    sCurrentDate := '"' + FormatDateTime('yyyy-MM-DD hh:nn:ss.000', now) + '"';
    SQLExecute ('INSERT INTO WindowsID (DateTime, AccountID)' + 'VALUES (' +sCurrentDate+', '+ sUserName +')');
    Form1.LastUser_Grid.dbUpdate;
  

end;

Getting the current Windows username

Begin
// get current windows user login 

    sUserName := '"'+GetUserName+'"';

End.

And then, using SQLiteStudio, I create my triggers to log as much information I want:

Insert trigger:

INSERT INTO logs  (    
    Change_date,
    New_Id_user,            
    sqlAction         ,
    New_usr_Name       ,
    New_firstname     ,
    new_UsrID    ,
    new_dpt            ,
    New_Room      ,
    new_id_costcenter   ,
    New_id_computer ,
    src,
    Username      
        
    )

          values (DATETIME('NOW','localtime'),new.id,'INSERT',new.Name,
                  new.Firstname, new.UserID,
                  new.Dpt,new.Room,new.id_CostCenter,
                   new.id_Computers, 'Users',(select AccountID from WindowsID where id= (select max(id) from windowsID))
                 );


Update trigger:

INSERT INTO logs  (    
    Change_date,
    New_Id_user,            
    old_ID_user            ,
    sqlAction         ,
    Old_usr_Name       ,
    New_usr_Name       ,
    Old_firstname     ,
    New_firstname     ,
    old_UsrID   ,
    new_UsrID    ,
    old_dpt            ,
    new_dpt            ,
    old_Room     ,
    New_Room      ,
    old_id_costcenter   ,
    new_id_costcenter   ,
    Old_id_Computer       ,
    New_id_computer  ,
    src ,
    Username    
        
    )

          values (DATETIME('NOW','localtime'),new.id,old.id,'UPDATE',old.Name,new.Name,old.Firstname,
                  new.Firstname,old.UserID, new.UserID,old.Dpt,
                  new.Dpt,old.Room,new.Room,old.id_CostCenter,new.id_CostCenter,
    old.id_Computers,new.id_Computers,'Users',(select AccountID from WindowsID where id= (select max(id) from windowsID))
                 );

Delete trigger:


INSERT INTO logs  (    
    Change_date,            
    old_ID_user            ,
    sqlAction         ,
    Old_usr_Name       ,
    Old_firstname     ,
    old_UsrID   ,
    old_dpt            ,
    old_Room     ,
    old_id_costcenter   ,
    Old_id_Computer ,
    src ,
    Username        
        
    )

          values (DATETIME('NOW','localtime'),old.id,'DELETE',old.Name,old.Firstname,
                 old.UserID,old.Dpt,
                  old.Room,old.id_CostCenter,
    old.id_Computers, 'Users',(select AccountID from WindowsID where id= (select max(id) from windowsID))
                 );

10 (edited by tcoton 2015-06-11 20:43:49)

Re: Get username in triggers

Please find a simple example with one Table Employee and one table logs.

To see and edit the triggers, you will have to use an external tool SQLStudio
Have fun!! wink

Post's attachments

Attachment icon LogsbyTrigger-example.zip 338.33 kb, 897 downloads since 2015-06-11 

Re: Get username in triggers

tcoton wrote:

Please find a simple example with one Table Employee and one table logs.

To see and edit the triggers, you will have to use an external tool SQLStudio
Have fun!! wink

Thanks Tcoton wink