Home arrow Delphi-Kylix arrow Page 5 - Using Delphi with MySQL
DELPHI-KYLIX

Using Delphi with MySQL


Businesses that are strapped for money but need an RDBMS know that MySQL fits the bill. It works well with many programming languages, but what do you do if you need to write a multi-user database application for it in a language that it doesn't support quite as well? That's the challenge our author faced as a Delphi programmer. To see how he tackled it, keep reading.

Author Info:
By: Leidago
Rating: 5 stars5 stars5 stars5 stars5 stars / 22
October 25, 2006
TABLE OF CONTENTS:
  1. · Using Delphi with MySQL
  2. · Required Tools
  3. · Setting up the Connector
  4. · The Application
  5. · Form Two

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Using Delphi with MySQL - Form Two
(Page 5 of 5 )

There are two buttons on form2. The first button inserts a new article and the second button is used to update an article.

Add the following code to the first button:

procedure TForm2.BitBtn1Click(Sender: TObject);
begin
form1.q1.close;
form1.q1.SQL.Text:='INSERT INTO articles (auth,title,article_body,date_published) values
('''+edit1.text+''','''+edit2.Text+''','''+memo1.lines.
Text+''','''+edit3.text+''')';
form1.q1.ExecSQL;
showmessage('New record inserted.');
end;

The code is straightforward. It runs a query that inserts a new article based on the values entered into the form components.

Add the following code to the second button:

procedure TForm2.Button1Click(Sender: TObject);
begin
form1.q1.close;
form1.q1.SQL.Text:='UPDATE articles SET
auth=:aname,title=:atitle,article_body=:sbody,date_published=
:apubdate WHERE aid=:Aid';
form1.q1.Parameters.ParamByName('aname').Value:=edit1.text;
form1.q1.Parameters.ParamByName('atitle').Value:=edit2.text;
form1.q1.Parameters.ParamByName('sbody').Value:=memo1.Lines.Text;
form1.q1.Parameters.ParamByName('apubdate').Value:=edit3.text;
form1.q1.Parameters.ParamByName('Aid').Value:=id;
form1.q1.ExecSQL;
showmessage('All Updated.');
end;

This code is straightforward as well. It only updates the article sent over from form one and sends the updated version back to the database. The important thing here is the "aid" value in the "where" clause of the query. It identifies the article that you want changed. If you don't include a where clause, every article in the database will be overwritten with the values that you intended only for this article. Below is a test run of the application:

Conclusion

This is just a simple example intended to demonstrate how to connect and interact with a MYSQL database. It is not as easy as when creating a single user database, but it gives your applications a lot more performance power. It also makes it possible for you to write thin client applications that can be used on a large network. That is something you cannot do with a single user database.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials