You are on page 1of 21

MySQL Client-Server

Applications with
Visual FoxPro

Whil Hentzen

Hentzenwerke Publishing

Published by:
Hentzenwerke Publishing
980 East Circle Drive
Whitefish Bay WI 53217 USA
Hentzenwerke Publishing books are available through booksellers and directly from the
publisher. Contact Hentzenwerke Publishing at:
414.332.9876
414.332.9463 (fax)
www.hentzenwerke.com
booksales@hentzenwerke.com
MySQL Client-Server Applications with Visual FoxPro
By: Whil Hentzen
Technical Editor: Ted Roche
Copy Editor: Nicole Robbins-McNeish
Cover Art: Teaching Tricks by Todd Gnacinski, Milwaukee, WI
Copyright 2007 by Whil Hentzen
All other products and services identified throughout this book are trademarks or registered
trademarks of their respective companies. They are used throughout this book in editorial
fashion only and for the benefit of such companies. No such uses, or the use of any trade name,
is intended to convey endorsement or other affiliation with this book.
All rights reserved. No part of this book, or the ebook files available by download from
Hentzenwerke Publishing, may be reproduced or transmitted in any form or by any means,
electronic, mechanical photocopying, recording, or otherwise, without the prior written
permission of the publisher, except that program listings and sample code files may be entered,
stored and executed in a computer system.
The information and material contained in this book are provided as is, without warranty of
any kind, express or implied, including without limitation any warranty concerning the
accuracy, adequacy, or completeness of such information or material or the results to be
obtained from using such information or material. Neither Hentzenwerke Publishing nor the
authors or editors shall be responsible for any claims attributable to errors, omissions, or other
inaccuracies in the information or material contained in this book. In no event shall
Hentzenwerke Publishing or the authors or editors be liable for direct, indirect, special,
incidental, or consequential damages arising out of the use of such information or material.
ISBN: 1-930919-70-0
Manufactured in the United States of America.

List of Chapters
Chapter 1: Why Client-Server? Why VFP? Why MySQL?
Chapter 2: Development and Deployment Scenarios
Chapter 3: Installing MySQL on Windows
Chapter 4: Installing MySQL on Linux
Chapter 5: Configuration of Users and Hosts
Chapter 6: Connecting VFP to MySQL
Chapter 7: Configuring MySQL
Chapter 8: The Interactive Use of MySQL
Chapter 9: Under the Hood: Where MySQL Keeps Its Data
Chapter 10: Creating Data Sets from Scratch
Chapter 11: Populating a MySQL Database: LOAD DATA INFILE
Chapter 12: Populating a MySQL Database Programmatically
Chapter 13: Advanced Data Issues
Chapter 14: Constructing SQL to Avoid SQL Injection
Chapter 15: Religious Wars: Remote Views, CursorAdapters,
and SQL PassThrough
Chapter 16: A Client-Server State of Mind
Chapter 17: xBase to SQL Conversion Issues
Chapter 18: A Client-Server User Interface for Querying
Chapter 19: A Client-Server User Interface for Add/Edit/Delete
Chapter 20: Relational Integrity
Chapter 21: Getting Started with Stored Procedures
Chapter 22: Deployment

1
17
25
55
75
93
119
151
173
181
211
219
249
263
271
287
295
317
335
369
379
393

vii

Table of Contents
Our Contract with You, The Reader
Acknowledgments
About the Author
How to Download the Files
Introduction
Chapter 1: Why Client-Server? Why VFP? Why MySQL
What is Client-Server?
Why Client-Server?
Data access and security
Backup
Replication
Clustering
Stored procedures
Referential integrity
Scalability
What is VFP?
Why VFP?
Technical reasons
Business reasons
What is MySQL?
Why MySQL?
Technical reasons
Business reasons
Arguments against
FoxPro is dead (or obsolete, or not supported, or...)
Visual FoxPro wont have a native 64 bit version
ODBC is too old/slow
MySQLs SQL is not compliant or standard
Conclusion/Summary

Chapter 2: Development and Deployment Scenarios


The big picture of how pieces fit together
The components
Visual FoxPro
ODBC Drivers
MySQL
The scenarios
Development
Production
Conclusion/Summary

