different sql's returning same data

This is a discussion on different sql's returning same data within the C++ Programming forums, part of the General Programming Boards category; I think I am having a db closure issue. I am running a query from a dialog interface and inputing ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    164

    Question different sql's returning same data

    I think I am having a db closure issue. I am running a query from a dialog interface and inputing the info into a file and then displaying into a list box. This works fine, until a I use another statment execution from another dialog to a different list box. The data remains the same. I am accessing the same db table for the different info, am I just getting the same query data over and over? Here's a sample of the code being used when clicking different buttons in the same dialog.

    Code:
    void FCHVMUNIT::OnButton3() 
    {
    		//declare variables 
        CRecordset      rec;
    	CDatabase		db;  
    	CString			sz_statement;
    	CString			s_name;
    	CString			s_id;
    	CString			s_department;
    	CString			s_equipType;
    	CString			s_equipNumber;
    	CString			s_equipName;
    	CString			s_status;
    	CString			s_employee;
    	CString			s_employeeId;
    	CString			s_dateTime;
    	CString sDriver = "MICROSOFT ACCESS DRIVER (*.mdb)";
    	CString sDsn;
    	CString sFile = "C:\\HEBMH.mdb";
    	CString singlequote = "'";
    	CString percentquote = "%'";
    	CString selectStatmentEquip= "SELECT * from Equipment WHERE equiptype like 'HVM%' and status = 'a'";
    
    	sDsn.Format("ODBC;DRIVER={%s};DSN=HEBMH;DBQ=%s",sDriver,sFile);
    	
    	try
    	{
    	
    	 db.Open(sDsn);
    
    	}
    	catch(CDBException *e)
    	{
    	   e->Delete ();    
    	}
    	rec.m_pDatabase = &db;
    	
    
    	ofstream fout;
    	fout.open ("C:\\mydbresults.txt", ios::app);
    	rec.m_pDatabase = &db;
    	s_id ="";
    	s_id += selectStatmentEquip;
    	sz_statement += s_id;
    		try
    		{
    			rec.Open(CRecordset::forwardOnly, sz_statement);
    			while(!rec.IsEOF())
    			{
    				
    				rec.GetFieldValue("EQUIPTYPE", s_equipType);
    				fout<<s_equipType<<" \t";
    							
    				rec.GetFieldValue("EQUIPNUMBER", s_equipNumber);
    				fout<<s_equipNumber<<" \t";
    		
    				rec.GetFieldValue("EQUIPNAME", s_equipName);
    				fout<<s_equipName<<" \t";
    				
    				rec.GetFieldValue("STATUS", s_status);
    				fout<<s_status<<" \t";
    	
    				rec.GetFieldValue("EMPLOYEE", s_employee);
    				fout<<s_employee<<" \t";
    				
    				rec.GetFieldValue("EMPLOYEEID", s_employeeId);
    				fout<<s_employeeId<<"\t";
    
    				rec.GetFieldValue("TIMESTAMP", s_dateTime);
    				fout<<s_dateTime<<"\n";
    									
    				rec.MoveNext();
    			}
    			rec.Close();
    		}
    		catch (CDBException *e)
    		{
    			  e->Delete ();
    		}
    
    		db.Close(); 
    		
    
    	//	CListBoxSafe::LoadList(m_List1,"C:\\mydbresults.txt",0,0);
    	ifstream file; 
    	char x;
    	CString buffer;
    	//Open file 
    
    	file.open("C:\\mydbresults.txt", ios::in); //What ever your file is 
     
    
    	//Check for file exist 
    	if(!file) 
    	{ 
    		CString facts = "File not found"; 
    		MessageBox(facts,NULL,MB_OK); 
    	} 
    
    	//Read data 
    	while(file.get(x)) 
    	{ 
    
    		if(x=='\n') 
    		{ 
    			m_List1.AddString(buffer); 
    			//MessageBox(buffer,NULL,MB_OK); //Test buffer 
    			buffer=""; 
    		} 
    		else 
    		buffer = buffer + x; 
    	} 
    
    
    		m_List1.AddString(buffer); 
    		UpdateData(FALSE);	
    		//delete old file
    		file.close();
    		DeleteFile ("C:\\mydbresults.txt");
    }
    Thanks as always, everyone on this site has been super helpful for a newbie like me!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You might try closing your output stream:
    Code:
    		db.Close(); 
    		fout.close();
    		
    
    	//	CListBoxSafe::LoadList(m_List1,"C:\\mydbresults.txt",0,0);

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    164
    Thanks for the reply Swoopy, sorry to say that didn't work, but you were right to point out that I was not closing my output stream. I fixed that but I am still getting the same issue. Repeated data regardless of the statement being executed, from any one dialog.

    Could this be related to me not having a seperate class for each table?

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    164
    Hey Swoopy, your advice helped more than I thought, after closing my output stream, took a closer and look and realized I was not closing my db soon enough, emmediately after ouputing to my file. I changed my db.close and fout.close to be within my initial "try" call to the sql statement and that fixed the problem. Thanks-

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Waternut, that's interesting, so you've got it like this now?
    Code:
    		try
    		{
    			rec.Open(CRecordset::forwardOnly, sz_statement);
    			while(!rec.IsEOF())
    			{
    				//code
    			}
    			rec.Close();
    			db.Close(); 
    			fout.close();
    		}
    I did notice something minor, which you may have already seen:
    Code:
    	rec.m_pDatabase = &db;
    	
    
    	ofstream fout;
    	fout.open ("C:\\mydbresults.txt", ios::app);
    	rec.m_pDatabase = &db;
    You've this line twice:
    rec.m_pDatabase = &db;

    I'm glad you got it working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 11:28 PM
  2. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 01:22 PM
  3. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  4. binary tree of processes
    By gregulator in forum C Programming
    Replies: 1
    Last Post: 02-28-2005, 12:59 AM
  5. Replies: 1
    Last Post: 07-31-2002, 12:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21