Solved

Urgent Help! CDaoDatabase: add new fields problem...

Posted on 2000-02-29
11
337 Views
Last Modified: 2013-11-20
Hi,

 lets say i want to insert 2 fields "a" and "b"  to the new database, but when i check the database, it is like this:

                      the values of the fields are got from an old database, say there are 2000 records in the original db.. then the new one is like this:


                      for the first 1000 records , only values of field "a" are added,
                      for the next 1000 records, only values of field "b" are added..
                      for the next 1000 records, only values of field "a" are added...
                      for the next 1000 records, only values of fiedl "b" are added...

                      so it seemes that the values of the new one are created parallely, fill some for field "a", then for field "b".... not fill in "a" (since it
                      is the first field i want to insert), then fill in "b"... so i do not know what is wrong ?

                      pls help! as i need to solve it asap....

                      thanks a lot!
0
Comment
Question by:greenbird
  • 7
  • 4
11 Comments
 
LVL 3

Expert Comment

by:V_Bapat
Comment Utility
The problem lies where you are inserting the RECORDS(In CDaoRecordset or CRecordset).

Show the code where you are inserting the records.

What is the old database?
0
 

Author Comment

by:greenbird
Comment Utility
Hi,

the old database is also .mdb file, i read values of two fields from the old one, then (according to some specifications) combine them into one field and write into the new .mdb file...

Thanks for helping!


in the following code m_ppDatabase m_ppTabledef if the new database and table def, m_pDatabase and m_pTabledef is the old one...

// create new database
createDatabase(m_ppDatabase,m_strDatabaseName)

for (i=0;i<count;i++){

// create a new table or add new field to the   // already created table.

openDatabase(m_ppDatabase,m_strDatabaseName, FALSE);      
// create new one
if ((IsExistentTable((*m_ppDatabase),m_strTableName)) == FALSE){
                  strcpy(sql_add,"CREATE TABLE DataTab (");
strcat(sql_add,fd_name);
strcat(sql_add," CHAR(10))");      (*m_ppDatabase)->Execute(sql_add);            (*m_ppDatabase)->Close();

}
// else inserting into the newly created one            else{
strcpy(sql_add,"ALTER TABLE DataTab ADD ");
strcat(sql_add,fd_name);
strcat(sql_add," CHAR(10)");
(*m_ppDatabase)->Execute(sql_add);            (*m_ppDatabase)->Close();
}


// open new database and tabledef
openDatabase(m_ppDatabase,m_strDatabaseName, FALSE);
openTableDef((*m_ppDatabase), &m_ppTableDef, m_strTableName);
// open the new recordset
CDaoRecordset rs1((*m_ppDatabase));            
rs1.Open(m_ppTableDef);

// open old database and old tabledef
openDatabase(&m_pDatabase, m_oldDatabaseName, FALSE);
openTableDef(m_pDatabase, &m_pTableDef, m_oldTableName);
// open old recordset
CDaoRecordset rs(m_pDatabase);            
rs.Open(m_pTableDef);


while(!(rs.IsEOF())){
// read in the values from the old database...
.....
// then write to the new one
rs1.AddNew();
// age_str is the string got from the old database                        rs1.SetFieldValue(fd_name,age_str);
                        rs1.Update();
// move to the next old record
      rs.MoveNext();
}// while

// close them each time                                                            m_pDatabase->Close();                                                            (*m_ppDatabase)->Close();

}//for
0
 

Author Comment

by:greenbird
Comment Utility
HI,

could u pls help me solve this problem also? why the tuples are inserted like this? thanks a lot! ur help is greatly appreciated...
0
 
LVL 3

Expert Comment

by:V_Bapat
Comment Utility
A few questions:
1. How many tables are there in the database.
2. What is the value of count? I think it is the no of tables but how do you obtain it?
3. What is the value of fd_name and where do you get it?
4. What is the table structure? Show the table structure of DataTab.

It seems that you have only one table in the database. If so, why have you put the FOR loop "for (i=0;i<count;i++)" for? I am not very sure what are you doing.

Also fill in the whole code for what you posted earlier. Complete this part:

while(!(rs.IsEOF())){
// read in the values from the old database...
......
// then write to the new one
rs1.AddNew();
// age_str is the string got from the old database rs1.SetFieldValue(fd_name,age_str);
rs1.Update();
// move to the next old record
rs.MoveNext();
}// while


Vicky
0
 

Author Comment

by:greenbird
Comment Utility
Hi,

1. there is only one table in the old db, and i create one table for the new db.

2. count is the number of fields to be added to the new db, it is obtained from a list box (selected by user).

3. fd_name is the field name to be added to the new db, got from listbox also.

4. table is just a simple db table: fields are : NRIC,Race, Sex, Age....... (some are text, some are date /time and some are numberic).

the complete code is:


// rs is the old db
rs.MoveFirst();
while(!(rs.IsEOF())){      

// now read values from two fields
// ex_fd[0] and ex_fd[1]. (names got from user)                  v1=rs.GetFieldValue(ex_fd[0]);
s1_str = CCrack::strVARIANT( v1 ) ;
s1 = strdup((LPCTSTR)s1_str);
                        v2=rs.GetFieldValue(ex_fd[1]);
s2_str=CCrack::strVARIANT( v2 ) ;
s2 = strdup((LPCTSTR)s2_str);

/***
 actually the following part is specific to my
program, i will use other criteria to determine the values for the new field, then add it in.. here i just show how i added the new value (coz the process of obtaining the new value is quite complexed)
***/
   rs1.AddNew();
   rs1.SetFieldValue(fd_name," ");
   rs1.Update();            
                        // move to next record
rs.MoveNext();
}//while

thanks a lot!
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:greenbird
Comment Utility
Hi,

is it because i use a "for" loop?

                      for (i=0;i<field_count;i++){

                      // read values from old one, process data
                      // write new values to new table

                      }

so that it parallely fill in the new field?
                      thanks a lot!
0
 
LVL 3

Accepted Solution

by:
V_Bapat earned 10 total points
Comment Utility
You have to put the loop for the fields inside the while loop for the records since you have to copy all the fields for the current record before moving to the next record.
0
 

Author Comment

by:greenbird
Comment Utility
Hi,

but say in the old db "oldDB" i have field "a", and "b". Now i want to add new fields "c" "d"  to a new db "newDB", the value for "c" is obtained from "a", plus some processing of data.  the value for "d" is obtained from "b" plus some other processing of data... so i need to process column by column, that is process one field first, then another field... that is why for (i=0;i<count;i++) outside while loop...

but i will try what u mentioned also...

thanks a  lot!
0
 

Author Comment

by:greenbird
Comment Utility
Hi,

i tried ur method, this time it becomes:

filed_A      field_B
1
                      2
3                    
                       4
5
                       6
7
                        8
.......


that is, one in this record, one in the next record..
i use
rs1.AddNew();
                        rs1.SetFieldValue(fd_name,age_str);
                        rs1.Update();

to update... i did not move to next record...

how come?

thanks a lot!
0
 

Author Comment

by:greenbird
Comment Utility
Hi, V_Bapat,

solved it already :) so happy and really greatly appreciate the help u had given... pls continue to help me :)
bye...
0
 
LVL 3

Expert Comment

by:V_Bapat
Comment Utility
Since you are adding column-wise and calling AddNew() to create new field everytime, new records are getting created.
There are two options for you:
1. Either you add records row-wise.
2. If you add records column-wise, you have to call AddNew() if it is the first iteration and call Edit() otherwise. Set a flag which tells you whether it is the first column or not. If it is the first column, call AddNew() else call Edit().

Vicky
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now