How to get a Mariadb prepared statement using LOAD DATA LOCAL INFILE working - file-upload

I am not able to get LOAD DATA LOCAL INFILE working as a prepared statement. Error 1064 is thrown, but the syntax displays correctly as a select statement (as text). Local infile is enabled "local_infile=true" in cnf file, and I am able to import files using the same statement shown below replacing variables with actual values. Any help in pointing me in the right direction is gratefully appreciated. Of note, mariadb knowledgebase mentions "LOAD DATA INFILE is unsafe for statement-based replication." Is this the reason the prepared statement and subsequently a stored procedure won't work?
Thanks again.
SET #sql := 'LOAD DATA LOCAL INFILE ? INTO TABLE ?
FIELDS OPTIONALLY ENCLOSED BY \'\"\' TERMINATED BY \'\,\'
LINES TERMINATED BY \'\\n\'
(item, price, pdetail, #store, #purchased)
SET store = ?';
PREPARE stmt_loadmeup FROM #sql;
EXECUTE stmt_loadmeup USING 'full path to file.csv','TEST','TH';

Related

How to ignore responding to stored '&lt' and '&gt' text in Oracle SQL statement?

I have an issue when writing the sql query below in SQL Command line. It asks me "Enter a value for lt:" and then gives error
ORA-00933: SQL command not properly ended
I need to properly read the column that includes '&lt' or '&gt' as a string. How can I edit the query to make it works?
Delete from authorization1
where role = 'staff' AND object = ' /department/gradstudent/gpa'
AND predicate = ' & l t ; 2.0') AND action = 'read'
Assuming you are using SQL*Plus or SQL Developer as your front end, this issue is that &foo is the syntax for defining substitution variables. You can
set define off;
before running your script to disable substitution variables. That will stop the front end from prompting you for a value.

how to insert utf8 characters into oracle database using robotframework database library

I have a robot script which inserts some sql statements from a sql file; some of these statements contain utf8 characters. If I insert this file manually into database using navicat tool, everything's fine. But when I try to execute this file using database library of robot framework, utf8 characters go crazy!
This is my utf8 included sql statement:
INSERT INTO "MY_TABLE" VALUES (2, 'تست1');
This is how I use database library:
Connect To Database Using Custom Params cx_Oracle ${dbConnection}
Execute Sql Script ${sqlFile}
Disconnect From Database
This is what I get in the database:
������������ 1
I have tried to execute the SQL file using cx_Oracle directly and it's still failing! It seems there is a problem in the original library. This is what I've used for importing SQL file:
import cx_Oracle
if __name__ == "__main__":
dsn_tns = cx_Oracle.makedsn(ip, port, sid)
db = cx_Oracle.connect(username, password, dsn_tns)
sql_commands = open(sql_file_addr, 'r').read().split(";")
cr = db.cursor()
for command in sql_commands:
if not command in ["", "\t", "\n", "\r", "\n\r", "\r\n", None]:
print "Executing SQL command:", command
cr.execute(command)
db.commit()
I have found that I can define character-set in the connection string. I've done it for mysql database and it the framework successfully inserted UTF8 characters into database; this is my connection string for MySQL:
database='db_name', user='db_username', password='db_password', host='db_ip', port=3306, charset='utf8'
But I don't know how to define character-set for Oracle connection string. I have tried this:
'db_username','db_password','db_ip:1521/db_sid','utf8'
And I've got this error:
TypeError: an integer is required
As #Yu Zhang suggested, I read discussion in this link and I found out that I should set an environment variable NLS_LANG in order to have a UTF-8 connection to the database. So I've added below line in my test setup:
os.environ["NLS_LANG"] = "AMERICAN_AMERICA.AL32UTF8"
Would any of links below help?
http://docs.oracle.com/cd/B19306_01/server.102/b14225/ch6unicode.htm#i1006779
http://www.theserverside.com/news/thread.tss?thread_id=39575
https://community.oracle.com/thread/502949
There can be several problems in here...
The first problem might be that you don't save the test files using UTF-8 encoding.
Robot framework expects plain text test files to be saved using UTF-8 encoding, yet most text editors will not save by default using UTF-8.
Verify that your editor saves that way - for example, by opening the file using NotePad++ and choosing Encoding -> UTF-8
Another problem might be the connection to the Oracle database. It doesn't seem like you can configure the connection custom properties to explicitly state UTF-8
This means you probably need to state that the database schema itself is UTF-8

In Teradata how do I script out all stored procedures and tables?