iii
xvii
xix
xxi
xxiii

1
2
3
3
3
3
3
3
4
4
4
5
5
8
9
9
10
11
12
12
13
13
14
15

17
17
17
18
19
19
20
20
23
24

viii

Chapter 3: Installing MySQL on Windows

25

Software and environment


The big picture what installation does
The essential installation process
Read the documentation
Download files
Decide on a target location
Grab the files
Verify the bits
Uninstalling an old MySQL installation
Stop the service
Remove the service
Install the MySQL files with the installer
The Setup Wizard
Instance Configuration Wizard
Potential problems
Firewall troubles
Anti-virus conflicts
Collisions with a prior installation
Installation results
MySQL files have been installed
MySQL service created and started
Conclusion/Summary

25
25
26
26
26
26
27
30
30
30
31
31
31
38
47
47
49
50
52
52
53
54

Chapter 4: Installing MySQL on Linux

55

Software and environment used


The big picture what installation does
The essential installation process
Read the documentation
Download the files
Decide on a download target
Grab the files
Verify the bits
Uninstalling an old version
Install the files
Install the server
Install the client
Set a password for the MySQL root user
Potential problems
Firewall conflicts
Installation with Fedora Core 5/6
Installation results
MySQL files have been installed
The Linux user account is created
Data files are created

55
55
56
56
57
58
58
62
62
64
64
65
66
66
66
67
69
69
70
71

ix

Automatic startup entries are created


The mysqld daemon is started
Conclusion/Summary

Chapter 5: Configuration of Users and Hosts


Work with the server from the command line
Windows and Linux command line tools
Running MySQL scripts
Summing up
The MySQL interactive environment (MySQL monitor)
Enter the MySQL interactive environment
Exit the MySQL interactive environment
A quick tour of the MySQL monitor
Set passwords and new accounts
Quickly adding a user
MySQL password concepts
How MySQL user passwords affect commands
Adding a work-a-day user
Conclusion/Summary

Chapter 6: Connecting VFP to MySQL


Installing the MySQL ODBC driver
Uninstalling an old version of the MySQL ODBC driver
Downloading the MySQL ODBC driver
Installing the MyODBC driver
Connecting to MySQL from VFP
Connecting to MySQL from VFP with a DSN
Connecting to MySQL from VFP with a connection string
DSNs or connection strings?
Initial tests with Visual FoxPro and MySQL
Creating a database
Where is the data created?
Working with tables in our new database
Conclusion/Summary

Chapter 7: Configuring MySQL


The MySQL configuration file
my.ini in Windows
my.cnf in Linux
Downloading the MySQL Administrator
Installing the Administrator
On Windows
On Linux
Running the Administrator
Advanced login options

72
73
74

75
75
75
76
83
83
83
84
85
86
86
87
89
90
91

93
93
93
93
94
99
99
108
114
114
115
116
116
118

119
119
119
121
122
124
124
128
131
133

The Administrator
Editing the my.ini/my.cnf file directly
Specific settings in the MySQL configuration file
Conclusion/Summary

Chapter 8: The Interactive Use of MySQL


Installation of the MySQL Query Browser
Windows download
Linux download
Windows installation
Linux installation
Using the Query Browser
Starting the Query Browser
Startup Errors
Parts of the Query Browser
Functions of the Query Browser
Other Tools
Conclusion/Summary

Chapter 9: Under the Hood: Where MySQL Keeps Its Data

140
147
148
149

151
151
151
152
152
156
156
156
159
161
167
170
171

173

Types of MySQL data


Where is your data?
Windows
Linux
Where is the MySQL metadata?
MyISAM
InnoDB
Where is the MySQL privilege data?
Moving your Windows data out of Program Files
Conclusion/Summary

173
173
174
175
176
176
177
177
179
180

Chapter 10: Creating Data Sets from Scratch

181

Best practices in database design


Relational database theory
Naming conventions
Keys
Time stamps
Overloading tables
Choosing data types
Available data types in MySQL
Comparing MySQL data types to VFP's choices
Making the decision
Creating your database from scratch
Creating a database
Creating tables

