MySQL

From wiki.visual-prolog.com

Revision as of 11:34, 26 September 2008 by Steve Lympany (talk | contribs) (spelling etc)

MySQL and VIP

This article briefly describes how to install MySQL locally and how to interact with a MySQL DB using a VIP (GUI) program.


  1. Download and install MySQL

    See http://www.mysql.com
    and
    http://dev.mysql.com/downloads/
    Download the MySQL community Server

  2. Configure the Server

    At the end of the installation you will automatically be prompted to configure the server, via the MySQL Server Instance Configuration Wizard. You can call this wizard at any time from the Windows Start-Programs-MySQL menu item.

    Select all the defaults for the dialogs as presented by the wizard: it's probably best to use a non-blank password (e.g. "scott").

    Check that the server is running as a Windows service (Control Panel - Performance and Maintenance - Administrative Tools - Services)

  3. Download and install the MySQL ODBC Driver

    See http://www.mysql.com/products/connector/odbc/

  4. Create a Datasource

    Go to Control Panel - Performance and Maintenance - Administrative Tools - Data Sources (ODBC)
    Under the dialog tab User DSN, press ADD, select the MySQL ODBC Driver (probably version 3.51).

    In the dialog, enter the following:
    • Data Source Name: myodbc
    • Description: a test
    • Server: localhost
    • User: root
    • Password: scott (the password you entered for the server in step 2 above)
    • Database: fred
      Press the TEST button - it should connect.

  5. Test the database with the Command Line Client

    From the Start-All Programs-MySQL menu, start the command line client. Enter the password at the prompt. The prompt should now be:
    mysql>

    • Enter "show databases;" (without the quotes, but with the semi-colon), and database "fred" should be listed.
    • Enter "use fred"

      Now create a table called "person" in "fred":
    • Enter "create table person (name varchar(20), age integer);"
    • Enter "show tables;" (remember the semi-colons)

      Enter some data into the table:
    • Enter "insert into person values('fred','87');"

      And then check the data is there:
    • Enter "select * from person;"

      and the data should be listed.

      Close the MySQL session:
    • Enter "quit".

  6. Create a new VIP GUI project
    Cut and paste the following code into taskwindow.pro, and (e.g.) call predicate test:() from File-New.

    This example adds one more record to the table "person" and then lists the table. It will also list the data entered previously at the mysql command line prompt (step 5 above).
predicates
	test:() determ.
clauses
	test():-
        	Connection = odbcConnection::new(256),
		ConStr =  @"DSN=myodbc;"
			@"UID=root;"
			@"PW=scott;",
		X = Connection:driverConnect(getVpiWindow(), ConStr, odbc_native::sql_driver_noprompt),
		stdio::write("Connection info =",X),
%			OR USE 
%			Connection:connect("myodbc","root","scott"),
		Stmt = odbcStatement::new(Connection),
		Stmt:execDirect("INSERT INTO person VALUES ('harrypotter','4')"),
		look(Stmt),
		Connection:disconnect().
 
predicates
	look : (odbcStatement Stmt).
clauses
	look(Stmt) :-
		Stmt:resetParameter_all(),
		Stmt:execDirect("select * from person"),
		NumColumns = Stmt:numResultCols(),
		Stmt:fetch_nd(),
		   stdIO::nl(),
		   Column = convert(core::positive, programControl::sequence_multi(1, NumColumns+0)),
		   ColName = Stmt:colAttribute_string(Column+0, odbc_native::sql_desc_name),
		   ColValue = Stmt:getColumnValue(Column+0),
		   stdIO::writef("% = %, ", ColName, ColValue),
		fail.
	look(Stmt) :-
		Stmt:closeCursor().