I am working on documenting an existing set of databases in a Teradata installation. I am from a SQL Server background. How do I script out all stored procedures and tables in a Teradata DB, like I would in SQL Server, by using the Generate Scripts facility ?
I can SHOW individual tables and SPs and I can select a desired list of those objects from dbc.tables, but how do I do the Dynamic SQL to feed that list of names to the SHOW command ?
Thanks for your help. JK
If you have access to the BTEQ tool you can write SQL to dynamically generate the SHOW commands. This output can be exported from BTEQ to a flat file using the EXPORT command. Then the output file can be fed back into BTEQ and run against the database whose output can be exported to a flat file using an EXPORT command. Clear as mud?
Here is a sample BTEQ script to give you an idea and set you in the right direction. It may not be 100% accurate but it should give you enough to start writing your own script to accomplish the task at hand.
.SET FOLDLINE OFF;
.SET WIDTH 1000;
.SET TITLEDASHES OFF
.OS rm {path to exported SHOW SQL file}
.EXPORT FILE={path to exported SHOW SQL file}
SELECT CASE WHEN T1.TableKind = 'T'
THEN 'SHOW TABLE ' || TRIM(T1.DatabaseName) || '.' || TRIM(T1.TableName) || ';'
WHEN T1.TableKind = 'P'
THEN 'SHOW PROCEDURE ' || TRIM(T1.DatabaseName) || '.' || TRIM(T1.TableName) || ';'
/* Repeat for object types your interested in */
ELSE ''
END (TITLE '')
FROM DBC.Tables T1
WHERE DatabaseName = '{Database}';
.EXPORT RESET
.OS rm {path to SHOW output file}
.EXPORT FILE={path to SHOW output file}
.RUN FILE={path to SHOW SQL file}
.EXPORT RESET
The Teradata documentation found at http://info.teradata.com can help fill the gaps on the TableKind domain and the BTEQ utility and its commands. If you run into problems or have additional questions edit your question above with more information about what you have tried and either I or someone else will be happy to supply additional information.

How do we print characters line by line and save it to csv or text file in PLSQL

DECLARE
V_NUMBER NUMBER :=23;
BEGIN
LOOP
V_NUMBER:=V_NUMBER+1;
EXIT WHEN V_NUMBER:=25;
--Some kind of function to be applied for printing and nesting lines into CSV or TEXT file.
END LOOP;
COMMIT;
END;
Scripting an Oracle SQL Query for Creating a CSV or Text Typed File Output
Consider running this from a SQL Plus session and use the SPOOL command. All output of the SQL command that follows will be written to the file name you specify.
If you need to append your results each successive time the SQL commands are run, then an OS level command would work appropriately when invoking this sqlplus executable block of PL/SQL:
Where the file name of this script is: "sample_csv_out.sql"
DECLARE
v_total_columns constant number:= 3; -- Number of columns queried
v_column_counter number;
v_csv_record varchar2(1000);
c_csv_column_format constant varchar2(15):=
'<<COLUMN1_VAL>>,<<COLUMN2_VAL>>,<<COLUMN3_VAL>>';
cursor result_cur is
SELECT column1, column2, column3
FROM tablea
WHERE column1 = ... ;
BEGIN
v_csv_record:= 'COLUMN1,COLUMN2,COLUMN3';
dbms_output.put_line (v_csv_record);
FOR i in result_cur LOOP
v_csv_record:= replace(c_csv_column_format, '<<COLUMN1_VAL>>', i.column1);
v_csv_record:= replace(v_csv_record, '<<COLUMN2_VAL>>', i.column2);
v_csv_record:= replace(c_csv_record, '<<COLUMN3_VAL>>', i.column3);
dbms_output.put_line(v_csv_record);
END LOOP;
END;
So, for example in a WINDOWS O/S environment, the call to append the output to a specific file name would be:
C:\> sqlplus sample_csv_out.sql >> mycsv_out.csv
The >> notation instructs the operating system to pipe the output of running sample_csv_out.sql via a sqlplus session.
The command DBMS_OUTPUT does the rest. If you need more details, see more Oracle documentation on DBMS_OUTPUT.
COMMENTS: I chose the RECORD STRING TEMPLATE approach to make this script a little more flexible and reusable. I recommend to keep any data manipulation logic within the CURSOR statement. Often when the two are mixed, it gets harder to debug any typos in syntax within a long string of values.
The construction of an output record was also designed to reduce typos, mistakes and frustration... if there are more than 3 columns in your own scripts, adding another element to the output string is mostly a cut-and-paste operation. Likewise with the "header" row (column titles).
You can read and write files in PL/SQL using the UTIL_FILE package
http://docs.oracle.com/cd/B19306_01/appdev.102/b14258/u_file.htm

SAS: Using the Put statement to create dynamic code

I'd like to create dynamic code using the PUT statement. According to this document from SUGI 29 (http://www2.sas.com/proceedings/sugi29/175-29.pdf),
put
"data XXXXX; "
/ 'infile "&datadir/&compid&filetype" missover ls=' tbla_fle
';' / 'input'
;
is equivalent to running
data onecomp ;
infile
"&datadir/&compid&filetype"
missover ls = 268 ;
input
However, when I try something similar to their example, the code enclosed in the PUT statement doesn't run and is instead written to the SAS Output Log:
data _NULL_;
put // "data put_test;" / "b=2;" / "run;";
run;
In Output Log:
data put_test;
b=2;
run;
I've checked the SAS documentation, and it seems that PUT is only used to "Write lines to the SAS log, to the SAS output window, or to an external location that is specified in the most recent FILE statement." Nowhere does it say that it can be used to create dynamically generated code.
I know that I must be missing something, but I'm not sure what. I'm using SAS Enterprise Guide 4.1.
Thank you!
The idea is to use put to write your generated code to a file. You then %include the file into your SAS session to run it. What you're missing is a file statement and the %include directive.
data _null_;
file 'temp.sas'; /* redirects put to a file instead of the SAS log */
put
"data XXXXX; "
/ 'infile "&datadir/&compid&filetype" missover ls=' tbla_fle
';' / 'input'
;
run;
%include 'temp.sas';