Changes on this Version (2025.09.01):
- Modified: 'Select' method. Second parameter can now (optionally) be a reference to a
standard or hash array. This array (if specified) will receive the resulting
recordset in the corresponding format (standard or hash).
In addition, two new optional parameters, allows to specify a grid and window
names to automatically fill it with the resulting recordset. In this case,
the second parameter is optional.
If grid is specified an additional parameter can be specified (codeblocks array)
for formating.
Hash array example...
Code: Select all
hRows := {=>} ; hRow := NIL
oSql:Select('SELECT * FROM hmgtest ORDER BY code', @hRows)
Code: Select all
FOR EACH hRow IN hRows
Test.Grid_1.AddItem({ StrZero(hRow["code"],4), AllTrim(hRow["description"]), AllTrim(hRow["location"]), AllTrim(Str(hRow["stock"])), AllTrim(Str(hRow["price"])) } )
NEXT
Code: Select all
oSql:Select('SELECT * FROM hmgtest ORDER BY code',, 'Grid_1', 'Test')
Code: Select all
aFmt := { ;
NIL, ;
{|x| STRZERO( x, 4) } , ;
{|x| UPPER( x ) } , ;
NIL, ;
NIL , ;
{|x| TRANSFORM( x ,'999,999.99' ) } ;
}
oSql:Select('SELECT * FROM hmgtest ORDER BY code',,'Grid_1','Test',aFmt)
- New: GetLastInsertId() method. Return last AUTO_INCREMENT on INSERT.
- Fixed: Various problems with error processing.
- Modified: Demo, showing new features.
This class must be considered EXPERIMENTAL.
My idea to start this library, was to have a wrapper for 'Simple SQL Interface' contribution by Mindaugas Kavaliauskas that I've used for many years.
This wrapper aims to have the following features:
- Handle all possible runtime error situations (bad parameters, connection, SQL syntax, etc.).
- Show all required progress information and error messages to the user.
- Handle simultaneous connections in an easy/intuitive way.
This is a very very basic documentation:
METHODS:
AffectedRows(): Returns the number of rows modified by the last executed
INSERT, UPDATE, or DELETE command.
CloseAreas(): Close all workareas open with 'Select' method (if any).
Returns: Number of workareas closed.
Commit(): Executes 'COMMIT' command.
Returns: .T. if command was successfully executed (.F. otherwise).
Connect(cServer, cUser, cPassword [, cDatabase]): Connect to a MySql server.
Returns: .T. if connection was successful (.F. otherwise).
Delete( cTable, cWhere ): Delete rows based on 'cWhere' condition.
Returns: .T. if command was successfully executed (.F. otherwise).
Destroy(): Set all object variables to NIL, closes all workareas used by
'Select' method (if any) and disconnects.
Returns: NIL
Disconnect(): Disconnect from server.
Returns: NIL
Exec(cCommand): Executes a MySql command.
Returns: .T. if command was successfully executed (.F. otherwise).
GetLastInsertId(): Returns last AUTO_INCREMENT on INSERT.
Insert(cTable, aCols): Insert row. aCols, specifies cColumnname => xData pairs (hash array).
Returns: .T. if command was successfully executed (.F. otherwise).
IsConnected(): Returns .T. if the current connection still alive, .F. if not.
New(): Object Initialization.
Returns: SELF (refers to the instance of the class that is being constructed).
Reconnect(). Attempts to re-connect using the current parameters.
Returns: .T. if command was successfully executed (.F. otherwise).
RollBack(): Executes 'ROLLBACK' command.
Returns: .T. if command was successfully executed (.F. otherwise).
Select(cCommand,[cWorkArea|@aArray|@hHash],[cGridName,cWindowName][,abFormats]):
cCommand must be a MySql SELECT statement.
If cWorkArea is specified a new workarea will be created containing the
query result. If you, instead, specifies a reference to an array
(or hash array), the resulting recordset will be stored there in the
corresponding format.
If cGridName and cWindowName are specified, a grid will be filled with the
resulting recordset. In this case, second parameter (cWorkArea, aArray or
hArray) is optional.
If grid is specified an additional parameter can be specified (codeblocks array)
for formating.
Returns: .T. if query was successful (.F. otherwise).
StartTransaction(): Executes 'START TRANSACTION' command.
Returns: .T. if command was successfully executed (.F. otherwise).
Update(cTable,cWhere,aCols): Modify rows based on 'cWhere' condition. aCols, specifies
cColumnname => xData pairs (hash array).
Returns: .T. if command was successfully executed (.F. otherwise).
Use(cDatabase): Selects the default database. Useful when connecting without
specifying a database or when working with multiple databases.
Returns: .T. if command was successfully executed (.F. otherwise).
VARIABLES:
cErrorDesc (Read Only): Error description (Empty if no error).
cMsgLang (Read Only): Current language for error/progress messages.
cNoQuoteChar: Set leftmost character indicator to instruct Update and Insert
methods to not quote a character value on aColumns
(cColumnName,xColumnValue) array (default value is '@').
lAutoReconnect: When set to .T. (the default value) operations that requires
connection to the server, will check if the connection still alive, if not,
reconnection will be attempted.
lError (Read Only): .T. if an error occured on last operation (.F. otherwise).
lShowMsgs: Determines if error/progress messages will be shown.
lTrace: When set to .T., a file called 'trace.log' containing last command
executed.
nErrorNo (Read Only) Current error code.
MariaDB:
Last years, for new projects (even local-data ones), I've used portable MariaDB.
It is compact, fast, reliable, secure and requires no installation.
You can download it at https://mariadb.org/download/ and select 'ZIP file' on
'Package Type'. You can unpack it at any place (<base_folder>).
For newer versions, prior to start MariaDB, you must initialize data directory:
<base_folder>\bin\mysql_install_db.exe --datadir=<base_folder>\data
Then you can run MariaDB:
<base_folder>\bin\mysqld.exe.
The default user is root with no password.
Best database utility: HeidiSQL (https://www.heidisql.com/)
I hope this be useful for someone.
Roberto.