MYSQL.insert

MYSQL.insert — inserts a new row into a table.

Syntax

MYSQL.insert (row)

Arguments

row

An instance of a class corresponding to a table.

Returns

On success, nil, or an error on failure.

Description

This method of the MYSQL class inserts a new row into the table corresponding to the class of the row argument. The value in the primary key of the row is ignored, on the assumption that the primary key is auto-incrementing. The new primary key value will be stored into the row instance.

This method is defined in the Gamma library /usr/cogent/require/MySQLSupport.g. It is made available to a Gamma program with the statement: require("MySQLSupport");.

Example

This example is taken from Section 2.2, “Tutorial 2: create_pets.g”

...

  /* Test the MYSQL.insert method. */
  inserted = mysql.insert(newrow);
  pretty_princ ("\nINSERT  After inserting this new row:\n", newrow, "\n");
  pretty_princ ("The .insert method returns:\n", inserted, "\n");
  query = mysql.query_to_class (mypetclass, "select * from pets where sex = 'f'");
  pretty_princ ("And now the .query_to_class method ",
		"applied to females in mypetclass returns:\n", query, "\n");
    
...

The output for this section of the code is as follows:

INSERT  After inserting this new row:
{mypetclass (birth . 1925-12-25) (death . N) (id . 9) (name . Petunia)
	    (owner . Warner Bros.) (sex . f) (species . pig)}
The .insert method returns:
nil
And now the .query_to_class method applied to females in mypetclass returns:
[{mypetclass (birth . 1993-02-04) (death) (id . 1) (name . Fluffy)
	     (owner . Harold) (sex . f) (species . cat)} 
 {mypetclass (birth . 1998-09-11) (death) (id . 6) (name . Chirpy)
	     (owner . Gwen) (sex . f) (species . bird)} 
 {mypetclass (birth . 1925-12-25) (death . 0000-00-00) (id . 9)
	     (name . Petunia) (owner . Warner Bros.) (sex . f)
	     (species . pig)}]