#1 2014-07-24 09:05:50

chula
Member
Registered: 2014-06-18
Posts: 19

What is the correct way to rename a table/field without loosing data?

Renaming table/field name in Object definition just create new table/field. Changing field type, say change Integer to RawUTF8, gave nothing happen at all.

What is the correct way to do this? I read from other post someone execute "Alter Table" before CreateMissingTables(). Is it the correct way? Thanks!

Last edited by chula (2014-07-24 09:08:44)

Offline

#2 2014-07-24 09:24:12

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: What is the correct way to rename a table/field without loosing data?

If you use SQLite as the backend databae, SQLite itself doesn't support renaming a field.

Assume you have TSQLOldTable, now create a new class called TSQLNewTable with the new field definitions, call CreateMissingTables(); Then read old records from TSQLOldTable and add to TSQLNewTable;

drop tale  TSQLOldTable.

Then change all references to TSQLOldTable in your code to TSQLNewTable.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#3 2014-07-25 02:40:25

chula
Member
Registered: 2014-06-18
Posts: 19

Re: What is the correct way to rename a table/field without loosing data?

edwinsn wrote:

If you use SQLite as the backend databae, SQLite itself doesn't support renaming a field.

Assume you have TSQLOldTable, now create a new class called TSQLNewTable with the new field definitions, call CreateMissingTables(); Then read old records from TSQLOldTable and add to TSQLNewTable;

drop tale  TSQLOldTable.

Then change all references to TSQLOldTable in your code to TSQLNewTable.


Very thanks!

As I am new to the framework, do I have to copy the table by looping read/write progress, or there is a more effective way provided from the framework?

Offline

#4 2014-07-25 06:27:28

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,206
Website

Re: What is the correct way to rename a table/field without loosing data?

This is a sqlite limitation.

Offline

#5 2014-07-26 02:43:55

chula
Member
Registered: 2014-06-18
Posts: 19

Re: What is the correct way to rename a table/field without loosing data?

ab wrote:

This is a sqlite limitation.


I see. Thanks!

Offline

#6 2014-07-26 05:41:04

edwinsn
Member
Registered: 2010-07-02
Posts: 1,215

Re: What is the correct way to rename a table/field without loosing data?

chula wrote:
edwinsn wrote:

If you use SQLite as the backend databae, SQLite itself doesn't support renaming a field.

Assume you have TSQLOldTable, now create a new class called TSQLNewTable with the new field definitions, call CreateMissingTables(); Then read old records from TSQLOldTable and add to TSQLNewTable;

drop tale  TSQLOldTable.

Then change all references to TSQLOldTable in your code to TSQLNewTable.


Very thanks!

As I am new to the framework, do I have to copy the table by looping read/write progress, or there is a more effective way provided from the framework?

Maybe execute a raw "insert into...select" SQL statement using EngineExecute() is faster and less of a hassle. Just an idea, I don't remember the details, please google yourself.


Delphi XE4 Pro on Windows 7 64bit.
Lazarus trunk built with fpcupdelux on Windows with cross-compile for Linux 64bit.

Offline

#7 2014-07-26 11:17:58

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,206
Website

Re: What is the correct way to rename a table/field without loosing data?

Yes, SQLite3 has the INSERT INTO .... SELECT syntax.
See http://www.sqlite.org/lang_insert.html

You have also the CREATE TABLE .. AS SELECT statement.
See http://www.sqlite.org/lang_createtable.html

Then DROP the previous table, and run ALTER TABLE ... RENAME TO ... to replace the previous table.

It should be faster to use those kind  of syntax.
But only for Sqlite3 backend.

If you use external databases, pure CRUD / ORM within a BATCH process (and a transaction) is preferred.
And could be only slightly slower.

Ensure you create the TRIGGERs and INDEXs after having populated the new database.
It is most of the time faster to do so.

Offline

Board footer

Powered by FluxBB