MSQL Perl interface commands Dica retirada do site www.websetter.com |
Making Connections |
|
$dbh = Msql->connect($host)
|
dbh gets connection information from host
|
|
$dbh->selectdb("database")
|
dbh also gets the dtabase informatino
|
|
$dbh = Msql->connect($host,"database")
|
dbh connects to host with database 'test'
|
Database & Server Information |
|
@databaselist = $dbh->listdbs
|
@databaselist gets all the current databases from the server selected in $dbh
|
|
@tablelist = $dbh->listtables
|
@tablelist gets all the tables from the database selected in $dbh
|
|
$fields = $sth->numfields
|
$fields gets the number of rows in the database determined by $dbh and the table specified in the query
|
|
$sth->name
|
Returns array reference to the field names.
|
|
@fieldnum{@{$sth->name}} = 0..@{$sth->name}-1
|
%fieldname gets (id => 0, num => 1, val => 2)
|
|
$dbh->query("drop table $newtable")
|
Drop table example
|
|
Msql->getserverinfo
|
Returns server version
|
|
Msql->errmsg =~ /error/
|
Checks Msql error (can also be checked with eval and $@
|
Composing Queries |
$query = qq{ create table $newtable ( id char(3), num char(4) not null, val char (5)) }
|
$query gets a query statement
|
|
$dbh->query($query)
|
performs the query $query on the database selected by $dbh
|
$query = qq{ insert into $newtable values ('one', 'two', 'three') }
|
Another query example
|
|
$query = "select * from $newtable"
|
Another query example - finds everything
|
|
$sth = $dbh->query($query)
|
$sth gets a reference to the query result
|
$sth = $dbh->query("select * from $newtable where id = 'one'")
|
Another query example. $sth gets a reference to the result
|
Working with Query Results |
|
@row = $sth->fetchrow
|
@row gets a value for each field in the fecthrow result. e.g. ('one', 'two', 'three')
|
|
$rows = $sth->numrows
|
$rows gets the number of rows in the database determined by $dbh and the table specified in the query
|
|
$sth->length
|
Returns array reference to the field lengths. e.g. @{$sth->length} is ('3', '4', '5')
|
|
$sth->table
|
Returns table array reference
|
|
$sth->type
|
Reference to the data type. Used to compare with the exported functions (CHAR_TYPE, NUM_TYPE and REAL_TYPE)
|
|
$sth->type->is_not_null
|
Returns definedness information
|
|
$sth->type->is_pri_key
|
Returns indexing information
|
|
$sth->fetchrow())
|
Returns the next rownumber - e.w. while (@row = $sth->fetchrow()) {do something}
|
|
$sth->dataseek(parameter)
|
E.G. - $sth->dataseek(0) starts at the top of the query again.
|
|
undef ($sth)
|
frees memory associated with $sth
|
|
$sth_query->fetchhash
|
Fetchhash puts the contents of a row into a hash
|
This is designed to be a quick reference to the various msqlPerl commands.
Send any questions or comments to sjohnson@websetter.com
To the main page |