Database Utility



The Utility Rate Database (URDB) is a free storehouse of rate structure information from utilities in the United States. The URDB includes rates for utilities based on the authoritative list of U.S. Utility companies maintained by the U.S. Department of Energy’s Energy Information Administration. This utility may be run in conjunction with Microsoft Jet database engine 3.x and 4.x for recovering corrupted databases. Although you can run the Microsoft Access Compact utility or the CompactDatabase method with Microsoft Jet database engine 3.x and 4.x, Jetcomp.exe may be able to recover some databases that these utilities cannot.

The Database Utility Class contains methods that help you manage yourdatabase.

This utility even provides the capability to roll forward from the logical logs to bring the table current as of a specific point in time. Seeing the utility in action. The table-level restore is best explained using an example. In this case, we can perform a restore of mytable in the storesdemo database from an ontape. Microsoft Database Utility missing on my Mac Hi, I have issues exporting my Outlook folders into an olm file and some of the advice found in thsi forum suggested I rebuild Outlook Database.

  • Using the Database Utilities
  • Backup Your Database

Important

In order to initialize the Utility class, your databasedriver must already be running, since the utilities class relies on it.

Load the Utility Class as follows:

You can also pass another database object to the DB Utility loader, in casethe database you want to manage isn’t the default one:

In the above example, we’re passing a custom database object as the firstparameter and then tell it to return the dbutil object, instead ofassigning it directly to $this->dbutil.

Note

Both of the parameters can be used individually, just pass an emptyvalue as the first one if you wish to skip it.

Once initialized you will access the methods using the $this->dbutilobject:

Returns an array of database names:

Sometimes it’s helpful to know whether a particular database exists.Returns a boolean TRUE/FALSE. Usage example:

Note

Replace database_name with the name of the database you arelooking for. This method is case sensitive.

Permits you to optimize a table using the table name specified in thefirst parameter. Returns TRUE/FALSE based on success or failure:

Note

Not all database platforms support table optimization. It ismostly for use with MySQL.

Permits you to repair a table using the table name specified in thefirst parameter. Returns TRUE/FALSE based on success or failure:

Note

Not all database platforms support table repairs.

Permits you to optimize the database your DB class is currentlyconnected to. Returns an array containing the DB status messages orFALSE on failure.

Note

Not all database platforms support database optimization. Itit is mostly for use with MySQL.

Permits you to generate a CSV file from a query result. The firstparameter of the method must contain the result object from yourquery. Example:

The second, third, and fourth parameters allow you to set the delimiternewline, and enclosure characters respectively. By default commas areused as the delimiter, “n” is used as a new line, and a double-quoteis used as the enclosure. Example:

Important

This method will NOT write the CSV file for you. Itsimply creates the CSV layout. If you need to write the fileuse the File Helper.

Permits you to generate an XML file from a query result. The firstparameter expects a query result object, the second may contain anoptional array of config parameters. Example:

Important

This method will NOT write the XML file for you. Itsimply creates the XML layout. If you need to write the fileuse the File Helper.

Permits you to backup your full database or individual tables. Thebackup data can be compressed in either Zip or Gzip format.

Note

This feature is only available for MySQL and Interbase/Firebird databases.

Note

For Interbase/Firebird databases, the backup file name is the only parameter.

$this->dbutil->backup(‘db_backup_filename’);

Note

Due to the limited execution time and memory available to PHP,backing up very large databases may not be possible. If your database isvery large you might need to backup directly from your SQL server viathe command line, or have your server admin do it for you if you do nothave root privileges.

Backup preferences are set by submitting an array of values to the firstparameter of the backup() method. Example:

PreferenceDefault ValueOptionsDescription
tablesempty arrayNoneAn array of tables you want backed up. If left blank all tables will beexported.
ignoreempty arrayNoneAn array of tables you want the backup routine to ignore.
formatgzipgzip, zip, txtThe file format of the export file.
filenamethe current date/timeNoneThe name of the backed-up file. The name is needed only if you are usingzip compression.
add_dropTRUETRUE/FALSEWhether to include DROP TABLE statements in your SQL export file.
add_insertTRUETRUE/FALSEWhether to include INSERT statements in your SQL export file.
newline“n”“n”, “r”, “rn”Type of newline to use in your SQL export file.
foreign_key_checksTRUETRUE/FALSEWhether output should keep foreign key checks enabled.
class CI_DB_utility
backup([$params = array()])
Parameters:
  • $params (array) – An associative array of options
Returns:

raw/(g)zipped SQL query string

Return type:

string

Perform a database backup, per user preferences.

database_exists($database_name)
Parameters:
  • $database_name (string) – Database name
Returns:

TRUE if the database exists, FALSE otherwise

Return type:

bool

Check for the existence of a database.

list_databases()
Returns:Array of database names found
Return type:array

Retrieve a list of all the database names.

optimize_database()
Returns:Array of optimization messages or FALSE on failure
Return type:array

Optimizes the database.

optimize_table($table_name)
Parameters:
  • $table_name (string) – Name of the table to optimize
Returns:

Array of optimization messages or FALSE on failure

Return type:

array

Optimizes a database table.

repair_table($table_name)
Parameters:
  • $table_name (string) – Name of the table to repair
Returns:

Array of repair messages or FALSE on failure

Return type:

array

Repairs a database table.

csv_from_result($query[, $delim = ', '[, $newline = 'n'[, $enclosure = '']]])
Parameters:
  • $query (object) – A database result object
  • $delim (string) – The CSV field delimiter to use
  • $newline (string) – The newline character to use
  • $enclosure (string) – The enclosure delimiter to use
Returns:

The generated CSV file as a string

Return type:

string

Translates a database result object into a CSV document.

Database utility
xml_from_result($query[, $params = array()])
Parameters:
  • $query (object) – A database result object
  • $params (array) – An associative array of preferences
Returns:

The generated XML document as a string

Return type:

string

Translates a database result object into an XML document.

Posted September 20, 2020 by Vishwanath Dalvi in Database, SQL Server

SQLCMD is a command-line utility to connect and execute SQL queries from the command prompt. Using SQLCMD we can only connect to SQL Server instances. Importantly we can run ad-hoc as well as interactive script plus automating SQL scripts execution. This utility is pre-installed with SQL Server installation. Instead of relying on SSMS for quick tasks, we can use SQLCMD utility to execute SQL queries through ad-hoc statements or .SQL files.

SQLCMD Utility

1. Command-line utility to execute SQL scripts
2. Execute ad-hoc & interactive SQL script execution
3. Execute .SQL file queries on a database for automation

Database Utility Service

How to Launch SQLCMD Utility

As stated this utility is pre-installed with SQL Server installation. Under SQL Server root directly thus we can launch this from any path in the command prompt.

Go to Run > cmd > sqlcmd

SQLCMD – Help Options

Before starting up with SQLCMD it is essential to understand all the options within this utility. Understanding all the options will help to explore advanced features. You can use sqlcmd -h in the command prompt to see the following figure.


Database Utility Software

SQLCMD – Connect SQL Server Using Windows Authentication

Connecting to SQL Server instance using SQLCMD option. We have –S (Server Name) & -E (Trusted connection) switch for SQLCMD to connect to SQL Server. Using the following command you will be connected to an SQL Server instance. >1 indicates a successful connection to SQL instance using SQLCMD.

[ -S Server ]
[ -E Trusted Connection]

sqlcmd -S DESKTOP-4COH4E7SQLSERVER2019 -E


SQLCMD – Connect SQL Server Using SQL Server Authentication

Connecting to SQL Server using SQLCMD with SQL Server authentication. By specifying SQL server username and password with instance name can connect to SQL Server.

[ -S Server]
[ -U login id]
[ -P password]

sqlcmd -S DESKTOP-4COH4E7SQLSERVER2019 -U vishwanathdalvi -P YourPassword


SQLCMD – Execute Queries

We have learned how to connect to SQL Server instances using SQLCMD. Next we will learn to execute queries. Once >1 prompt is visible you can execute queries. Add GOkeyboard at end of the query to mark the end of the query.

Summary

In a nutshell, we have seen how to use SQLCMD utility to connect to SQL Server instance. Using Windows & SQL Server authentication connected to SQL Server. Executing queries through command prompt using SQLCMD utility, in next few posts we shall explore more on SQLCMD utility use.

Read more and browse through more posts related to SQL Server on Tech-Recipes.

1. How To Deploy ISPAC File & SSIS Package From Command Line
2. SSIS- How To Export & Import ISPAC File SSISDB – Visual Studio
3. How To Create Database Diagram In SQL Server SSMS
4. How To Index Computed Column In SQL Server
5. How To Use Computed Column In SQL Server

About Vishwanath Dalvi

Database Utility In Dbms

Vishwanath Dalvi is a gifted engineer and tech enthusiast. He enjoys music, magic, movies, and gaming. When not hacking around or supporting the open source community, he is trying to overcome his phobia of dogs.
View more articles by Vishwanath Dalvi

The Conversation

Database Utility Mac Office 2016

Follow the reactions below and share your own thoughts.