Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
Databases
»
(SOLVED) DBF add fields into existing table
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
[CLOSED] Hey! What about ...
by
EganSolo
[
Today
at 02:32:39 am]
Something kind of funny a...
by
OC DelGuy
[
Today
at 02:15:21 am]
Newbie in Web Programming...
by
Edgardo M. López
[
Today
at 01:34:43 am]
Why isn't Lazarus / Free ...
by
Lenny33
[
Today
at 01:10:14 am]
TListView - please advis...
by
TRon
[
Today
at 12:01:23 am]
Randomally generated code
by
rvk
[April 28, 2025, 11:06:58 pm]
Updating and redrawing SV...
by
PawelO
[April 28, 2025, 11:00:19 pm]
coded DBUG?
by
440bx
[April 28, 2025, 10:20:02 pm]
EOF Problem
by
OC DelGuy
[April 28, 2025, 09:38:57 pm]
hwnd type error
by
TBMan
[April 28, 2025, 09:10:31 pm]
'Events' Quiry
by
J-G
[April 28, 2025, 07:31:06 pm]
New shader examples for r...
by
Guva
[April 28, 2025, 07:18:51 pm]
Liverpool's wins are fib...
by
Thaddy
[April 28, 2025, 05:47:57 pm]
Why there is no build-in ...
by
Seenkao
[April 28, 2025, 05:41:52 pm]
Lazarus Install Question
by
440bx
[April 28, 2025, 04:28:40 pm]
Spiral search of an image
by
user5
[April 28, 2025, 04:05:34 pm]
[SOLVED] Testing with SQL...
by
1HuntnMan
[April 28, 2025, 03:53:35 pm]
Fpcupdeluxe
by
Philippe1
[April 28, 2025, 03:09:12 pm]
[Solved] Check if URL is ...
by
d2010
[April 28, 2025, 02:23:01 pm]
"Object Oriented Programm...
by
d.ioannidis
[April 28, 2025, 02:22:24 pm]
[solved with GENIUS demo]...
by
Nicole
[April 28, 2025, 01:53:07 pm]
Ctrl-V doesn't work anymo...
by
TRon
[April 28, 2025, 12:30:26 pm]
TStringGrid: what is the ...
by
Handoko
[April 28, 2025, 12:08:57 pm]
UUIDv7 library and demo
by
Thaddy
[April 28, 2025, 11:38:15 am]
macOS 15.4 Breaks All Com...
by
Hansaplast
[April 28, 2025, 11:35:27 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: (SOLVED) DBF add fields into existing table (Read 3257 times)
xinyiman
Hero Member
Posts: 2256
(SOLVED) DBF add fields into existing table
«
on:
February 13, 2019, 11:37:18 am »
Hi I want to add a column to my existing table in a dbf file. What am I doing wrong?
Code: Pascal
[Select]
[+]
[-]
var
MyDBF
:
TDBF
;
dbDefs
:
TDbfFieldDefs
;
begin
MyDBF
:
=
TDBF
.
Create
(
nil
)
;
MyDBF
.
FilePath
:
=
Self
.
PAthDBF
;
MyDBF
.
TableName
:
=
Self
.
TableNameDBF
;
if
MyDBF
.
FindField
(
'ELAB'
)
=
nil
then
begin
// create new field list
MyDBF
.
Exclusive
:
=
True
;
dbDefs
:
=
TDbfFieldDefs
.
Create
(
nil
)
;
try
dbDefs
.
Assign
(
MyDBF
.
DbfFieldDefs
)
;
dbDefs
.
Add
(
'ELAB'
,
ftString
,
1
)
;
MyDBF
.
RestructureTable
(
dbDefs
,
true
)
;
finally
dbDefs
.
Free
;
end
;
end
;
MyDBF
.
Open
;
MyDBF
.
Close
;
«
Last Edit: February 14, 2019, 08:40:41 am by xinyiman
»
Logged
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1
Handoko
Hero Member
Posts: 5415
My goal: build my own game engine using Lazarus
Re: DBF add fields into existing table
«
Reply #1 on:
February 13, 2019, 12:22:49 pm »
I have code to add dbf column. I copy/paste and modify a bit to suit your case:
Code: Pascal
[Select]
[+]
[-]
function
FieldIndex
(
aDBF
:
TDBF
;
const
strField
:
string
)
:
Integer
;
var
i
:
Integer
;
begin
Result
:
=
-
1
;
for
i
:
=
0
to
(
aDBF
.
DbfFieldDefs
.
Count
-
1
)
do
if
(
aDBF
.
DbfFieldDefs
.
Items
[
i
]
.
FieldName
=
strField
)
then
begin
Result
:
=
i
;
Exit
;
end
;
end
;
//...
var
MyDBF
:
TDBF
;
dbDefs
:
TDbfFieldDefs
;
dbDef
:
TDbfFieldDef
;
isNew
:
Boolean
;
begin
MyDBF
:
=
TDBF
.
Create
(
nil
)
;
MyDBF
.
FilePath
:
=
'the_path_to_the_location'
;
MyDBF
.
TableName
:
=
'the_file_name'
;
MyDBF
.
Open
;
dbDefs
:
=
TDbfFieldDefs
.
Create
(
nil
)
;
dbDefs
.
Assign
(
MyDBF
.
DbfFieldDefs
)
;
isNew
:
=
False
;
if
FieldIndex
(
'ELAB'
)
<
0
then
begin
isNew
:
=
True
;
dbDef
:
=
dbDefs
.
AddFieldDef
;
dbDef
.
FieldName
:
=
'ELAB'
;
dbDef
.
Type
:
=
ftString
;
dbDef
.
Size
:
=
1
;
dbDef
.
Required
:
=
False
;
end
;
MyDBF
.
Close
;
if
isNew
then
MyDBF
.
RestructureTable
(
dbDefs
,
True
)
;
dbDefs
.
Free
;
MyDBF
.
Free
end
;
«
Last Edit: February 13, 2019, 12:36:02 pm by Handoko
»
Logged
xinyiman
Hero Member
Posts: 2256
Re: DBF add fields into existing table
«
Reply #2 on:
February 14, 2019, 08:40:29 am »
I replace
dbDef.Type := ftString;
with
dbDef.FieldType := ftString;
But run correctly. Thank you very much.
Logged
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
Databases
»
(SOLVED) DBF add fields into existing table
TinyPortal
© 2005-2018