181
181
182
183
184
184
185
186
189
190
190
191
192

xi

Field attributes
Indexes
Creating primary keys
Assigning foreign keys
Working with time stamps
End result: empty data set what's it look like?
mysqlshow
SQL commands
Getting data into the database
Conclusion/Summary

195
198
199
200
205
206
206
207
210
210

Chapter 11: Populating a MySQL Database: LOAD DATA INFILE 211


The MySQL LOAD DATA INFILE command
Where do I run the command?
LOAD INFILE Syntax
Handling primary keys
Leading spaces
Date and date-time fields
Handling BLOBS and TEXT fields
Conclusion/Summary

Chapter 12: Populating a MySQL Database Programmatically


Converting DBF data to MySQL
An overview of the process
Setting up the MySQL tables for this chapter
Description of the sample database
Sample source code
A simple program skeleton
The SQLEXEC command
Formatting commands for SQLEXEC
Converting to a string for SQLEXEC
Using variables
Handling multiple data types
Tip: Getting the structure of an existing MySQL table
A better way to format SQL commands for SQLEXEC
Creating the sample MySQL database
Drop the existing tables
Create the tables
A simple, one table migration
Using SQL INSERT to copy data
Putting the INSERT in context
A bit of complexity
Sample migration with multiple data types
Dealing with special characters
Dealing with dates
Handling BIT fields

212
212
213
214
214
214
215
218

219
219
220
220
220
222
223
224
224
224
225
226
227
227
229
229
230
231
231
231
232
234
234
235
236

xii

Adding error trapping to the migration process


Alert about the error
Using AERROR() in a program
What should the program do?
Debugging the error
Not all errors are bad!
Error handling is just the start
Reorganizing the migration code
Handling SQL commands that are executed once
Handling SQL commands that are executed multiple times
Passing long strings
Moving more than data
Performance issues
Conclusion/Summary

Chapter 13: Advanced Data Issues


BIT field operations
Strategic use of BIT fields to replace VFP logicals
A bit arithmetic and functions primer
Define a BIT field
Putting data into a BIT field
New functions for modularizing code
Get data out of a BIT field
To extract just one position
To input just one position
New functions for creating cursors
When to use BLOB fields to store files
Infrastructure
Permanence and incremental backups
File size
Dealing with NULLs
What is a NULL?
Setting up a NULL
ENUM fields
Conclusion/Summary

Chapter 14: Constructing SQL to Avoid SQL Injection


How SQL Injection works
Improperly trapped delimiters
Direct injection: failure to validate data
Quoted injection: failure to trap data type
Database engine defects
Solutions to SQL Injection
Validate the data
Parameterized queries
Additional defenses

236
236
237
239
240
241
242
243
243
244
246
246
247
248

249
249
249
250
252
253
253
254
256
257
257
258
258
258
259
259
259
260
261
261

263
263
264
265
266
266
266
266
267
269

xiii

Conclusion/Summary

Chapter 15: Religious Wars: Remote Views, CursorAdapters,


and SQL PassThrough
Remote Views
How to use
Pros
Cons
CursorAdapter
How to use
Pros
Cons
SQL PassThrough
How to use
Pros
Cons
Decision time
Conclusion/Summary

270

271
271
271
282
282
283
283
284
284
285
285
285
285
285
286

Chapter 16: A Client-Server State of Mind

287

A birds-eye view of a client-server application


Startup
Menu
File card forms
Action-oriented forms
Connecting to the back end
Selecting a subset of records
Writing back to the database
Adding
Deleting
Updating
Closing the connection
Conclusion/Summary

288
288
288
288
290
290
290
291
291
291
291
292
293

Chapter 17: xBase to SQL Conversion Issues


Architecture
Intermixed xBase
SQL commands
Central data access mechanism
Built with views
Command comparison
SQL commands
xBase commands
Function cross reference
Interactive evaluation of MySQL functions

295
295
295
295
295
297
297
297
299
299
300

xiv

Math functions
Lists, comparison, logic functions
Empty/Blank/NULL functions
Data/database functions
String functions
Date functions
System information functions
Miscellaneous functions
Conclusion/Summary

