Save the output as csv file from VScode programmatically - sql

My Requirement: Save the SQL query output (received the data) in to the local drives in csv file format.
My OS: Windows 10 64 bit
VS Code 1.67.1:
I have installed the following extensions to connect with snowflake data warehouse:
SQL Tools
Snowflake driver for SQL Tools
I have successfully connected my snowflake (cloud) data warehouse and received the data at
the VS code.
What I want is to save (export) the output (received the data) to the local file (for example to D:\result\result.csv).
How can I achieve that?
image attached for your reference.
thank you all.
pmk

If you can run a bit of python, this does pretty much what you need
import snowflake.connector
import csv
conn = snowflake.connector.connect(
user='',
password='',
account='',
warehouse='',
database='',
schema='',
role=''
)
results = conn.cursor().execute("""MY QUERY""").fetchall()
csvfile = open(r"PATH TO FILE",'a', newline='')
output = csv.writer(csvfile)
output.writerows(results)
csvfile.close

Related

Pandas connect to Oracle error oci.dll not present

I am trying to connect pandas to Oracle as below(I already downloaded oracle client):
import pandas as pd
import cx_Oracle
username='a'
password='d'
host_name = 'aa.com'
service_name= 'ss'
dsn = cx_Oracle.makedsn(host=host_name,port=1535,sid=None,service_name=service_name)
con = cx_Oracle.connect(user=username, password=password, dsn= dsn ,encoding = "UTF-8", nencoding = "UTF-8")
my_sql_query=(""" SELECT * FROM schema.tbl1 WHERE ROWNUM =1 """)
##1- Directly reading SQL to Pandas
#Read SQL via Oracle connection to Pandas DataFrame
df = pd.read_sql(my_sql_query, con=con)
I get:
Cannot locate a 64-bit Oracle Client library: "C:\oracle\product\11.2.0\client_1\bin\oci.dll is not the correct architecture". See https://oracle.github.io/odpi/doc/installation.html#windows for help
When I click the link shown in the message, it asks me to run some .exe file. What is this file going to do?
You need to make sure that Python, cx_Oracle and the Oracle client libraries are all the same 64-bit or 32-bit architecture. It sounds like you have a mis-match.
The link given in the error is for HTML documentation; it doesn't run an exe file. The documentation mentions a VS Redistributable is needed - which is an exe file. This is a Microsoft package needed by Oracle Instant Client.

Python sqlite3 error- the database is encrypted

I'm trying to query a data base in the form of an SQL file I have downloaded on my computer to use the data in a machine learning project. I've looked at the database source code, and there is no password setting statement, so I'm very confused at the error I keep getting, which is 'DatabaseError: file is encrypted or is not a database.'
import sqlite3 as lite
con = None
con = lite.connect('haiku1aip1.sql')
cur = con.cursor()
cur.execute('SELECT * FROM haiku1aip1')
rows = cur.fetchall()
poems = []
for row in rows:
poems.append(row)
print(poems)
con = lite.connect('haiku1aip1.sql')
This line is trying to connect to a database named "haiku1aip1.sql", but .sql is not the correct file extension for a database file. Your database file would end in .db.
.sql files contain SQL queries, inserts, and other statements (similar to your "SELECT * FROM haiku1aip1" query).

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

Running SQL in Stata

I am trying to load data from SQL server management studio into Stata. How do I get Stata to run the .sql file? I have used the -ado- procedure from another post, but it does not work because my database has a username and password.
Original -ado- code:
program define loadsql
*! Load the output of an SQL file into Stata, version 1.2 (dvmaster#gmail.com)
version 12.1
syntax using/, DSN(string) [CLEAR NOQuote LOWercase SQLshow ALLSTRing DATESTRing]
#delimit;
tempname mysqlfile exec line;
file open `mysqlfile' using `"`using'"', read text;
file read `mysqlfile' `line';
while r(eof)==0 {;
local `exec' `"``exec'' ``line''"';
file read `mysqlfile' `line';
};
file close `mysqlfile';
odbc load, exec(`"``exec''"') dsn(`"`dsn'"') `clear' `noquote' `lowercase' `sqlshow' `allstring' `datestring';
end;
help odbc discusses connect_options for connecting to odbc data sources. Two of which are u(userId) and p(password) which can be added to the original code written by #Dimitriy V. Masterov (see post here).
I believe you should be able to connect using SQL Server authentication by adding the u(string) and p(string) as additional options following syntax in the ado file, and then again down below following
odbc load, exec(`"``exec''"') dsn(`"`dsn'"')
This would also require that you pass these arguments to the program when you call it:
loadsql using "./sqlfile.sql", dsn("mysqlodbcdata") u(userId) p(Password)

Import dump file

I am totally new in oracle world and need help.
I installed oracle free version OracleXE112_Win64 and has got a dump file that I need to import.
Is there anyone who can guide me how to do it?
Thank's
Can someone tell me what happens here when I import the dump file. I get the following:
Username: system
Password:
Connected to: Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production Import data only (yes/no): no > yes
Import file: EXPDAT.DMP > C:\Buildings22.dmp
Enter insert buffer size (minimum is 8192) 30720> 30720
Export file created by EXPORT:V11.01.00 via conventional path
Warning: the objects were exported by Buildings22, not by you
import done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set
import server uses AL32UTF8 character set (possible charset conversion)
List contents of import file only (yes/no): no > yes
IMP-00402: Invalid parameter "SHOW" with data_only mode
IMP-00000: Import terminated unsuccessfully
How can I check if I import a dump file?
How can I display the content of dump file, tables in Oracle SQL Developer?
I apologize for the issues, but I really brand new in oracle?