本来以为很简单,结果总是不太对。求一个最简单的写法。用到 C 也行。唯一的要求是不要用到数据库名。

解决方案 »

  1.   

    C 代码中先 select * from tableName
    然后判断数据库返回,如果出错,则近似说明表不存在。 不出错则说明表存在,继续执行 delete from tableName
      

  2.   

    似乎可以更简单地。直接执行 delete from tableName这样,表存在的话,自然清空也,表不存在的话,也就是一个错误信息处理一下。
      

  3.   

    select count(TABLE_NAME) from `INFORMATION_SCHEMA`.`TABLES` where `TABLE_NAME`='sessions_20071021'
    SHOW   TABLES   LIKE   '%tb_bp_d_case%';  
    select `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` where `TABLE_NAME`='res_mos_cpu_statistics'
      

  4.   

    if exists(select 1 from `INFORMATION_SCHEMA`.`TABLES` where `TABLE_NAME`='sessions_20071021'
    )
    select '存在'
    else
    select 'bucunzai'
      

  5.   


    if exists 能这样用吗?通不过的。
      

  6.   

    sp:IF EXISTS(SELECT 1 FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME`='a1') THEN
    select '存在';
    ELSE
    select '不存在';
    END IF;
        ENDor
    SELECT count(*) into @num FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME`='a1';
    if @num>=1 then
    select '存在';
    else
    select '不存在';
    end if;
      

  7.   

    IF EXISTS(SELECT 1 FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME`='a1') THEN
    select '存在';
    ELSE
    select '不存在';
    END IF;
      END
    好办法,我就是这么搞的。
      

  8.   

    如果要严格要求自己,建议你实现两个API
    (1)bool tableExists(const std::string& table_name);
    (2)void cleanTable(const std::string& table_name); //假设表已经存在分解完以后,就很好弄了
    上边的回复都有相应的思路
      

  9.   

    你只要在mySQL的command里直接, delete from tableName,就行了阿,没必要楼上那么麻烦吧
      

  10.   

    我只是说“如果要严格要求自己”,....
    如果只是想达到目的,truncate table <tablename>似乎最爽,忽略异常是吧。
      

  11.   

    DROP TABLE IF EXISTS tablename;
    CREATE TABLE IF NOT EXISTS tablename (fields) ;
      

  12.   

    drop table if exists tablename;
      

  13.   


    6楼的正解,应该可以用.
    -----------------------------------------sp:IF EXISTS(SELECT 1 FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME`='a1') THEN
    select '存在';
    delete from TABLE;//删除表
    ELSE
    select '不存在';
    END IF;
      ENDor
    SELECT count(*) into @num FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME`='a1';
    if @num>=1 then
    select '存在';
    delete from TABLE;//删除表
    else
    select '不存在';
    end if;
    ------------------
    C语法:
    SELECT count(*) into @num FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME`='a1';
    if (@num>=1){
    // '存在';
    delete from TABLE;//删除表
    }else{
    //'不存在';
    }
    endif;自己查一下num可不可以直接这样赋值的,就是这种方法。
      

  14.   

    啊,整整一年前自己发的帖子,谢谢大家。// table exists  2010/6/28
    sql = "show tables like "+ QuotedStr(tablename);
    int recordcount = getRecordCount(_mysql, sql.c_str());
    if (recordcount == -1)
    {
    string err = "error in SQL: "+ sql + ". " + mysql_error(_mysql);
    throw err;
    }
    if (recordcount == 0)
    continue;// clear the table
    sql = "delete from " + tablename;
    if (-1 == setData (_mysql, sql.c_str()))
    {
    string err = mysql_error(_mysql);
    throw err;
    }