Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Scanning my INI file and storing the results in a DB

Posted on 1999-10-22
16
Medium Priority
?
168 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:JPersinger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 3
16 Comments
 
LVL 10

Expert Comment

by:Lischke
ID: 2149537
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
 
LVL 7

Expert Comment

by:RBertora
ID: 2149538
I can help you but I must admit I
need you to explain better this question.
Rob ;-)
0
 
LVL 7

Expert Comment

by:RBertora
ID: 2149567
Mike: somehow I don't think thats the end of this question...
Rob ;-)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:JPersinger
ID: 2149624
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
 
LVL 7

Expert Comment

by:RBertora
ID: 2149650
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
 
LVL 10

Expert Comment

by:Lischke
ID: 2149655
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
 

Author Comment

by:JPersinger
ID: 2149761
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
 
LVL 7

Expert Comment

by:RBertora
ID: 2149774
Mike you take this one...
Rob ;-)
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2149833
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
 

Author Comment

by:JPersinger
ID: 2149884
>>Now I do something I've never done before :-),

Well that makes at least two of us :-)
0
 
LVL 7

Expert Comment

by:RBertora
ID: 2149909
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
 

Author Comment

by:JPersinger
ID: 2150427
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
 

Author Comment

by:JPersinger
ID: 2150645
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
 
LVL 7

Accepted Solution

by:
RBertora earned 400 total points
ID: 2154994
// 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
 

Author Comment

by:JPersinger
ID: 2159242
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
 
LVL 7

Expert Comment

by:RBertora
ID: 2159279
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question