Jump to content

saving data in a database

- - - - -

  • Please log in to reply
7 replies to this topic

#1
ployo60

ployo60

    Newbie

  • Members
  • PipPip
  • 28 posts
hello, this is something I am not at all familiar with. I want to try to make a simple form with 4 edit boxes. 2 at the top and 2 at the bottom. and a button. Basically what I want to do is type a couple of things in the top two boxes that are related to each other. when I have them both filled in I click on the button and it saves this information in a database, preferable an external file (doesnt have to be text, i think it would be better if not). So i can do that a couple of times. Saving from the edit fields into a database. Then when I type one of the words saved in one of the edit fields at the bottom it automatically types the other word in the last edit field. The form should remember to connect to the database everytime its opened so that when i open it another time i can still work the edit fields. can anyone advice me on how to do this? thanks

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Step one: pick a database. There can be minor variations from Oracle to MS SQL to Firebird to MySQL to...
Step two: you'll need to learn SQL for this. One is a SQL Insert statement, the other is a SQL Select statement.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ployo60

ployo60

    Newbie

  • Members
  • PipPip
  • 28 posts
Do you recomend any type of database? It would be great if it could be created from a simple button and if i could create multiple databases with different names of course. I'm not familiar with databases, but I'm just keeping it simple. just add strings and possible integers, also add columns so it can be expanded. any idea?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
It sounds like you're thinking of a database as simply being a file, or being something like an Excel file. It isn't.

Firebird and MySQL are both free. Firebird is pretty easy to install, as is MySQL. The problem is, you will need to learn how to use them.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
ployo60

ployo60

    Newbie

  • Members
  • PipPip
  • 28 posts
Yes I did think that it would only be one file. But I'm not sure an entire database is something I need. It sounds too complicated for the simple thing I want it too do. I only want a "Tstringlist" with 4 values. with a couple of lines and that would be about it. This should be able to be saved and edited to an external file and loaded when wanted. I can't imagine it being so difficult with files and folders. It has nothing to do with servers or internet connections. Just simple lets says Name, age, streetname, number and then about 10 times. And when I search the name i can get the rest of the information too. Do I really need an entire database program for that? If so I will learn it of course and do you have any recomandations on a website to start? thx

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
For that, you may want to use the support for INI files.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Zorfox

Zorfox

    Learning Programmer

  • Members
  • PipPipPip
  • 36 posts
I agree that an ini file would offer more options to you. However if you simply want to use a text file there are many options. You can encrypt the text file if you don't want users seeing the contents by the way.

For a small amount of data you can use an array of records. Let's assume you have 4 edit boxes and a button. Edit1 is attached to the name "field" for the pseudo database we will use. When you enter a name that matches any name in the data it completes the other edit boxes. We can assign this event to the button or better yet, the OnKeyUp event of edit1.

First we need a “database” (I am using that term loosely, this is NOT a database). Create a text file with the following format,

John Doe|32|1234|Elm Street
Jane Doe|36|4321|Oak Avenue
Junior Doe|12|9875|Sesame Street

For this example save it as users.txt in the same directory your project is in. Whola! We have a database. Now we need to load the data into our application. We can use an array of records. First define a record by adding the code below to you interface section ( I usually add these just below all the initial uses clause)…

type

  TUsers = record

    Name: String[25];

    Age: String[3];

    StName: String[30];

    StNum: String[15];

  end;
That is the record we can use. Now we need to assign that record as a variable so we can use it. Instead of one record, we can have as many as we want if we make an array of them. For this example let’s use ten. Add the following code just after the implementation…

var

  Users: Array[1..10] of TUsers;


Now that we have an array of records, let’s fill a few for testing. Add the following code to the form’s OnCreate event. This will load the data into the array of records we created

procedure TForm1.FormCreate(Sender: TObject);

var

  i,x: Integer;

  S: String;

  f: textfile; 

begin

  i := 1;

  x := 0;

Assignfile(f,'users.txt');

 Reset(f);

    while not eof(f) do

      begin

        {Add Names to record}

        Readln(f,s);

        x := Pos('|',S);

        Users[i].Name := Copy(S,1,x-1);

        Delete(S,1,X);


        {Add ages}

        x := Pos('|',S);

        Users[i].age := Copy(S,1,x-1);

        Delete(S,1,X);


        {Add Street number}

        x := Pos('|',S);

        Users[i].StNum := Copy(S,1,x-1);

        Delete(S,1,X);


        {Add Street names}

        x := Pos('|',S);

        Users[i].stName := S;

        inc(i);

      end;

    Closefile(f);


end;

Now we have a database and can use that data in our application. To make edit2, edit3 and edit4 all update it’s text based on the record’s values add this to the OnKeyUp event of edit1 (or button1.click wherever you like)…

procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;

  Shift: TShiftState);

var

  i: integer;

begin

  for i := 1 to 10 do

    begin

      if SameText(Edit1.Text, Users[i].Name) then

        begin

          Edit2.Text := Users[i].Age;

          Edit3.Text := Users[i].stNum;

          Edit4.Text := Users[i].stName;

        end;

    end;

 end;

That about does it for updating edit box’s based on information in the application. It’s a “down and dirty” method but should give you a starting point. Adding a combobox with autocomplete on, adding new records, deleting etc. are all things you can do.

#8
LuthfiHakim

LuthfiHakim

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 765 posts
@Zorfox: Great post!

@ployo60: If you really want to learn pascal programming I recommend to use code samples from Zorfox. That way you will learn a lot on how to do basic things in pascal, such as record manipulation, string manipulation, etc. But if you are really in a hurry, you better use ini files, like WP's suggestion. It's important to learn how to manipulate ini files. Basically it's Windows' default format for configuration file. And it's perfect for your need (easy to handle and fulfill all your current needs), unless you want to learn more basic things.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users