1 (edited by v_pozidis 2022-02-19 01:40:08)

Topic: new records and tables without loosing our records

Hi all, I face a problem which is the following, I have added in my sqlite database new columns and new tables which in some cases are Related. How can I add from my old database the records to the new one using script code ? When I  copy the old sqlite database it crash in the folder of the program. So my idea was to fix a small program that will add in the  old sqlite database  the new columns and tables with the relations.  Or is there another way? In any case its a good time to learn how to convert our database with new tables and columns.
Please your help.

Re: new records and tables without loosing our records

v_pozidis wrote:

Hi all, I face a problem which is the following, I have added in my sqlite database new columns and new tables which in some cases are Related. How can I add from my old database the records to the new one using script code ? When I  copy the old sqlite database it crash in the folder of the program. So my idea was to fix a small program that will add in the  old sqlite database  the new columns and tables with the relations.  Or is there another way? In any case its a good time to learn how to convert our database with new tables and columns.
Please your help.

I'm thinking you already deployed your application to your client or you are using it.

I handle problem like this by saving a version number to the database (something like `settings`.`dbVersion`). Then, I created script to handle the alter queries. You need to carefully document each changes in your database

From the script.pas, I am indicating a CONST variable e.g.

CONST DBVER = 2; // this is the current version on the development database. I always change this manually on the dev database to avoid error when developing.

Then, executing the db revision checking on the begin end. area

procedure DBREVISION();
var currVer: Integer;
begin
    currVer := := SQLExecute('SELECT COALESCE(dbVersion, 0) AS currVer FROM settings WHERE 1 LIMIT 1');
    if currVer < DBVER then
    begin
        if DBVER < 1 then
        begin
            // alteration of the version 1
        end;
        if DBVER < 2 then
        begin
            // alteration of the version 2
        end;
        SQLExecute('UPDATE `settings` SET dbVersion='+IntToStr(DBVER));
    end;
end;

begin
    DBREVISION;
end.

I hope this can give you an idea on how handle your problem

brian

3 (edited by v_pozidis 2022-02-19 10:05:50)

Re: new records and tables without loosing our records

Thank you for your answer but what I meant is how can I add tables or modify columns or add new columns in a table and how can I do relationships between tables using script. (SQLEXECUTE ....)

Re: new records and tables without loosing our records

You can add new tables, columns and relationships to your database in the "tables" tab of your MVD project.
It is highly recommended to make a copy of your database or export data from it. In some cases, you may
receive a message that since the database has been created, you must specify the default value.
Another way is that you can create a new database with the necessary changes in your project and
MVD import the old data (saved earlier). In any case, the information about your tables and columns
must match their description in the tables.ini file. Otherwise, the program will not work correctly.

5 (edited by v_pozidis 2022-02-19 15:46:25)

Re: new records and tables without loosing our records

I know that I can select them from the tab. I need to know the commands to do it b y script. Like the following script sqlexecute(update tablename set columnnameboolean = '0' where id > 0). I do not know the commands to add a new table, to add new columns with relationships... All this because my database sqlite has many records and I need to include them in the new updated database (with the new table and comumns)

Re: new records and tables without loosing our records

I do not understand your difficulties.
In any case, you can find the commands in the SQLite documentation.

Re: new records and tables without loosing our records

Its a simply question ... How can I create a tabe  using script and not the tab "table". Realy  I can not understand the confusion .

Re: new records and tables without loosing our records

Hello v_pozidis, Hello sparrow

Can this link help you to create your table by script ?

https://stackoverflow.com/questions/359 … -fielddefs

JB

Re: new records and tables without loosing our records

v_pozidis

this other link is also interesting :

https://www.sqlitetutorial.net/sqlite-create-table/

JB

10 (edited by v_pozidis 2022-02-19 18:52:01)

Re: new records and tables without loosing our records

I found out how to create by script a table with the columns but I can not understand how to insert a FOREIGN KEY
here is my script  sqlexecute('CREATE TABLE Alfa (Alfa integer PRIMARY KEY, mera DATE, phone TEXT, Comment TEXT');
ALFA is the name of the table and the FOREIGN KEY that I like to include in the table  is the id_test from the Table Test
your help pls