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 » DBF crash error while writing record
DBF crash error while writing record [message #43812] Thu, 23 October 2014 19:25 Go to next message
forlano is currently offline  forlano
Messages: 1189
Registered: March 2006
Location: Italy
Senior Contributor
Hello,

I know that DBF format is no longer popular. Unfortunately I had to deal with it and get rid a very old Xbase library.
I created a very simple application that convert a text file in a dbf file. It links and run, but crash while tempting to write.
The DBF db is created reading the info contained in the txt file that look like this:
8
D_EVENT_ID    C    9
D_SEC_NUM     C    2
D_PAIR_NUM    C    4
D_REC_SEQ     C    1
D_MEM_ID      C    8
D_RND01       C    5
D_RND02       C    5
D_RND03       C    5
023434001	1	1	1	156756756	W   4	L   2	W   6
023434001	1	2	1	456456456	W   5	W   1	L   3
023434001	1	3	1	567567567	D   6	W   4	W   2
023434001	1	4	1	324234234	L   1	L   3	W   5
023434001	1	5	1	234234234	L   2	D   6	L   4
023434001	1	6	1	123123455	D   3	D   5	L   1

That is I read the number of fields, how they are made and then the records (fields separated by '\t').
A test case is attached with the txt files to be converted. Here is the simple program so that you can preview it:

#include <CtrlLib/CtrlLib.h>
#include <plugin/dbf/dbf.h>

using namespace Upp;
void ConvertDB(String fn);

GUI_APP_MAIN
{
  ConvertDB("thexport");
  ConvertDB("tsexport");
  ConvertDB("tdexport");
}

void ConvertDB(String fn)
{   
    String infile = fn + ".txt";
	String outfile = fn + ".dbf";  
    FileIn in;
    in.Open(infile);
    if (in.IsError()) {
     	Exclamation("Error reading the file " + infile);
	    exit(0);
    }

    int numFields = StrInt(in.GetLine()); // read numebr of fields
    int i;
	Array<DbfStream::Field> fields;
    for (i=0; i<numFields; i++) 
     	{   // legge le specifiche dei campi
	        Vector<String> f = Split(in.GetLine()," ");
	        //Exclamation(f[0]+"-"+f[1]+"-"+f[2]);
 	        if      ( f[1]=="C") fields << DbfStream::Field::Text(f[0], StrInt(f[2]) );
 	        else if ( f[1]=="N") fields << DbfStream::Field::Number(f[0], StrInt(f[2]), 0);
 	        else if ( f[1]=="D") fields << DbfStream::Field::Date(f[0]);
 	        else Exclamation("error");
     	} 
      	
	DbfStream output; //create DB
	bool isOpened = output.Create(outfile, fields, CHARSET_CP852);
	if(!isOpened)
	{
		PromptOK("Error creating file [* \1" + outfile + "\1].");
		exit(1);
	}

	while (in.IsOK()) //continue to read the txt file and write the dbf 
	{
		Vector<Value> values;
		Vector<String> f = Split( in.GetLine(), "\t", false);
		if (f.GetCount()<=1) break;

		for (int k=0; k<f.GetCount(); k++) values.Add(f[k]);
		output.WriteRow(values); // <----- ??? crash program
	}

	output.Close();
	in.Close();
}


Thanks,
Luigi
  • Attachment: DBF.rar
    (Size: 1.89KB, Downloaded 278 times)
Re: DBF crash error while writing record [message #43823 is a reply to message #43812] Wed, 29 October 2014 10:09 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13984
Registered: November 2005
Ultimate Member
Hi,

the problem seems to be is that you are passing "String" Values for "double" ("N") columns. It crashes on Value conversion.

You will have to convert texts into correct Value types to make it work.

Mirek
Re: DBF crash error while writing record [message #44617 is a reply to message #43823] Sat, 25 April 2015 11:08 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1189
Registered: March 2006
Location: Italy
Senior Contributor
Hi Mirek,

mirek wrote on Wed, 29 October 2014 10:09
Hi,

the problem seems to be is that you are passing "String" Values for "double" ("N") columns. It crashes on Value conversion.

You will have to convert texts into correct Value types to make it work.


At last I solved the problem with "N" field (just use Integer field and cast the String with StrInt() ) but now appear problems for "D", date field.
How can I convert the String to Date and add it correctly in
values.Add( ... );
I have not find any method to cast a string in the date format used by dbfstream.

Thanks,
Luigi
Re: DBF crash error while writing record [message #44620 is a reply to message #44617] Sun, 26 April 2015 13:59 Go to previous message
mirek is currently offline  mirek
Messages: 13984
Registered: November 2005
Ultimate Member
forlano wrote on Sat, 25 April 2015 11:08
Hi Mirek,

mirek wrote on Wed, 29 October 2014 10:09
Hi,

the problem seems to be is that you are passing "String" Values for "double" ("N") columns. It crashes on Value conversion.

You will have to convert texts into correct Value types to make it work.


At last I solved the problem with "N" field (just use Integer field and cast the String with StrInt() ) but now appear problems for "D", date field.
How can I convert the String to Date and add it correctly in
values.Add( ... );
I have not find any method to cast a string in the date format used by dbfstream.

Thanks,
Luigi


Well, depends on date format... Use some combo of .Mid, atoi, StrInt .....
Previous Topic: Bugs with svn build 8144 and NOAPPSQL
Next Topic: How to display fields from different tables in one SQLArray
Goto Forum:
  


Current Time: Thu Jun 13 19:36:11 CEST 2024

Total time taken to generate the page: 0.02212 seconds