Solved

Urgent help! CdaoDatabase add multiple new fields ...

Posted on 2000-02-26
8
332 Views
Last Modified: 2013-11-20
Hi,

I have created a new database as specified by user, then i want to insert new fields (together with values got from an old database). i use the following code:(outline)

// create the new database
if (!createDatabase(&m_newDatabase, m_newDatabaseName))
    m_newDatabase = NULL;

// read values from old one and add them to the new one
for (i=0;i<count;i++){

......
  // add/create  new fields
   openDatabase(m_newDatabase, m_strDatabaseName, FALSE);      
   strcpy(sql_add,"CREATE TABLE DataTab (");
   strcat(sql_add,fd_name);
   strcat(sql_add," CHAR(10))");
   m_newDatabase->Execute(sql_add);       
   m_newDatabase->Close();

   m_strTableName="DataTab";
   openDatabase(m_newDatabase, m_newDatabaseName, FALSE);
  openTableDef(m_newDatabase,&m_newTableDef,                         m_strTableName);

  // open a recordset for the new database
  CDaoRecordset rs1(m_newDatabase);            
  rs1.Open(m_newTableDef);

  //open a recordset for the old database
  openDatabase(&m_oldDatabase, m_oldDatabaseName, FALSE);
  openTableDef(m_oldDatabase, &m_oldTableDef, m_strTableName);
  CDaoRecordset rs(m_oldDatabase);            
  rs.Open(m_pTableDef);
 // read values from old one and add them to the new one
 ....
  rs1.AddNew();
  rs1.SetFieldValue(field_name,age_str);
  rs1.Update();

}


my problem is :

for the first time i add ONE new field, it works; but later on if i did not delete the newly created databse, and if i continue to add new filed(s) (even to another new database), it says:

the DataTab already exists...

i use the same table name for the old and new database...

any one has any idea? pls help me!!!! i need to solve it urgently!!!!

thanks a million!!!!

0
Comment
Question by:greenbird
  • 5
  • 3
8 Comments
 
LVL 3

Expert Comment

by:V_Bapat
ID: 2569143
Why don't you split the job into 2 parts?

1. Create tables & fields.
2. Copy records.

This will make it easy.

I have code to copy all the tables which I can give you.
0
 

Author Comment

by:greenbird
ID: 2571130
Hi,

where is ur code? :) u can email to :
aibinbin@hotmail.com...

i think in my another post, u answered my qn rite? i post new thing there, pls check it out...

thanks a lot for ur greate help! really appreciate it..
0
 
LVL 3

Accepted Solution

by:
V_Bapat earned 20 total points
ID: 2571761
This function copies all the tables. All the tables should already be created before calling this function.

So you have to write a function to create all the tables and create the fields.

CopyTables()
{
      COleVariant covFieldValue;
      CString strTableName;
      int nTableCount, i, j, nFieldCount;
      CDaoTableDefInfo pTableInfo;
      
      CDaoDatabase* m_pSrcDatabase = NULL;
      CDatabase::OpenDatabase(&m_pSrcDatabase, m_strDbName);
      
      CDaoRecordset m_pSrc(m_pSrcDatabase);
      CDaoRecordset m_pDest(m_pDatabase);
      
      nTableCount = m_pDatabase->GetTableDefCount();
      
      for(j = 0; j < nTableCount; j++)
      {
            m_pDatabase->GetTableDefInfo(j, pTableInfo);

            if(pTableInfo.m_lAttributes & dbSystemObject | pTableInfo.m_lAttributes & dbHiddenObject)
                  continue;

            m_strSQL = CString("SELECT * FROM ") + pTableInfo.m_strName;
            
            m_pSrc.Open(dbOpenDynaset, m_strSQL);
            m_pSrc.MoveFirst();
            m_pDest.Open(dbOpenDynaset, m_strSQL);
            
            nFieldCount = m_pSrc.GetFieldCount();
            
            while(!m_pSrc.IsEOF())
            {
                  m_pDest.AddNew();
                  for(i = 0; i < nFieldCount; i++)
                  {
                        m_pSrc.GetFieldValue(i, covFieldValue);
                        m_pDest.SetFieldValue(i, covFieldValue);
                  }
                  m_pDest.Update();
                  m_pDest.MoveLast();
                  m_pSrc.MoveNext();
            }
            
            m_pSrc.Close();
            m_pDest.Close();
      }

      CDatabase::CloseDatabase(&m_pSrcDatabase);
}

Vicky

0
 

Author Comment

by:greenbird
ID: 2572677
Hi, Vicky,

thanks a lot! i will try it tomorrow.. but could u pls refer to my another post: add field problem also, i have some qns there waiting for ur help... thanks a lot!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:V_Bapat
ID: 2586484
greenbird
I was out of town for last 4 days. I will try it today.

What about this one. Did you try it?

Vicky
0
 

Author Comment

by:greenbird
ID: 2586620
Hi, V_bapat,

no wonder why u did not help me .. :) now feel good that u will help me :) i need to solve it by tomorrow....
Thanks a lot!
0
 

Author Comment

by:greenbird
ID: 2586621
Adjusted points to 20
0
 

Author Comment

by:greenbird
ID: 2587153
Hi,

sovle it already :))) thanks a lot for ur help!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 87
Folder Comparison 12 52
wait notify demo infinite loop 3 80
Detect file exist or not 3 78
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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

706 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

17 Experts available now in Live!

Get 1:1 Help Now