I'm developing on an environment which I'm not allowed to install anything. It's a monitoring server and I'm making a script to work with logs and etc.
So, I need to connect to a SQL Server with Python 2.7 without any lib like pyodbc installed. Is it possible to make this? I've found nothing I could use to connect to that database.
There are certain things you can do to run sql from the command line from python:
import subprocess
x = subprocess.check_output('sqlcmd -Q "SELECT * FROM db.table"')
print x
Related
I have a problem that I've been trying to solve for a very long time and it seems like I'm missing something very basic.
I use a Linux server with Anaconda, Oracle client, Pycharm and jupyter-notebook installed.
I use python scripts in which I write and read data to Oracle DB, and I use the Cx_oracle extension.
The server has several users with a personal username for each and each of them has sudo privileges.
I performed all the installations on the server with sudo privileges.
When I try to connect from the server to Oracle DB I connect properly.
When I connect to Python on the server itself, I connect properly to Oracle DB.
When I connect using Pycharm and I define ORACLE_HOME=/OracleTools/19.0.0/ in the environment variables, I connect properly to the Oracle DB.
My problem starts when I want to use jupyter-notebook
When I try to connect to the DB I get the error -
DatabaseError: Error while trying to retrieve text for error ORA-12715
I noticed when I execute os.environ I see that it is defined for me:
ORACLE_HOME: /OracleTools/19.0.0/bin
and should be
/OracleTools/19.0.0
So I changed using command os.environ['ORACLE_HOME'] = '/OracleTools/19.0.0'
Then I get an error:
DatabaseError: ORA-12715: invalid character set specified
And of course this change is not permanently saved ...
If on the server itself I execute the env command both in the private user and in sudo I see ORACLE_HOME: /OracleTools/19.0.0 and not ORACLE_HOME: /OracleTools/19.0.0/bin
My questions:
Where does the data I get in the os.environ command come from?
How can I edit them permanently ?
Is this even the problem I'm having or should I check something else?
I manage to import cx_oracle, which means that there is no problem of expansion
Thanks!
I have been using the mysql.connector module with Python 2.7 and testing locally using XAMPP. Whenever I upload my script to the server, I am getting an import error for the mysql.connector module. I am assuming this is because, unlike my local machine, I have not installed the mysql.connector module on the server.
My question is: can I somehow use the mysql.connector module on the server or is this something only for local development? I have looked into it, and apparently do not have SSH access for my server, only for the database. As well, if I cannot use the mysql.connector module, how do I connect to my MySQL database from my Python script on the server?
You can use mysql.connector on the server. However, you will have to install it first. Do you have root (admin) access? If no, you might need help from the server admin.
So you can use the mysql.connector on the server, and it can be done without SSH or any special access!
Download it from here: https://dev.mysql.com/downloads/connector/python/2.1.html
(Click on the "Development Releases" tab and download the zip archive)
After you unzip, take the entire "mysql" folder from the lib directory and upload it to the server (wherever your other Python files are, e.g. cgi-bin or similar).
Change the entire mysql directory permission to 755 recursively.
Now you can use it in any Python script on the server by using "import mysql.connector as conn" (or name it whatever you want to)
Is there a way for python to connect to MS SQL Server using Windows Authentication, even when not running the python app on a windows box?
I'm trying to do this with pymssql, but the examples mostly seem to assume that you're running on windows.
If there is a way to make this connection using some other library, please feel free to suggest, but I do like how pymssql is simple to install and deploy via pip.
I want to connect to 2005/2008 databases, and I'm running Ubuntu 13.04 (but I can upgrade to a later Ubuntu if it makes a difference)
SOLUTION:
It turns out that pymssql can connect to my database via my windows username and password. But to do so, I need to pass the actual username/password like this:
pymssql.connect(host, 'THEDOMAIN\\theusername', 'thepassword', db)
The solution EkoostikMartin provided is still good though (if you do not want to store a password somewhere, which is probably the point to windows auth anyway)
You can use the SQL Server ODBC driver for linux, and set up Kerberos.
See this article - http://technet.microsoft.com/en-us/library/hh568450.aspx
I'm trying to install MYSQLdb on a windows client. The goal is, from the Windows client, run a python script that connects to a MySQL server on a LINUX client. Looking at the setup code (and based on the errors I am getting when I try to run setup.py for mysqldb, it appears that I have to have my own version of MySQL on the windows box. Is there a way (perhaps another module) that will let me accomplish this? I need to have people on multiple boxes run a script that will interact with a MySQL database on a central server.
you could use a pure python implementation of the mysql client like
pymysql
(can be used as a dropin-replacement for MySQLdb by calling pymysql.install_as_MySQLdb())
MySql-Connector
You don't need the entire MySQL database server, only the MySQL client libraries.
It's been a long time since I wrote python db code for windows...but I think something like this should still work.
If you're running the client only on windows machines, install the pywin32 package. This should have an odbc module in it.
Using the windows control / management tools, create an odbc entry for either the user or the system. In that entry, you'll give the connection parameter set a unique name, then select the driver (in this case MySQL), and populate the connection parameters (e.g. host name, etc.) See PyWin32 Documentation for some notes on the odbc module in pywin32.
Also, see this post: Common ways to connect to odbc from python on windows.
Over a year ago someone asked this question: Execute .sql files that are used to run in SQL Management Studio in python.
I am writing a script in python that connects to a SQL server and creates and populates a database based on SQL commands in a large (several GBs) .sql file.
It looks like SQLCMD requires a download and install of SQL Server Express. Are there other ways to execute a .sql file from python without requiring everyone who uses my script to download and install SQL Server? Does pyodbc have this capability?
EDIT:
Here's another similar question:
execute *.sql file with python MySQLdb
Here, again, the solution is to call a utility from command (in this case, mysql.exe) with the file listed as an argument.
It seems to me that there should be a way to do this using one of Python's DB API libraries, but I haven't found it so I'm looking for an *.exe like SQLCMD or MYSQL that I can use to run the file from command line.
P.S. Please feel free to correct me if I'm not looking at this correctly. Maybe the code below is just as efficient as running from command line:
for line in open('query.sql','r'):
cursor.execute(line)
I found it's actually faster to read the file in python and execute in batches using pyodbc than it is to use the SQLCMD utility externally (and I don't have to install SQLCMD on every computer I run the scripts on!).
Here is the code I used (because pyodbc doesn't seem to have an executescript() method):
with open(scriptPath, 'r') as inp:
for line in inp:
if line == 'GO\n':
c.execute(sqlQuery)
sqlQuery = ''
elif 'PRINT' in line:
disp = line.split("'")[1]
print(disp, '\r')
else:
sqlQuery = sqlQuery + line
inp.close()
Not sure if this is what you are asking but why not use MS SQL directly from Python? There are libraries like pymssql that will allow you to do that. Or you can use ODBC.
See http://wiki.python.org/moin/SQL%20Server for a list of Python MS SQL drivers
SQLCMD and other management utilities are freely available for download. They are part of what Microsoft calls "Feature pack for SQL Server".
I understand you want to perform large bulk imports. To do this you may want to check out the the BCP utility. Download http://www.microsoft.com/en-us/download/details.aspx?id=16978
Using SQL you may also perform a BULK INSERT. This command can insert data from a file to a SQL db.
Another way is of course to use SQL Server Integration Services for pure ETL jobs.