Recent

Author Topic: [HELP] Connecting to a SQL - Console Application  (Read 34420 times)

nobodyknowsme

  • Full Member
  • ***
  • Posts: 114
[HELP] Connecting to a SQL - Console Application
« on: December 16, 2014, 04:02:41 pm »
I want to connect my console application to my SQL database. I have my SQL database set up. How would I connect or link my website? In my SQL database I have a table storing all my users and such.

I've tried to use this code but it isn't recognizing TSQLConnection, also this is for the offline SQL database. Are there any good ways of connecting my console application to my database easily?

Website where I got the following code from: http://wiki.freepascal.org/SqlDBHowto/nl

 
The code from the website. I basically just want to link my console application with my SQL database. Maybe link my users so that they can sign in the application using there website details.

 
Code: [Select]
Program ConnectDB
 
 var AConnection : TSQLConnection;
 
 Procedure CreateConnection;
 begin
   AConnection := TIBConnection.Create(nil);
   AConnection.Hostname := 'localhost';
   AConnection.DatabaseName := '/opt/firebird/examples/employee.fdb';
   AConnection.UserName := 'sysdba';
   AConnection.Password := 'masterkey';
 end;
 
 begin
   CreateConnection;
   AConnection.Open;
   if Aconnection.Connected then
     writeln('Succesful connect!')
   else
     writeln('This is not possible, because if the connection failed, an exception should be raised, so this code would not be executed');
   AConnection.Close;
   AConnection.Free;
 end.
« Last Edit: December 16, 2014, 04:05:33 pm by nobodyknowsme »

nobodyknowsme

  • Full Member
  • ***
  • Posts: 114
Re: [HELP] Connecting to a SQL - Console Application
« Reply #1 on: December 16, 2014, 04:07:33 pm »
EDIT: I have added sqldb to the uses and it now recognises TSQLConnection however it will now not recognize TIBConnection.Create(nil);. Any idea why?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: [HELP] Connecting to a SQL - Console Application
« Reply #2 on: December 16, 2014, 04:22:31 pm »
EDIT: I have added sqldb to the uses and it now recognises TSQLConnection however it will now not recognize TIBConnection.Create(nil);. Any idea why?
That's because TIBConnection is in IBConnection (and not sqldb). So adding IBConnection to your uses will solve that.

You could also define AConnection as TIBConnection (instead of TSQLConnection).

nobodyknowsme

  • Full Member
  • ***
  • Posts: 114
Re: [HELP] Connecting to a SQL - Console Application
« Reply #3 on: December 17, 2014, 09:49:38 am »
I have done that and I have added my websites SQL database and it wont work now on "AConnection.Open;"


The code is attached below. Any ideas why it breaks at AConnection.Open? Will this work and link with my online SQL database on my website?

Code: [Select]
program project1;

uses sqldb,IBConnection;

var AConnection : TIBConnection;

Procedure CreateConnection;
begin
  AConnection := TIBConnection.Create(nil);
  AConnection.Hostname := 'mysq16.000webhost.com';
  AConnection.DatabaseName := 'a3762836_issue2';
  AConnection.UserName := 'a3762836_issue2';
  AConnection.Password := '******';
end;

begin
  CreateConnection;
  AConnection.Open;
  if Aconnection.Connected then
    writeln('Successful connect!')
  else
    writeln('This is not possible, because if the connection failed, ' +
            'an exception should be raised, so this code would not ' +
            'be executed');
  AConnection.Close;
  AConnection.Free;
end.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: [HELP] Connecting to a SQL - Console Application
« Reply #4 on: December 17, 2014, 10:00:47 am »
Your code works correctly (I just tested it).

Some thoughts as to why it doesn't work for you:
  • You don't give a complete path to your database a3762836_issue2. You should always specify the exact path and filename of the database so the database can connect to it.
  • If mysq16.000webhost.com is another computer/server are you sure the port for the firebird/interbase database-engine is open. (firewall?)

Is above points are not your problem maybe you could specify the exact error you're getting.

nobodyknowsme

  • Full Member
  • ***
  • Posts: 114
Re: [HELP] Connecting to a SQL - Console Application
« Reply #5 on: December 17, 2014, 12:20:21 pm »
How would I give an exact path? In my SQL database I have a table called users. How would I then get that exact directory? What hosting details did you test it with??

Also I am not using firebird. I just want it to link to my SQL database online.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: [HELP] Connecting to a SQL - Console Application
« Reply #6 on: December 17, 2014, 12:43:25 pm »
How would I give an exact path? In my SQL database I have a table called users. How would I then get that exact directory? What hosting details did you test it with??
Also I am not using firebird. I just want it to link to my SQL database online.
If you're not using Firebird then why are you using TIBConnection? This is a Firebird specific connection component.
(I have a local Firebird-database running to which i connected.)

What kind of SQL-database do you have. MS-SQL, MySql or other ??

For example if you use something different from Firebird you could use the TSQLConnector. This component has a ConnectorType which you could set to MSSQLServer, Oracle, and all kind of MySQL versions. With MySql you (normally) wouldn't need to specify a path with your databasename.

Example:

Code: [Select]
program project1;

uses
  sqldb,
  mysql55conn;

var
  AConnection: TSQLConnector;

  procedure CreateConnection;
  begin
    AConnection := TSQLConnector.Create(nil);
    AConnection.ConnectorType := 'MySQL 5.5';
    AConnection.Hostname := 'mysq16.000webhost.com';
    AConnection.DatabaseName := 'a3762836_issue2';
    AConnection.UserName := 'a3762836_issue2';
    AConnection.Password := '******';
  end;

