Solved

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

Posted on 2000-02-29
11
351 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
ID: 2568965
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
ID: 2571113
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
ID: 2572638
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:V_Bapat
ID: 2586546
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
ID: 2586649
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
 

Author Comment

by:greenbird
ID: 2586657
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
ID: 2586685
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
ID: 2587013
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
ID: 2587088
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
ID: 2587162
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
ID: 2587185
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maven archtype selection in eclipse 1 58
VBA "SendKeys" Syntax for Multiple Keystrokes 7 125
modThree challenge 4 110
How do I get Window Title of all opened process? 4 38
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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