Scanning my INI file and storing the results in a DB

I have an ini file that has two sections marked by brackets []. I would like to retrieve the information and put the results in a SQL 7 database. For Example, in the section of the INIfile of [Install Info] is a field called SerialNumber=ABC123. I want my DB application to retrieve the file (which is going to be stored in a hard coded folder/path) and first to do a lookup on the SN to see it already exists. If in does, added the contents. If not create a new record. Once I get this routine down, I should be able to read the rest of the ini file and add them to the record.
JPersingerAsked:
Who is Participating?
 
RBertoraConnect With a Mentor Commented:
// Hi this should help
// Rob;-)

function DoesExist(SN : String) : Boolean;
var
  Tempquery : TQuery;
begin
  TempQuery := TQuery.Create(Form1);
  TempQuery.DatabaseName := 'YourDatabaseAliasName';
  TempQuery.Sql.Clear;
  TempQuery.Sql.Add('Select from TheNameOfTheTable where SN = ''' + SN + '''');
  TempQuery.Open;
  if TempQuery.RecordCount > 0 then
    result := True
  else
    result := False;
  Tempquery.Free;
end;

function InsertSN(SN,DateOfInstall,ApplicationVersion,CustomFileVersion,DataBaseType,
                   UserSystemName,OSVersion : String) : Boolean;
var
  Tempquery : TQuery;
begin
  TempQuery := TQuery.Create(Form1);
  TempQuery.DatabaseName := 'YourDatabaseAliasName';
  TempQuery.Sql.Clear;
  TempQuery.Sql.Add('Insert into TheNameOfTheTable (SN,DateOfInstall,ApplicationVersion,'+
                   'CustomFileVersion,DataBaseType,UserSystemName,OSVersion) values(''' +
    SN + ''',''' +
    DateOfInstall + ''',''' +
    ApplicationVersion + ''',''' +
    CustomFileVersion + ''',''' +
    DataBaseType + ''',''' +
    UserSystemName + ''',''' +
    OSVersion + ''')' );
  TempQuery.ExecSql;
  TempQuery.Free;
end;



function UpDateSN(SN,DateOfInstall,ApplicationVersion,CustomFileVersion,DataBaseType,
                   UserSystemName,OSVersion : String) : Boolean;
var
  Tempquery : TQuery;
begin
  TempQuery := TQuery.Create(Form1);
  TempQuery.DatabaseName := 'YourDatabaseAliasName';
  TempQuery.Sql.Clear;
  TempQuery.Sql.Add('Update TheNameOfTheTable set ' +
    'DateOfInstall = ''' +  DateOfInstall +
    ''', ApplicationVersion = ''' + ApplicationVersion +
    ''', CustomFileVersion = ''' +  CustomFileVersion +
    ''', DataBaseType = '''  +  DataBaseType +
    ''', UserSystemName = ''' +  UserSystemName +
    ''', OSVersion = ''' +  OSVersion +
    ''' where SN = ''' + SN + '''');

  TempQuery.ExecSql;
  TempQuery.Free;
end;



procedure TForm1.Button1Click(Sender: TObject);
var
  SN , DateOfInstall, ApplicationVersion,  CustomFileVersion,
  DataBaseType,  UserSystemName,  OSVersion : String;

begin

// initialize the variables, there for SN will never be undefined.
  SN := '';
  DateOfInstall :='';
  ApplicationVersion :='';
  CustomFileVersion :='';
  DataBaseType :='';
  UserSystemName :='';
  OSVersion :='';

  SN := IniFile.ReadString('Install Info', 'SerialNumber', '');
  DateOfInstall := IniFile.ReadString('Install Info', 'DateOfInstall', '');
  CustomFileVersion := IniFile.ReadString('Install Info', 'CustomFileVersion', '');
  DataBaseType := IniFile.ReadString('Install Info', 'DataBaseType', '');
  UserSystemName := IniFile.ReadString('Install Info', 'UserSystemName', '');
  OSVersion := IniFile.ReadString('Install Info', 'OSVersion', '');

  if SN = '' then exit; // if there is no entry found in the ini file then exit.

  if DoesExist(SN) = False then //insert it
  begin
    InsertSN(SN,DateOfInstall,ApplicationVersion,CustomFileVersion,DataBaseType,UserSystemName,OSVersion);
  end
  else  // it does exist in the database, but update it non the less
  begin
    UpdateSN(SN,DateOfInstall,ApplicationVersion,CustomFileVersion,DataBaseType,UserSystemName,OSVersion);
  end;
end;
0
 
LischkeCommented:
This one is easy:

var
  IniFile: TIniFile;
  SN: String;

begin
  IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'MyIni.ini');
  try
    SN := IniFile.ReadString('Install Info', 'SerialNumber', '');
    if SN <> '' then ...
  finally
    IniFile.Free;
  end;
end;

Ciao, Mike
0
 
RBertoraCommented:
I can help you but I must admit I
need you to explain better this question.
Rob ;-)
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
RBertoraCommented:
Mike: somehow I don't think thats the end of this question...
Rob ;-)
0
 
JPersingerAuthor Commented:
Actually - we're on the right track..:)
Ok. Now I can read the INI file, but how do I put it in the database field that relates to that part of the ini file?

SN is a string value --- even for the database its a string. So is telling the application that SN is a string is as easy as saying
SNBox.Text := IniFile.ReadString('Install Info', 'SerialNumber', ''); // where SNBox is an DBEdit Box

and then 'posting' that info into the database?

How can I scan the SN to see if the database already has an ABC123 company?
0
 
RBertoraCommented:
What you want to do is something along these lines:

begin
  TempQuery := TQuery.Create(AnOwner);
  TempQuery.DatabaseName := ADatabaseName;
  TempQuery.Sql.Clear;
  TempQuery.Sql.Add('Update mytablename set SN = ''' +
  IniFile.ReadString('Install Info', 'SerialNumber', '')
  + ''' where FileID = ' + Edit1.Text);
  TempQuery.ExecSql;
end;

where edit1.Text is your FileID,

please supply your table definition as we can only speculate
on an answer for you at this stage..

Rob ;-)
0
 
LischkeCommented:
Oops, sorry, I didn't realize that you also want the DB part :-)

What you also need for the DB is TTable.Locate with the SN returned by the code above (if SN <> '').

if either SN = '' or Locate returns False then do TTable.Insert with the new SN else you can use the record found by Locate to do whatever you need.

Ciao, Mike
0
 
JPersingerAuthor Commented:
All fields on the form belongs to the DB group of components.

Here is a copy of the Scema-

SN= char 25
DateOfInstall=DateTime 8
ApplicationVersion=char 10
CustomFileVersion=char 10
DataBaseType=char 20
UserSystemName=char 30
OSVersion=char20

Customer= Char 25 (gets converted looking at the first series of numbes from the SN (ex: a standard SN looks like this:
00000-100110-9000-90ABC is us- because the 00000 = us)
0
 
RBertoraCommented:
Mike you take this one...
Rob ;-)
0
 
LischkeCommented:
Now I do something I've never done before :-), but I only have left 3 minutes here in my office. Not enough time to work out the solution. Rob, I'd be glad if you could do this and earn the points. Hint: it seems a normal TTable is not enough here as you will have to lookup a partial string (the SN from the ini file in the SN field of the table).

Ciao, Mike
0
 
JPersingerAuthor Commented:
>>Now I do something I've never done before :-),

Well that makes at least two of us :-)
0
 
RBertoraCommented:
Ok look at the following, and tell me if you can figure
out a solution from there..
Rob ;-)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  IniFiles, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Inifile : TInifile;


implementation

{$R *.DFM}


function DoesExist(SN : String) : Boolean;
var
  Tempquery : TQuery;
begin
  TempQuery := TQuery.Create(Form1);
  TempQuery.DatabaseName := 'YourDatabaseAliasName';
  TempQuery.Sql.Clear;
  TempQuery.Sql.Add('Select from TheNameOfTheTable where SN = ''' + SN + '''');
  TempQuery.Open;
  if TempQuery.RecordCount > 0 then
    result = True
  else
    result := False;
  Tempquery.Free;
end;

function InsertSN(SN,ApplicationVersion : String) : Boolean;
var
  Tempquery : TQuery;
begin
  TempQuery := TQuery.Create(Form1);
  TempQuery.DatabaseName := 'YourDatabaseAliasName';
  TempQuery.Sql.Clear;
  TempQuery.Sql.Add('Insert into TheNameOfTheTable (SN,ApplicationVersion) values(''' +
    SN + ''',''' + ApplicationVersion + ''')' );
  TempQuery.ExecSql;
  TempQuery.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  SN : String;
  ApplicationVersion : String;
begin
  SN := IniFile.ReadString('Install Info', 'SerialNumber', '');
  ApplicationVersion := IniFile.ReadString('Install Info', 'ApplicationVersion', '');
  if DoesExist(SN) = False then //insert it
  begin
    InsertSN(SN,ApplicationVersion);
  end;
end;

(*
SN= char 25
DateOfInstall=DateTime 8
ApplicationVersion=char 10
CustomFileVersion=char 10
DataBaseType=char 20
UserSystemName=char 30
OSVersion=char20 *)


procedure TForm1.FormActivate(Sender: TObject);
begin
  IniFile := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'MyIni.ini');
end;

end.


0
 
JPersingerAuthor Commented:
Getting close. I do get a truncating error. Do I need a trim statement? If so where?

Here:

TempQuery.Sql.Add((Trim'Insert into dbo.Version (SN,ApplicationVersion) values(''' +
    SN + ''',''' + ApplicationVersion + ''')' ));

Could this be the Ini file is returning the entire line for which the SN is on?
0
 
JPersingerAuthor Commented:
Ok. So I added the TRIM in my last comment to the wrong place (what was I thinking.. ). So we can disregard that comment(unless you want to make fun of me). But I do get this message: Return value of InsertSN might be undefined. can you help with that one? And I thing I need RBertora to send a grade sheet.
0
 
JPersingerAuthor Commented:
Everything working well. (Have some DB issues that I guess I need to learn how to do 0 like linking one table with a customer field and another table with a customer table). RBertora - Do you have a source of how I can learn some SQL statements like you used?
0
 
RBertoraCommented:
I use Sql books online , which comes with MSSql server... but you can use
dbddesk.hlp which comes with delphi it has a section on database queries in the using SQL section..

Rob ;-)
0
All Courses

From novice to tech pro — start learning today.