begin
  CreateConnection;
  AConnection.Open;
  if Aconnection.Connected then
    writeln('Successful connect!')
  else
    writeln('This is not possible, because if the connection failed, ' +
      'an exception should be raised, so this code would not ' +
      'be executed');
  AConnection.Close;
  AConnection.Free;
  readln;
end.

Note the addition of mysql55conn the your uses-clause. Although nothing is used in the source it's still needed for the ConnectionType := 'MySQL 5.5'; otherwise you get an error that you're using an unknown driver. You also need the .dll installed in your program directory (or searchpath). If your using something else than MySQL 5.5 you'll need to adjust the mysql55conn accordingly.

nobodyknowsme

  • Full Member
  • ***
  • Posts: 114
Re: [HELP] Connecting to a SQL - Console Application
« Reply #7 on: December 17, 2014, 03:06:14 pm »
That sounds really good however the same error is still occurring. It breaks at AConnection.Open

It wont get passed AConnection.Open at all. Also this is better as I do not want to use FireBird.

It doesn't break here:

Code: [Select]
program project1;

uses sqldb,IBConnection;

var AConnection : TIBConnection;

Procedure CreateConnection;
begin
  AConnection := TIBConnection.Create(nil);
  AConnection.Hostname := 'mysq16.000webhost.com';
  AConnection.DatabaseName := 'a3762836_issue2';
  AConnection.UserName := 'a3762836_issue2';
  AConnection.Password := '******';
end;

begin
  CreateConnection;

  if Aconnection.Connected then
    writeln('Successful connect!')
  else
    writeln('This is not possible, because if the connection failed, ' +
            'an exception should be raised, so this code would not ' +
            'be executed');
  AConnection.Close;
  AConnection.Free;
  readln;
end.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: [HELP] Connecting to a SQL - Console Application
« Reply #8 on: December 17, 2014, 03:15:35 pm »
That sounds really good however the same error is still occurring. It breaks at AConnection.Open
It wont get passed AConnection.Open at all. Also this is better as I do not want to use FireBird.
It doesn't break here:
Code: [Select]
  AConnection := TIBConnection.Create(nil);
I'm not sure what you're trying to accomplish with your last code. You're still using TIBConnection and create a connection-instance with a Firebird server (TIBConnection is only for Firebird) but you don't connect it. Of course you don't get an error in that case. If you don't do a .Open there is nothing to report. But you don't get connected either.

With the code I showed you... what error do you get on the AConnection.Open line ??

You also didn't answer amy of my other questions.
What kind of database server is installed on that host?
What version of the database server is installed on that host?

One thing is for sure... If it's not a Firebird database engine that's installed on that host you can't use TIBConnection.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: [HELP] Connecting to a SQL - Console Application
« Reply #9 on: December 17, 2014, 03:25:02 pm »
I just checked and webhost mysq16.000webhost.com reports back that it expects that you use MySQL 4.1.18 drivers.
Quote
TMySQL55Connection can not work with the installed MySQL client version: Expected (5.5), got (4.1.18).

So you need to specify that you're using MySql 4.1

Code: [Select]
program project1;

uses
  sqldb,
  mysql41conn;

var
  AConnection: TSQLConnector;

  procedure CreateConnection;
  begin
    AConnection := TSQLConnector.Create(nil);
    AConnection.ConnectorType := 'MySQL 4.1';
    AConnection.Hostname := 'mysq16.000webhost.com';
    AConnection.DatabaseName := 'a3762836_issue2';
    AConnection.UserName := 'a3762836_issue2';
    AConnection.Password := '******';
  end;

begin
  CreateConnection;
  AConnection.Open;
  if Aconnection.Connected then
    writeln('Successful connect!')
  else
    writeln('This is not possible, because if the connection failed, ' +
      'an exception should be raised, so this code would not ' +
      'be executed');
  AConnection.Close;
  AConnection.Free;
  readln;
end.

B.T.W. did you install the libmysql.dll in your program directory ?

nobodyknowsme

  • Full Member
  • ***
  • Posts: 114
Re: [HELP] Connecting to a SQL - Console Application
« Reply #10 on: December 17, 2014, 05:22:11 pm »
I haven't installed anything no. Where would I download this file?? Is it needed?

nobodyknowsme

  • Full Member
  • ***
  • Posts: 114
Re: [HELP] Connecting to a SQL - Console Application
« Reply #11 on: December 17, 2014, 07:56:44 pm »

nobodyknowsme

  • Full Member
  • ***
  • Posts: 114
Re: [HELP] Connecting to a SQL - Console Application
« Reply #12 on: December 17, 2014, 08:05:47 pm »
I have downloaded that file. What do I do with it? Where do I put it?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: [HELP] Connecting to a SQL - Console Application
« Reply #13 on: December 17, 2014, 08:10:07 pm »
I have downloaded that file. What do I do with it? Where do I put it?
You can put in in your program directory. (The one with your executable)

nobodyknowsme

  • Full Member
  • ***
  • Posts: 114
Re: [HELP] Connecting to a SQL - Console Application
« Reply #14 on: December 17, 2014, 08:12:14 pm »
Its in the folder with my exe and I run it and it still is giving me the same error.

http://screenshu.com/static/uploads/temporary/ub/bl/du/e2osm7.jpg

Any ideas?
« Last Edit: December 17, 2014, 08:14:11 pm by nobodyknowsme »

 

TinyPortal © 2005-2018