2.4. Example function: table_list.g

This example lists all the tables in a MySQL database.

require ("MySQLSupport");

host := "localhost";
user := "test";
password := "";
database := "test_gamma";

function main ()
{
  local		mysql = new MYSQL();
  local		result;
  
  if (!mysql.connect (host, user, password))
    error (string ("Could not connect to host: ", host,
		   " as user: ", user));
  else if (mysql.select_db (database) == -1)
    error (string ("Could not select database: ", database));
  
  // We can use the built-in method
  princ("\nThe tables in ", database, "\nUsing the built-in method list_tables():\n");
  result = mysql.list_tables ("%");
  with row in result.Data() do
    princ (row[0], "\n");
  
  // ... or we can issue the query manually
  princ("Using the query_and_store method for a manual query:\n");
  result = mysql.query_and_store("show tables");
  with row in result.Data() do
    princ (row[0], "\n");
  princ ("\n");
}

Output

The tables in test_gamma
Using the built-in method list_tables():
customers
suppliers
Using the query_and_store method for a manual query:
customers
suppliers