|
|
| ��� �������� �������� �� ��������� ������: English Castellano ChineseGB Deutsch Francais Nederlands Russian Turkce |
����� Diego Alberto Arias Prad <dariapra(at)yahoo.es> �� ������:
� ������� ��������� � ������� ���������������� � Lugo, �������. ����� �� ����� ����� � �����
������������ Linux, �����������, ��� ��� ��������� � 1995 ��� 1996 ����. �� ����� �������
� ����������� Microsoft Windows � ���� �� ���� � ������������� Linux. ������� ������ ���������
� Linux � ������������ � ���� �������������� ��� ������������ �������, ����� ����� � ���������
Linux �� ���� �������� ���������. ������ ���� ������������� ��� Slackware.
������� �� �������: Pukhlyakov Kirill <kirill(at)linuxfocus.org> ����������:
|
���������� � ����������� TclMySQL
������:
� ������� �� ���������� ��� ���������� � ������������ ����������� MySQLTcl, ��� ����������
����� Tcl ��� ������ � ���� MySQL �� ��������, ���������� �� ���. ������
Tcl, MySQL � ���������� MySQLTcl �������������� ��� ��������� ������� ��������������
8.4.2, 4.0.15 � 2.40.
|
���� ���� Linux ��� *BSD ������� ������������ ������ ( ������� RPM ��� DEB ) ��� ����� ���
Crux Linux ��� FreeBSD,
�� ������ ������������ �� ��� ��������� ���������� MySQLTcl � ���������� ���� ������.
���� ��� �� ��� ��� �� ������ ������������� ������������� ��� "������", � ��������������� ���
��� ��� ����� ���. �� ������ ������������ ��� ��� ����������, �� �� ��� ��������� �����������
���������. ��������� bash ������������ Linux Mandrake
������ 9.2 ������ ���������:
$ ./configure --with-mysql-lib=/usr/lib $ make $ make install |
$ export CPP=/usr/bin/cpp $ ./configure --with-tcl=/usr/lib/local/tcl8.3 --with-tclinclude=/usr/local/include/tcl8.3 --with-mysql-include=/usr/local/include/mysql --with-mysql-lib=/usr/local/lib/mysql $ make $ make install |
� ���� ������� ���������� ������ Tcl ��� ���, ���� ��������� ������ �������, �� ��� ��
������� � ���. ���� �� �������������� �� Tcl - ������ ���������� ���� ������.
�� ������ ��������������� �� ���� �������� �� �������� Tcl (tclsh).
���������� Tcl ��������� � ������� ������� set. ���������� ��������� ��������:
% set address {Edison Avenue, 83}
Edison Avenue, 83
% set zip_code 48631
48631
|
% set address Edison Avenue, 83 % set zip_code 48631����� ������� �������� ���������� address �� ����� ������������� �������� puts:
% puts stdout [set address] Edison Avenue, 83�������� stdout ������������ ������� puts ������ ��� �������� ������ � ����������� ����� ������ - �� �����. ������ ������������ �������� ������� puts ��� [set address]. ���������� ������ ��������� �������������� Tcl, ��� ��� ������ ������� ����� Tcl, ������� ������ ���� ��������� ����� �������� puts - ��� ���������� ����������� ������. �� �� ����� ����� ������� ��������� �������:
% puts stdout $address Edison Avenue, 83� ���� ������� �� ��������� ����������� ����������: ������ $ ����� ���������� ��������� �� ���.
% puts stdout "the zip code is [set address]"
the zip code is 48631
% puts stdout "the zip code is $address"
the zip code is 48631
% puts stdout {the zip code is [set address]}
the zip code is [set address]
% puts stdout {the zip code is $address}
the zip code is $address
����� �����, ��� ��� ������������� �������� ������ ����������� ���������� �� ����������,
�� ���������� ��� ������������� �������.
% unset address % set address can't read "address": no such variable % unset zip_code % set zip_code can't read "zip_code": no such variable
������ �������� ����� �� ���� �������� ����� ������ � Tcl. ������ - ��� ����� ��������.
������ ��������� � ������� ������� set.
% set surname Westmoreland Westmoreland % set number 46.625 46.625��� ���������� - surname � number ����� ��� ������. �������� � ����������� ����� ���� ����� �������� string. ��������� ���� ������� ��������� - string �������� �������� ������_���������. ������� ���������:
% string length $surname 12 % set surname [string range $surname 0 3] West % puts stdout $surname West % string tolower $surname west� ������� �� Java ��� Pascal Tcl �� ����� ������� ���� ����������������. ��������� ������ ������������� ���:
% set number [expr $number + 24.5] 70.125 % string range $number 2 5 .125� ������� ������� expr �� ��������� �������� ���������� number �� 24.5. ����� ����� �� ����������� ���������� number ��������� � � ������� ������� string ������ ��������� ������ �������.
������ � Tcl �������� �������������� �����, ������� ��������� �������� � ����������������
��������������� ��-������.
% set friends_list {Fany John Lisa Jack Michael}
Fany John Lisa Jack Michael
% set friends_list [string tolower $friends_list]
fany john lisa jack michael
% set friends_list
fany john lisa jack michael
� Tcl ���������� ����� ������ ��� ������ �� ��������. ���������� ��������� ��������:
% lindex 2 $friends_list lisa % lrange $friends_list 2 4 lisa jack michael % set friends_list [lsort -ascii $friends_list] fany jack john lisa michael % set friends_list fany jack john lisa michael % set friends_list [linsert $friends_list 2 Peter] fany jack Peter john lisa michael % string toupper $friends_list FANY JACK PETER JOHN LISA MICHAEL�� ���������� ������� �����, ��� ������ � ������ ����� ���������� ���������.
������ ����� ������������� ������ ����� ������ � �������� � �������� ��������
��������� ���������� ���� "������". ���������� ������ �������� �������:
% set phone_numbers(fany) 629 629 % set phone_numbers(michael) 513 513 % set phone_numbers(john) 286 286�������� �������� �� ������� ����� ��������� ������� set ��� � � ���������� �������, ������ ����������� � ������� ������.
% set $phone_numers(michael) 513���������� � ������� ����� �������� � ������� ������� array. ���������� ��������� �������� ������������� ������ �������:
% array size phone_numbers 3 % array names phone_numbers fany john michael
��� ���� ����� ��������� ������� � ���� ������ �� �������� Tcl ������� ���� ���������� ���������� � �������� ��� ������. � ���� ����� ������� �� ���������� ���� ������, � ����� ��������� ������, ������� ����� ���������� � �������� ����������.
���������� ������ ����������� � ������� MySQL �� Tcl �������:
0: #!/usr/bin/tclsh8.4
1:
2: package require mysqltcl
3: global mysqlstatus
4:
5: set port {3306}
6: set host {127.0.0.1}
7: set user {john_smith}
8: set password {jsmith_password}
9:
10: set mysql_handler [mysqlconnect -host $host
-port $port -user $user -password $password]
11:
12: mysqlclose $mysql_handler
�������� �������� �� ����� ������� ���� - ��� �� �������� ������ ������ �������, ����� �������
�� ������ ������������� ������. �����, � ����������� �� ������������� ���� ������������
Linux, ��� �������� �������� �������� ������ #0, ����������� ���� � Tcl shell.
� ���������� ����� �� �� ����������� ������ #3. ������� ��� ������.
���������� ������ ���������� MySQLTcl ����� �������� ������. ���� �� �� ������� ��� ������,
������ ����� ���������� ���� ������. �������� ������� ��� ������ ��������� �������:
10: if [catch {mysqlconnect -host $host -port $port
-user $user -password $password} mysql_handler] {
11: puts stderr {error, the database connection could not be established}
12: exit
13: } else {
14: mysqlclose mysql_handler
15: }
���� ������� set mysql_handler [mysqlconnect -host $host... ������� ������ - ��� �����
����������� �������� catch. ������� catch ���������� 1 � ������ �������������
������ � 0 � ���������������.
| ������� | �������� |
| code |
���� ������ �� ��������� mysqlstatus(code) ����� ����, � ��������� �������
��������������� � ��� ������ ������� MySQL. ���� ������ ��������, �� �� �������� �������
MySQL, �� �������� mysqlstatus(code) ��������������� � -1.
�������� mysqlstatus(code) ����������� ����� ������� ������������� ������� ���������� MySQLTcl. |
| command |
������� ���������� MySQLTcl, ���������� ������� �������� ������, ����������� �
mysqlstatus(command).
�������� mysqlstatus(command) ����������� ����� ������ ������� ��������� ������, ������� ������� - mysqlstatus(command) �� ����������� ����� �������� �������. |
| message | �������� mysqltcl(message) ����������� ����� ������ ������� ��������� ������ � ����������� ���������� �� ������. ����� ��� � mysqlstatus(command), mysqlstatus(message) �� ����������� ����� �������� �������. |
| ������� | �������� |
| nullvalue | ������, ������������ ��� �������� null ��� ���������� ���������� SQL �������. �� ��������� ������������ ������ ������; musqlstatus(nullvalue) ����� ���� ����������� � ����� ��������� ��������. |
10: catch {mysqlconnect -host $host -port $port
-user $user -password $password} mysql_handler
11: if {$mysqltatus(code) != 0} {
12: puts stderr $mysqlstatus(message)
13: } else {
14: mysqlclose mysql_handler
15: }
���������� ������ mysqlstatus ����� ������������ ��� ��������� �������� ����� ������,
�� ������ ��� ������������ ���������� � ��.
� ���� ����� ���������� �������� ������� ���������� MySQLTcl � ��������� ������� ��
�������������. ������ ���������� �� ������� ������� �������� �� man �������� MySQLTcl.
������� �������� ������� � ������� ���������. ��������� �����������. ���� �������� ����� �����
"?" - �� ������������, ������ "|" ���������� "���".
| ������� | ������� �������� |
| mysqlconnect ?option value ...? | ����������� � ��; ���������� handler, ������� ���������� ������������ � ������ �������� mysqltcl |
| mysqluse handle dbname | ����������� MySQL handler � ������������ �� |
| mysqsel handle sql_statement ?-list | -flatlist? | �������� SQL ������� � �� |
| mysqlexec handle sql_statement | �������� ��_select SQL ������� � �� |
| mysqlclose handle | �������� ���������� � �� |
��� ������� �� ��� ��������� � ������� "���������� � �������� ��", �� ���� ���� ������,
������� �� �� ��������� - �������� -db, �� ����� ��� �������� ������������ �� ���
������������ �������������. ��������� ������:
% set mysql_handler [mysqlconnect -h 127.0.0.1 -p 3306 \\ -user john_smith -password jsmith_password -db jsmith_database]� ���� ������ ��� ����������� ������������� mysql_handler ������� ����� ����� ���������� � �� jsmith_database.
������� ������ ��� ����� �� ��������������� � handler'��, �� ����������� ������ ����������.
� ������� ������ ������� ���������� SQL select ������ � ��. ���� �������� sql_statement
�� �������� SQL select �������� - ���������� ������.
����� ������������ � ���� ������� ������ ( ������������ ) �������� - list ��� flat_list.
��������� �� ������� ��� ���������� � �������, ��� ������������� ����� ���������. �����������
� �� ���� ������� people:
| id | first_name | last_ name | phone |
| 26 | Karl | Bauer | 8245 |
| 47 | James | Brooks | 1093 |
| 61 | Roberto | Castro Portela | 6507 |
% mysqlsel $mysql_handler {select first_name, last_name from people order by id asc} -list
{Karl Bauer} {James Brooks} {Roberto {Castro Portela}}
% mysqlsel $mysql_handler {select first_name, last_name from people order by id asc} -flatlist
Karl Bauer James Brooks Roberto {Castro Portela}
� ������ ������ (-list parameter) ������� ���������� ������, �������� �������� ������.
�� ������ ������ (-flatlist parameter) ������� ���������� ������, �������� ��������
���� ��� ������.
% mysqlsel $mysql_handler {select first_name, last_name from people order by id asc}
3
� ������� ������� mysqlexec ����� ������� ��_select SQL ������ � ��. ���� � �������
���� ������� �� ������� select SQL ������ - ������ �� ����������, �� � ������ �� ����� �������.
��������� ��� ��� �� ���������� ������:
% mysqlexec $mysql_handler {delete from people where id=26}
1
% mysqlsel $mysql_handler {select first_name, last_name, phone from people order by id asc} -list
{James Brooks 1093} {Roberto {Castro Portela} 6507}
% mysqlexec $mysql_handler \\
{insert into people (id, first_name, last_name, phone) values (58, "Carla", "di Bella", 8925)}
1
% mysqlsel $mysql_handler {select first_name, last_name, phone from people order by id asc} -list
{James Brooks 1093} {Carla {di Bella} 8925} {Roberto {Castro Portela} 6507}
����������� SQL ������� �������� �� delete ��� insert ����� ���� ������� � ������� ���� �������.
�������� update ������:
% mysqlexec $mysql_handler {update people set phone=3749 where first_name="James"}
1
% mysqlsel $mysql_handler {select first_name, last_name, phone from people order by id asc} -list
{James Brooks 3749} {Carla {di Bella} 8925} {Roberto {Castro Portela} 6507}
����������� ������ ������� mysqlexec �������� ����� �����, ������� ���� ����������
��_select SQL ��������.
������� mysqlclose ��������� ���������� � ��. ���������� ���� ������� �������� MySQL handler ����������, ������� �� ����� �������.
����� ������ ���������� MySQLTcl �� �������������� �������������� ���� - ���� ������� ��� ��������� ���������� � ��, ��������� ����� ��� ������������� � ��������, �������� ��������� ��������. ���������� ������ ��� �������� ���������� MySQLTcl �������� �� man ��������.
[1] a slightly skeptical view on John K. Ousterhout and Tcl:
http://www.softpanorama.org/People/Ousterhout/index.shtml
[2] ����������� �� ���������� ���������� Tcl:
http://www.mapfree.com/sbf/tcl/book/select/Html/7.html
TclTutor - ���������� ���������� ��� �������� Tcl:
http://www.msen.com/~clif/TclTutor.html
������������ MySQL � ������ �������� ( HTML, PDF... ):
http://www.mysql.com/documentation/index.html
|
|
Webpages maintained by the LinuxFocus Editor team
© Diego Alberto Arias Prad, FDL LinuxFocus.org |
Translation information:
|
2004-04-23, generated by lfparser version 2.43