Forum > Databases

Restructuring a table, adding a field (TSqlite3Dataset)

(1/3) > >>

Zomis:
I want to add a field to my TSqlite3Dataset table, and keeping all the records already in the table.

"levels" is the name of my TSqlite3Dataset.


--- Code: ---  levels.FieldDefs.Add('scr_diffels', ftInteger, 0);
  levels.FieldDefs.Update;
  levels.RefetchData;// i've tried without this as well, no difference

--- End code ---


After the above code, I go through all the records (using level.First and while not levels.EOF do...)

--- Code: ---  levels.Edit;
  levels.FieldByName('scr_diffels').AsInteger:=64;
  levels.UpdateRecord;

--- End code ---

But the field 'scr_diffels' does not exist.
I've also tried adding the field using levels.Fields.Add, but that didn't work because I couldn't set the "DataType" property of the field. And I wonder if it would work using that anyway...

So how to add a field to a table and keeping all the current data in the table?

jozagulikoza:
Dont add fields, tables or databases in your client program. its risky.
For that purpos you have DB tools.

Zomis:
I don't see the riskiness in adding fields, tables, databases in my application, I've already created all those things and it works perfectly (now I just like to add a new field to the existing table). And also, Sqlite doesn't use a server.

So please, any other replies?

jozagulikoza:
Imagine you are working on stocking program and you must add one field to some table. If you do it with stocking program it must ask if field dont exists and creat it. Field dont exists just once. If your program is used 5 years ;-) day after day it is not good idea to do that control.
One level is database administration (it is good to use DB tool, eg. sqliteadmin), the next  level is data manipulation (adding, editing, deleting, sorting,... datas).

Zomis:

--- Quote from: "jozagulikoza" ---Imagine you are working on stocking program and you must add one field to some table. If you do it with stocking program it must ask if field dont exists and creat it. Field dont exists just once. If your program is used 5 years ;-) day after day it is not good idea to do that control.
One level is database administration (it is good to use DB tool, eg. sqliteadmin), the next  level is data manipulation (adding, editing, deleting, sorting,... datas).
--- End quote ---
I just sent you a pm about that. I'd like a solution to my problem in this topic and not "you shouldn't do that - use a DB tool instead", please.

I've tried this code now

--- Code: ---  levels.Close;
  levels.FieldDefs.Add('test', ftInteger, 0, False);
  levels.FieldDefs[levels.FieldDefs.Count-1].CreateField(levels);
  levels.FieldDefs.Update;
  levels.Open;

--- End code ---

And that gave me an error after opening the table, it gives me a SIGSEGV
Address: $005D175C
Procedure: fpc_pchar_to_ansistr

And then it stops on the "S := F.DisplayText;" line in this code in 'DBGrids'

--- Code: ---    case ColumnEditorStyle(aCol, F) of
      cbsCheckBoxColumn:
        DrawCheckBoxBitmaps(aCol, aRect, F);
      else begin
        if F<>nil then begin
          if F.dataType <> ftBlob then
            S := F.DisplayText
          else
            S := '(blob)';
        end else
          S := '';
        DrawCellText(aCol,aRow,aRect,aState,S);
      end;
    end;
--- End code ---

So I'm still not sure about how to add a new field to an existing table.

Navigation

[0] Message Index

[#] Next page

Go to full version