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
Forum Rules
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
How to get at files that ...
by
tfurnivall
[
Today
at 10:28:56 pm]
Need help converting a C+...
by
jamie
[
Today
at 10:27:56 pm]
make cycle fails on macOS...
by
Thausand
[
Today
at 10:05:00 pm]
Matching video to form
by
Thausand
[
Today
at 09:48:04 pm]
[Solved] Need help with c...
by
Thausand
[
Today
at 08:03:53 pm]
TTagEdit Component 1.2
by
JD
[
Today
at 08:02:41 pm]
[SOLVED] progress dialog ...
by
cdbc
[
Today
at 07:55:29 pm]
[SOLVED] Form OnTop not w...
by
Pe3s
[
Today
at 07:32:12 pm]
Save record to BLOB Field
by
Sc0li0sis
[
Today
at 07:31:24 pm]
$ifdef highlighting
by
440bx
[
Today
at 07:09:02 pm]
Convert string with Key-V...
by
Warfley
[
Today
at 05:29:37 pm]
Clipboard Formats
by
AlexTP
[
Today
at 04:39:19 pm]
Lazarus Bugfix Release 4...
by
BSaidus
[
Today
at 04:17:24 pm]
[BUG REPORT] Code explore...
by
Martin_fr
[
Today
at 02:54:20 pm]
Qt6 / X11: problem with G...
by
paweld
[
Today
at 02:44:07 pm]
Dumb Problems Contest!
by
Thaddy
[
Today
at 02:41:59 pm]
Please ask about the valu...
by
zzzzzzz7
[
Today
at 02:14:52 pm]
laz_xmlstreaming.pas fail...
by
Ryan J
[
Today
at 01:00:52 pm]
Probleme installing Lazar...
by
DonAlfredo
[
Today
at 09:05:41 am]
Wikipedia pollution
by
Thaddy
[
Today
at 08:31:35 am]
How to register a windows...
by
Thaddy
[
Today
at 08:24:15 am]
TurboBird IBX
by
rvk
[
Today
at 07:10:47 am]
append new record to arra...
by
speter
[
Today
at 12:46:02 am]
Corrupted strings
by
Aruna
[December 06, 2025, 08:25:44 pm]
Lazarus Trunc & macOS Tah...
by
Thaddy
[December 06, 2025, 05:00:57 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: (SOLVED) DBF add fields into existing table (Read 3404 times)
xinyiman
Hero Member
Posts: 2259
(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: 5506
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: 2259
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