Click to See Complete Forum and Search --> : m_db.ExecuteSQL() errors!


forester
May 28th, 2005, 03:49 AM
I just began to write a database program using ODBC, and got 2 problems.

the 1st was: I used the m_db.ExecuteSQL(), but failed, get error info as "ORA-00911 invalid string "
here is the sample code:
CString sql="drop table employee cascade constraints create table employee (name varchar2(90) not null, ID number(6) not null);";
m_db.ExecuteSQL(sql);

this piece of code failed, I don't kown where is wrong .

the another similiar SQL command is below
CString sql="insert into employee values('a',1); insert into employee values('b',2);";
m_db.ExecuteSQL(sql);

this snippet didn't work too. the same error occured !

----------------------------------------------------------------------------

the 2nd questions is, How can I know if a table exists or not, if the table doesn't exist, the
command
m_db.ExecuteSQL("drop table employee ;")
always failed with erro info "the table or view doesn't exist;"

By executing this snippet, what I want to do is to drop the table first , then create the table again, and import some records, just do a new complete installation.
pseudocode below:
if(table exists)
drop table and create table again
else
create table without drop table or an errror showing "the table or view doesn't exist" prompted.


I don't known how to resolve these problems, Any help will be appreciated

ovidiucucu
May 29th, 2005, 08:13 AM
CString sql="drop table employee cascade constraints create table employee (name varchar2(90) not null, ID number(6) not null);";

m_db.ExecuteSQL("drop table employee ;")

CString sql="insert into employee values('a',1); insert into employee values('b',2);";

Remove semicolon (';') from the end of statements
Also you cannot pass multiple statements to ExecuteSQL (see the last quoted line).


if(table exists)
drop table and create table again
else
create table without drop table or an errror showing "the table or view doesn't exist" prompted.

For some DBMS you can SELECT from system tables/views to check for a table existence.
Others offer easier ways, e.g. in MySQL: "DROP TABLE IF EXISTS employee".
Another way, catch the exception thrown by ExecuteSQL.
TRY
{
m_db.ExecuteSQL(_T("DROP TABLE employee"));
}

CATCH(CDBException, e)
{
// do nothing...
}
END_CATCH
// Further, (re)create the table...

forester
June 1st, 2005, 09:10 AM
if there are dozens of tables. Can I put the try{} catch{} in the for loop
there is the example:

for(int i=0; i<30;i++)
{
try
{
//do something here
}
catch()
{
// do somethind here too.
}


}

Thanks!

ovidiucucu
June 1st, 2005, 09:19 AM
Yes, of course.

forester
June 4th, 2005, 09:03 AM
Thank you very much