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.
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.