Chapter 18: A Client-Server User Interface for Querying


The logical and physical components
Source code
Base classes
Simple navigation form
Simple navigation with children form
Simple navigation with filter
Simple navigation with MySQL back end
Pass connection parameters to simplenav_withmysql1
Store connection handle in simplenav_withmysql2
Simplenav_withmysql2.init()
CreateConnection()
GetResults()
TableToForm()
Close()
Where do you create connections?
Trade-offs for child and minor entity relationships
Child tables
Minor entities
Conclusion/Summary

301
303
305
305
307
310
314
315
315

317
317
319
319
320
321
322
325
326
326
326
327
327
329
330
331
332
333
333
334

Chapter 19: A Client-Server User Interface for Add/Edit/Delete 335


Creating a class library for client-server functions
Building a simple for to add records
User interface
Internal design
Saving data
A simple two table edit form
User interface
Internal design
Displaying data on the form
Saving data
A simple two table delete form
Denormalized table issues
The Delete() method
An all-in-one form

335
336
336
337
338
339
340
341
341
343
345
346
347
350

xv

Design of an all-in-one form


Internal design
Adding and deleting minor entities
Adding a business
Deleting a business
Adding and deleting business types
Adding and deleting contacts
Tricks and Tips
Retrieving empty dates from MySQL
Retrieving null dates from MySQL
Handling full outer joins in MySQL
Selecting number of records in a result set
Using auto-increment to handle primary key
Determining the last value of an auto-increment field
Conclusion/Summary

Chapter 20: Relational Integrity


Revisiting the interface
Creating foreign key constraints
Using foreign key constraints
Using RI interactively
Using RI in a VFP form
Errors encountered during RI setup
InnoDB only!
Indexes needed on both side of FK expression
Index fields need to be same type, and maybe length
Constraint name needs to be unique
Conclusion/Summary

350
352
354
354
355
357
360
362
362
363
363
365
365
366
367

369
369
371
374
374
375
375
376
376
376
377
377

Chapter 21: Getting Started with Stored Procedures

379

What is a stored procedure


Creating stored procedures in MySQL
Make sure MySQL is set up for stored procedures
Create your very own stored procedure
Run the proc from the MySQL monitor
Create a procedure that accesses a database table
A stored procedure with a parameter
Passing a parameter, somewhat more involved
Using a parameter more constructively
Using input and output parameters
Cleaning up
Where is a stored procedure stored?
Calling a MySQL stored procedure from VFP
Calling your basic stored procedure from VFP
Passing a hard-coded parameter to a stored procedure
Passing a user-provided parameter to a stored procedure

379
379
379
381
382
383
383
384
384
385
385
387
387
388
389
389

xvi

Incorporating stored procedures in the sample app


Conclusion/Summary

Chapter 22: Deployment


Getting started
Development
Build your applications EXE with VFP
Create an installation package for your application
Production server
Install production MySQL server
Configure the production MySQL server user
Install production data
Open firewall for port 3306
Client machine
Install the application
Test connection to the server
Licensing
Internal applications
Consultant
The GPL
What next?
Conclusion/Summary

390
391

393
393
394
394
394
395
395
395
397
400
400
400
400
401
401
402
402
403
403

Chapter 7: Configuring MySQL 119

Chapter 7
Configuring MySQL
MySQLs operation is controlled by a configuration file. You can use a graphical frontend, called the MySQL Administrator, to change this file, or you can edit this file directly.
In this chapter, Ill first show you how to install and use the Administrator. Once youre
comfortable with this tool, Ill cover the essential configuration items youll want to look
at and possibly tweak yourself, and then briefly cover the rest of the settings. Finally, Ill
discuss how to edit the configuration file directly.

The MySQL configuration file is a plain text file that is referenced during the startup of the
MySQL server as well as MySQL client applications. The documentation inside the file is
outstanding, and I urge you to read through it even if youre not planning on ever editing it
directly yourself.
But first, you need to know where to find it.

The MySQL configuration file


The configuration files used by MySQL are structured in a very flexible manner.
Unfortunately, in this situation, flexible is marketing-speak for confusing, because different
platforms use different, but also overlapping, files.

my.ini in Windows
On Windows, MySQL server looks for a file named my.ini. While the my.ini file is typically
in the same directory as the mysql executable, an argument is passed to the MySQL service
command that explicitly identifies where this file is.
On Windows, the syntax looks like this:
"C:\mysql\bin\mysqld-nt" --defaults-file="C:\mysql\my.ini" MySQL

You can find out what my.ini file is being used in your Windows installation via the
Services applet. Click on the Services applet in the Administrative Tools window in Control
Panel to open the Services list, as shown in Figure 1.

120

MySQL Client-Server Applications with Visual FoxPro

Figure 1. Locating the MySQL service in the Services applet.


Right click on the MySQL service and select Properties to bring forward the Properties dialog
as shown in Figure 2.

Chapter 7: Configuring MySQL 121

Figure 2. The location of the my.ini configuration file is in the executable path.
Youll see the configuration file being used by the current MySQL service displayed in
the read-only text box under Path to executable: the text box is read-only because it is
normally modified via manual MySQL commands or through the GUI tool.

my.cnf in Linux
On Linux, MySQL looks for a configuration file named my.cnf located in the /etc directory:
/etc/my.cnf

This file controls global options. Because you can have multiple MySQL databases
running on a single physical machine, you might think it would be convenient to be able to
specify specific configuration settings, and you can. You can specify server-specific options
by placing a copy of this file in the data directory for the server, like so:
/var/lib/mysql/my.cnf

122

MySQL Client-Server Applications with Visual FoxPro

and modifying the file accordingly. Even further fine-tuning is available on a user-by-user
case by placing a copy of this file in the users home directory, like so:
/home/<username>/my.cnf

and setting user-specific options.


To further complicate matters, you can also use my.cnf files on Windows, but at this
point, were venturing into complexities unnecessary for our current purposes. Lets move
along and get into the MySQL Administrator, a GUI tool used to make changes to the
configuration file graphically.

Downloading the MySQL Administrator


The MySQL Administrator is available from the same locations that the MySQL server itself
is from see Figure 3.
Go to http:\\dev.mysql.com, click on the Downloads menu (far left), and navigate down
the page to MySQL Tools. Click on MySQL Administrator, which brings you to the page
shown in Figure 3.

Figure 3. Locating the download link for the MySQL Administrator.

Chapter 7: Configuring MySQL 123

Clicking the link takes you to a list of files for various operating systems, as shown in
Figure 4.

Figure 4. Identifying the specific MySQL Administrator downloads.


Clicking the download link in the Windows section will eventually download a file named
mysql-administrator-1.1.9-win.msi

(or later) to your hard disk; clicking the download link in the SUSE Linux 9.3 section will
download a file named
mysql-administrator-1.1.6-1.suse93.i586.rpm

in a similar manner.

124

MySQL Client-Server Applications with Visual FoxPro

Installing the Administrator


On Windows
Right-click on the .msi file you just downloaded and select the Install option from the
context menu. Doing so displays the starting dialog as shown in Figure 5.

Figure 5. Starting up the Administrator Wizard in Windows.


Clicking the Next button brings you to the license screen, shown in Figure 6.

Figure 6. The Administrator license agreement.

Chapter 7: Configuring MySQL 125

Clicking the Next button takes you to the Destination Folder dialog. At this point, the
default is usually under C:\Program Files if you changed the default installation directory of
MySQL to something like C:\mysql, then I suggest you change the installation directory of the
administrator also so it is still under the main MySQL directory, like so:
c:\mysql\admin1.1

Figure 7 shows the Administrator under Program Files.

Figure 7. Pointing to the installation directory for the Administrator.


Choose the Custom install if you want to control which components are installed. There
are only two; one of which is optional, so its not that big a deal. Still, I usually choose
Custom, as shown in Figure 8, so I can see whats included.

126

MySQL Client-Server Applications with Visual FoxPro

Figure 8. Selecting the type of installation.


Figure 9 shows you the two components the Administrator itself and a System
Tray Monitor, which I recommend you include since it takes virtually no space and is
darn tootin handy.

Figure 9. Selecting the Administrator components.

You might also like