Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ SQL » how to read a dbf
how to read a dbf [message #8198] Sun, 18 February 2007 00:32 Go to next message
forlano is currently offline  forlano
Messages: 1185
Registered: March 2006
Location: Italy
Senior Contributor
Hello,

I need to read the content of a DBF file.
Is there a code snippet that shows how to get a given row of a DBF file?
The class DbfStream I'm sure can do this silly task easily but I do not know how to use it Embarassed

Thank you,
Luigi
Re: how to read a dbf [message #8200 is a reply to message #8198] Sun, 18 February 2007 11:05 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
forlano wrote on Sat, 17 February 2007 18:32

Hello,

I need to read the content of a DBF file.
Is there a code snippet that shows how to get a given row of a DBF file?
The class DbfStream I'm sure can do this silly task easily but I do not know how to use it Embarassed

Thank you,
Luigi


Here is copy&paste of routine that imports .dbf data into Oracle in one of my apps. I hope it helps:

void Eb::Import()
{
...........
	FileSel fs;
#ifdef _DEBUG
	fs.AllFilesType();
	fs.ActiveDir("F:\\DATA");
#endif
	if(!fs.ExecuteSelectDir())
		return;
	DbfStream dbf;

	if(!dbf.Open(AppendFileName(~fs, "EI_ZAKL.DBF"))) {
		Exclamation("Nelze otevøít ei_zakl.dbf");
		return;
	}
	
.........
	Progress pi("Importuji %d");
	pi.SetTotal(dbf.GetRowCount());
	Index<String> nf;
	while(dbf.Fetch()) {
		if(pi.StepCanceled()) return;
		String text;
		int osoba_seq = DoOsoba(dbf["RC"], dbf["JMENO"], dbf["PRIJMENI"], dbf["ROD_PRIJM"],
		                        dbf["ULICE"], dbf["POPCIS"], dbf["OBEC"], dbf["MESTO"],
		                        dbf["PSC"]);
		if(!IsNull(osoba_seq)) {
			int zastupce_seq = Null;
			if(!IsNull(dbf["ZASTPRIJM"])) {
				zastupce_seq = DoOsoba(dbf["ZAST_RC"], dbf["ZASTJMEN"], dbf["ZASTPRIJM"],
				                       dbf["ZASTROD_PR"], dbf["ZASTULICE"],
				                       dbf["ZASTPOPCIS"], dbf["ZASTOBEC"], dbf["ZASTMESTO"],
				                       dbf["ZASTPSC"]);
				if(IsNull(zastupce_seq))
					text << "Zástupce nenalezen: "
					     << dbf["ZAST_RC"] << ", "
					     << dbf["ZASTJMEN"] << " " << dbf["ZASTPRIJM"] << ", "
					     << "roz. " << dbf["ZASTROD_PR"] << ", " << dbf["ZASTULICE"]
					     << dbf["ZASTPOPCIS"] << ", " << dbf["ZASTOBEC"]
					     << " " << dbf["ZASTMESTO"];
			}
			String t = dbf["POSPR_TYP"];
			int stupen = t == "TP" ? 1 : t == "ZTP" ? 2 : t == "ZTP/P" ? 3 : 0;
			String cz = dbf["CISVOZIK"];
			SQL * Insert(SOCI_INVALIDA)
					(SOCI_INVALIDA_SEQ, ++SeqSoci)
					(OSOBA_SEQ, osoba_seq)
					(COP, dbf["OBCPRUK"])
					(DATZAD, dbf["DATZADOST"])
					(ZASTUPCE_OSOBA_SEQ, zastupce_seq)
					(ZASTUPCE_COP, dbf["ZASTPRUK"])
					(DATUM, dbf["DATPRUKOD"])
					(DATUM_DO, dbf["DATPRUKDO"])
					(CISLO, dbf["POSPR_CIS"])
					(STUPEN, stupen)
					(ZNACENI, cz)
					(DRUH, toupper(cz[0]) == 'A' ? 1 : 2)
					(TELEFON, dbf["TELEF"])
					(TEXT, text)
			;
...........

[Updated on: Sun, 18 February 2007 11:07]

Report message to a moderator

Re: how to read a dbf [message #8201 is a reply to message #8200] Sun, 18 February 2007 13:09 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1185
Registered: March 2006
Location: Italy
Senior Contributor
Thanks, it is very useful.
Unfortunately I am not able even to open the file. This simple app:
#include <Core/Core.h>
#include <TCore/dbf.h>

using namespace Upp;

CONSOLE_APP_MAIN
{   DbfStream dbf;
    if(!dbf.Open("C:\\upp\\out\\MSC8\\club.dbf", false) ) {
		return;
	}
    //... to do something
    dbf.Close();
}

crash with error:

index.php?t=getfile&id=463&private=0

Perhaps some mismatch with the internal format of the file I'm trying to read (but I can open it with Excel).
Luigi
  • Attachment: pic2.jpg
    (Size: 7.96KB, Downloaded 663 times)
Re: how to read a dbf [message #8204 is a reply to message #8201] Sun, 18 February 2007 19:21 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Try stack backtrace (latest ide is even able to copy it yo clipboard).

Alternatively, if possible (and not too long), post .dbf and package.
Re: how to read a dbf [message #8205 is a reply to message #8204] Sun, 18 February 2007 19:50 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1185
Registered: March 2006
Location: Italy
Senior Contributor
luzr wrote on Sun, 18 February 2007 19:21

Try stack backtrace (latest ide is even able to copy it yo clipboard).

Alternatively, if possible (and not too long), post .dbf and package.


The dbf file is in the folder package. The package consist of a few code line.
Thanks a lot,

Luigi

PS: this afternoon I tried another library called Shapefil and I was able to read the file, so I would exclude a corrupted file.
  • Attachment: readdbf.rar
    (Size: 33.86KB, Downloaded 287 times)

[Updated on: Sun, 18 February 2007 19:52]

Report message to a moderator

Re: how to read a dbf [message #8214 is a reply to message #8205] Tue, 20 February 2007 08:38 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
A bug in DbfStream related to encoding - unable to cope with UTF8.

Non-perfect quick fix (line 228):

void DbfStream::SetCharset(byte cs)
{
	charset = cs;
	byte dest = GetDefaultCharset();
	if(dest == CHARSET_UTF8)
		dest = CHARSET_WIN1252;
	codepage_cv = false;
	for(int i = 0; i < 256; i++) {
		codepage_uni[i] = ToUnicode(i, charset);
		codepage_map[i] = FromUnicode(codepage_uni[i], dest);
		codepage_rev[i] = FromUnicode(ToUnicode(i, dest), charset);
		if(codepage_map[i] != i)
			codepage_cv = true;
	}
}


Mirek
icon4.gif  Re: how to read a dbf [message #9244 is a reply to message #8198] Mon, 23 April 2007 21:15 Go to previous messageGo to next message
Sbleck is currently offline  Sbleck
Messages: 16
Registered: April 2007
Location: Brazil
Promising Member

Hi,

Quote:


A bug in DbfStream related to encoding - unable to cope with UTF8.

Non-perfect quick fix (line 228):

void DbfStream::SetCharset(byte cs)
{
charset = cs;
byte dest = GetDefaultCharset();
if(dest == CHARSET_UTF8)
dest = CHARSET_WIN1252;
codepage_cv = false;
for(int i = 0; i < 256; i++) {
codepage_uni[i] = ToUnicode(i, charset);
codepage_map[i] = FromUnicode(codepage_uni[i], dest);
codepage_rev[i] = FromUnicode(ToUnicode(i, dest), charset);
if(codepage_map[i] != i)
codepage_cv = true;
}
}

Mirek



How could I use this advice, to check if I'm having the same problem ?

Sven
Re: how to read a dbf [message #9261 is a reply to message #9244] Tue, 24 April 2007 14:00 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Most likely not, this patch is in 2007.1.

Mirek
Previous Topic: Cannot use *.mdb files
Next Topic: small patch for PostgreSQL
Goto Forum:
  


Current Time: Fri May 03 07:53:37 CEST 2024

Total time taken to generate the page: 0.03357 seconds