UltraLite Embedded SQL User's Guide
Adding Non Data Access Features to UltraLite Applications
Adding user authentication to your application
Managing user IDs and passwords
The following code fragment performs user management and authentication for an embedded SQL UltraLite application.
A complete sample can be found in the Samples\UltraLite\esqlauth subdirectory of your SQL Anywhere directory. The code below is taken from Samples\UltraLite\esqlauth\sample.sqc.
app() { ... /* Declare fields */ EXEC SQL BEGIN DECLARE SECTION; char uid[31]; char pwd[31]; EXEC SQL END DECLARE SECTION; ULEnableUserAuthentication( &sqlca ); db_init( &sqlca ); ... EXEC SQL CONNECT "DBA" IDENTIFIED BY "SQL"; if( SQLCODE == SQLE_NOERROR ) { printf("Enter new user ID and password\n" ); scanf( "%s %s", uid, pwd ); ULGrantConnectTo( &sqlca, UL_TEXT( uid ), UL_TEXT( pwd ) ); if( SQLCODE == SQLE_NOERROR ) { // new user added: remove DBA ULRevokeConnectFrom( &sqlca, UL_TEXT("DBA") ); } EXEC SQL DISCONNECT; } // Prompt for password printf("Enter user ID and password\n" ); scanf( "%s %s", uid, pwd ); EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
The code carries out the following tasks:
Enable user authentication by calling ULEnableUserAuthentication.
Initiate database functionality by calling db_init.
Attempt to connect using the default user ID and password.
If the connection attempt is successful, add a new user.
If the new user is successfully added, delete the DBA user from the UltraLite database.
Disconnect. An updated user ID and password is now added to the database.
Connect using the updated user ID and password.
For more information, see ULGrantConnectTo function, and ULRevokeConnectFrom function.