After completing this How To, you should be able to:
- Build a form and report in Oracle HTML DB which can upload CSV data into an Oracle table
Introduction
Many applications need the ability to bulk upload data from a spreadsheet into database tables. This can be accomplished using a TEXTAREA item and some PL/SQL code. In this example, we will upload data about different cars from a spreadsheet into the database.
Software Requirements
Create the Required Schema Objects
As a first step, you must create the required schema objects.
-
Download the DDL Script for the required schema objects and un-zip it to your local machine.
- Navigate to the
SQL Workshop.
- Click on
Scripts, which is the first option under the SQL Scripts heading.
- Click the
Upload button.
- Enter a title for your script and use the
Browse button to locate the file
upload_car_ddl.sql on your local machine.
- Click the
Add Script button.
- Your script will now be listed under the scripts report. Please click on the
Run link for your script.
- Click on the
Run Script Button.
- A screen showing the commands and the status will appear. The first time you run the script you will receive five "Object doesn't exist" errors. This will be normal as there are drop sequence and drop table commands in the script. You can ignore these errors. Any other errors indicate a problem has occurred.
Create an HTML DB Application
Next, you will need to create an HTML DB Application where you can build a form to upload the spreadsheet data. If you would like to implement this functionality into an existing HTML DB Application, then you can skip to the "Create a Form" section.
- Navigate to the Workspace home page by clicking on the Menu Item
Workspace <YOUR WORKSPACE NAME>.
- Click on
Create Application, which can be found at the top of the Application region.
- Select
From Scratch and click
Next.
- Enter a
Name for your Application, specify
1 Page, and click
Next.
- Select
One Level of Tabs and click
Next.
- Enter "
Spreadsheet Upload" as the
Page Name and click
Next.
- Take the defaults for the
Tab Name and click
Next.
- Select a
Theme and click
Next.
- Verify your settings and then click
Create Application.
Create a Form and Report
Now that you have an Application, you will need to create a form and report in order to facilitate the upload.
- Run your
Application and navigate to an empty page.
- Edit that page by clicking on
Edit Page X at the bottom of the screen.
- Click on the "
+" icon in the
Regions region, under
Page Rendering.
- Select
HTML as the
Region Type and click
Next.
- Select
HTML again and click
Next.
- Enter "
Upload Spreadsheet Data" as the
Title and click
Next.
- Leave the
HTML Text Region Source blank and click
Create Region.
- Click on the "
+" icon in the
Items region, under
Page Rendering.
- Select
Text Area and click
Next.
- Select
Textarea and click
Next.
- Enter
PX_UPLOAD as the
Item Name, where
X is the current page number and click
Next.
- Keep defaults and click
Create Item .
- Click on the "
+" icon in the
Buttons region, under
Page Rendering.
- Select the
Upload Spreadsheet region and click
Next.
- Select
Create a button in a region position and click
Next.
- Enter "
Submit" as the
Button Name. The button
Label will automatically be named the same. Click on
Next.
- Take the defaults for the
Display Properties and click
Next.
- Enter "
X" for
Branch to Page, where X is the page which you are on, and then click
Create Button.
At this point, run your application by clicking the stop light in the upper right-hand corner of the page, just below the main tabs. You should see a text area called "Upload", and a "Submit" button just below it to the right. If you enter some text, and click on the Submit button, the page should reload sucessfully. At this point, the data is not being uploaded into a table.
In the next steps, we will create a process to upload the data, which references a PL/SQL package which was created when the scripts were run at the beginning of this How To.
- Edit that page by clicking on
Edit Page X at the bottom of the screen.
- Click on the "
+" icon in the Processes region, under
Page Processing (Note - this is the middle column of the page)
- Select
PL/SQL and click Next.
- Enter "
Upload Spreadsheet Data" for the
Name, and click
Next.
- In the
PL/SQL Page Process, enter the following and click
Next:
process_upload.process_cars(:PX_UPLOAD, :PX_UPLOAD);
Be sure to replace
PX_UPLOAD with the item name you specified in Step 11. For example: P1_UPLOAD
- For the
Success Message, enter "
Data Processed" and click
Next.
- Select
SUBMIT for
When Button Pressed, and then click
Create Process.
Now that we have the process created, we need to create a Report so that we can immediately see the results.
- Click on the "
+" icon in the
Regions region, under
Page Rendering.
- Select
Report and click
Next.
- Select
SQL Report and click
Next.
- Enter "
Car Upload Results" as the
Title and click
Next.
- For the
SQL Query, enter the following and then click
Create Region :
select * from stage_cars
Our application is complete. All we have to do is test it with some sample data.
- Open the spreadsheet "
upload_cars_data.xls", which was a part of the ZIP file downloaded at the beginning of this How To.
- Select all of the rows and columns of data - excluding the titles (Row 1), and then select
Copy from the
Edit menu.
- Switch back to your HTML DB Application, and
Paste the data into the Upload textarea, then click
Submit.
- You should now see your data in the
Car Upload Results report.