Page 1 of 1

Truncate Table Before Import (v2021)

Posted: Thu Oct 06, 2022 9:23 am
by noorshiam

Dear All,

I want to make sure my table is really clean before importing data from excel. How do I truncate the table before import from excel.

Thanks for your advice.


Re: Truncate Table Before Import (v2021)

Posted: Thu Oct 06, 2022 9:40 am
by arbei

You may use Page_Importing server event to handle the import file first. In v2021, you may get the file path by $options["file"], load it and process it.


Re: Truncate Table Before Import (v2021)

Posted: Fri Oct 07, 2022 6:53 am
by noorshiam

I'm writing this code to Row_Import event :

public function rowImport(&$row, $cnt)
    {
        //Log($cnt); // Import record count
        //var_dump($row); // Import row
        //return false; // Return false to skip import
		$cek = ExecuteScalar("SELECT * FROM wbp WHERE nama = '" .$row["nama"]. "'"); //check if data exist
		if (!empty($cek)) { 
			ExecuteUpdate("DELETE * FROM wbp WHERE nama = '" .$row["nama"]. "'"); //delete data if exist
			return true; //running import
		} else {
			return true; //if not exist, running import
		}

    }

I got error messages --> Server error : 200

But if I refresh page, data successfully imported and List Page show correctly.
Where is the error in writing the code?


Re: Truncate Table Before Import (v2021)

Posted: Fri Oct 07, 2022 9:45 am
by arbei

You may read DELETE Statement and check your syntax.


Re: Truncate Table Before Import (v2021)

Posted: Fri Oct 07, 2022 10:51 am
by noorshiam

I change from this :
ExecuteUpdate("DELETE * FROM wbp WHERE nama = '" .$row["nama"]. "'");

to this :
ExecuteUpdate("DELETE FROM wbp WHERE nama = '" .$row["nama"]. "'");

and I still got same error massage : Server error : 200


Re: Truncate Table Before Import (v2021)

Posted: Fri Oct 07, 2022 2:58 pm
by mobhar

noorshiam wrote:

$cek = ExecuteScalar("SELECT * FROM wbp WHERE nama = '" .$row["nama"]. "'"); //check if data exist

Double check your code above. If you use ExecuteScalar, then you should SELECT field_name ... and not SELECT * ....

Read Some Global Functions for more info.


Re: Truncate Table Before Import (v2021)

Posted: Fri Oct 07, 2022 10:34 pm
by noorshiam

I change my code to:

$cek = ExecuteScalar("SELECT nama FROM wbp WHERE nama = '".$row["nama"]. "'");

And still got same error --> Server error: 200