Python3 connecting to MSSQL - python

I am writing Python3 and we have a database(mssql).
I have some strings and want to put these strings into db table. But I could not handle connecting to DB. Some people suggested using pyodbc but my python version is 3.4.4 and pyodbc is not supporting it. I am writing my codes in PyCharm IDE and I am not using anyother IDE such as Visual Studio, Eclipse, etc. DB is created and the table exists.
Which programs or libraries do I need?
Which lines that I should write to code (like import pypmssql)
Language: Python3 version 3.4.4
IDE: PyCharm latest version
DB: MSSQL

If you work on a Linux distribution, try to install pip3 (python dependency manager for python 3) then run
pip install pymssql
Then read the library documention to learn how to use it at http://pymssql.org/en/latest/

Related

Python: pyodbc import module not found

I'm badly confused. I have very little problem with the Python language, but setting up Python seems to be beyond me.
I have a Windows 7 machine. I have installed the 64-bit version of Python 3.6 (more about why 64 bits later). I need the pyodbc module. I also have the 32-bit version of Python 3.5, and pyodbc is installed in its site-packages folder. The first time I tried to get pyodbc for 3.6, pip told me the requirement was already fulfilled by the version in 3.5. Then, I found the --ignore-installed option and successfully (?) installed it in my 3.6 folder.
When I open my project in PyCharm and select my 64-bit Python 3.6 as the interpreter, PyCharm shows me the pyodbc package. But when I open the file in which pyodbc is imported, PyCharm highlights the import line and tells me it can't find pyodbc. Just in case PyCharm was confused, I tried running my program from a command window, and got the same error.
Do I have to set an environment variable? I didn't have one set when I used pyodbc in a project using Python 3.5, and I had no problem.
I am trying to use 64-bit Python because a customer requires us to write into their Oracle database. They installed an ODBC driver for us, into their standard DSN list. But since we installed a 32-bit version of Python on their system, the only DSNs it can see are those installed in the 32-bit list of DSNs. If there is some easy way to use the 64-bit Oracle ODBC driver from 32-bit Python, I'd appreciate learning about it.
Thanks again.

Python: pip looking in the wrong folder

Short version of the question: How can I get pip to install pyodbc in my Python 3.6 folder instead of seeing that it already exists in my 3.5 folder?
Because we have customers with various versions of Python, I have various versions on my computer. Also, we have been using the ancient adodb provider via COM, and using the win32com package to talk to it. Therefore, we have been using 32-bit versions of Python.
A new customer requires that we write to their Oracle database. They installed an Oracle ODBC driver in their server. However, it's a 64-bit ODBC driver, and when we run 32-bit Python, the driver will not be found.
A couple of years ago, I wrote a set of database wrapper classes based on adodb (and therefore on win32com) to meet our specific needs. I recently learned about pyodbc, and rewrote those classes to use pyodbc and remove reliance on adodb and win32com.
For this customer, I would like to use my new pyodbc classes. I tried to use pip to install pyodbc in my Python 3.6 installation, but it told me that the requirement is already satisfied because I have pyodbc in my Python 3.5 installation. I can copy it into 3.6, but I'd like to be able to use pip. What do I need to do?
Thank you.

Connecting Python with MySQL - Getting Started

I have MySQL and Python on my computer. I want to write statements in Python scripts that extract (SELECT) data from a MySQL schema and other statements that INSERT data into MySQL tables.
My version of Python is 2.7, and I installed it via Anaconda.
From what I have read it would be appropriate to use the module “import MySQLdb”. But before doing so, I would need to download and install some code that supports this module. The only code that I have found that might work are mysql-connector-python-2.1.5-py2.7-winx64.msi or mysql-connector-python-2.1.5-py2.7-win32.msi.
When I execute either of those MSI’s, the result is a pop-up with an error message: “Python 2.7 not found. We only support Python installed using the Microsoft Windows Installer (MSI) downloaded from http://www.python.org/download/. Please check the Connector/Python manual on http://dev.mysql.com/doc/ for more information."
I skimmed the Connector/Python manual to find more information about the incompatibility; no luck. I checked the PATH on my computer; it does include c:\users\sncole\Anaconda, and python.exe does reside in this folder.
Is there any way that I can use one of the MSI’s mentioned above, or is there another download that I can use for Python that was installed via Anaconda?
Assuming that you have installed Anaconda correctly, conda should be in your path. Then you can install mysql connector by typing
conda install -c anaconda mysql-connector-python

How do I install and use MySQLdb for Python 3 on Windows 10?

My various searches seem to come up with very old posts or a mention of how to do this under cygwin. I had python 3.5 installed and then installed Anaconda3. I have python 3.5 (Cpython) installed in my user directory. I tried changing the order of how things appear in my Windows Environment Variables path, so that I could try both the Anaconda version of Python and the other version of python that I have.
Currently, I am a bit confused as to the package name that I should use. Is it python-mysqldb, or MySQLdb, or mysqldb, mysqlclient. I believe that when I had Anaconda3 in my global path (and the other version of python in my user path), I was able to install mysqlclient.
Initially, I am just trying to follow a tutorial from a training site that covers databases and uses peewee.
So, can the mysql driver for peewee be installed for python3? Or on Windows specifically?
It is easy enough to use sqlite3, one doesn't use that in production, is that right?
Can someone help me? Provide some guidance?
Also, one source of confusion is when other forms of installation of a python package are listed in the google results (many point to stack overflow), such as using easy_install, or cloning something from git. When I see instructions that are from 2010 and they reference easy_install, I had been thinking that now we can just use pip instead? Also, sometimes I see use of the conda command. Does that work the same as pip?
Thanks in advance,
Bruce
You could use pymysql. "The goal of PyMySQL is to be a drop-in replacement for MySQLdb". Check the docs here. Install the following libraries
pip install mysqlclient pymysql
Once these libraries are installed, just add the lines in the manage.py file in your project and use the database settings for mysql.
import pymysql
pymysql.install_as_MySQLdb()
Now any files that import MySQLdb will work.

How to use MySQL with Anaconda's Python IDE

I just want to be able to do a simple SELECT * FROM query.
I found a Python MySQLdb Package, but can't get it to install using the Windows installer. It says,
Python is not found in the registry
I then tried briefly to compile it myself but I've debugged about six errors so far during the compile process and have given up.
If you are able to connect to MySQL using Anaconda, could you please detail out how you did it? Thanks.
The problem is that the binary installers for Python packages are looking for a specific registry key, which does not exist if you are using the Anaconda installer.
To make matters worse, Anaconda doesn't provide database drivers in either their commercial or free repository.
So now you have two options
Recommended - get rid of Anaconda. Uninstall it. Install Python using the official Python installer for Windows. At this time, I would recommend the 2.7.x series for maximum compatibility. To make your vanilla installation the same as Anaconda, you can install the packages that are bundled with Anaconda separately.
The Laboratory for Fluorescence Dynamics at the University of California, Irvine maintains a repository of Python packages that are difficult to install on Windows. They have taken these packages and converted them into Windows installer binaries. So, simply download and double click to install. Just like the MySQL installer, these will also only work with the official Python installer; for the same reason.
The main packages you are probably after, you can start by installing the following:
setuptools
ipython
numpy
scipy
pandas
matplotlib requires:
dateutil
pytz
pyparsing
six
Pillow
pycairo
tornado
certifi
backports
wx-python
pyside
pyqt
Numba
llvm
blaze
bokeh
It will take some time to download these, but they are all Windows installers so you just have to double click your way through them. Keep in mind the pre-requisites (indented entries above).
The other option is to trick the MySQL installer (and other Windows installers) into thinking the Anaconda installation is the official Python installation. You can do that by modifying the registry.
For Windows 7 64, create a file with the following:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Python\PythonCore\2.7\InstallPath]
#="D:\\Python27\\"
Replace "D:\\Python27\\" with the path to your Anaconda installation. Save the above file with a .reg extension, then double click it to affect the changes to your registry. To do this might require administrator level access so the above might not work for your system.
I would highly recommend option #1 because it will ensure your system is setup to be most compatible with other Python libraries.
Anaconda comes with the conda installer, so use conda instead of pip. This works for all packages that are available for conda. There are MySQL packages available, you can select them from https://docs.continuum.io/anaconda/pkg-docs.
For pymysql you open up the command line, call 'conda install pymysql' and watch the magic unfold.
There is of course a trade-off (for the not-gurus like me at least): there are also packages that are not available for conda. tweepy is one of those. On the other hand, I did not manage to get sklearn installed using pip after uninstalling anaconda as suggested in the other answer.
[Edit:]I could install tweepy after searching on https://anaconda.org
[Edit, in response on question:]
#scottlittle,you mean something like:
# -*- coding: utf-8 -*-
# imports
import pymysql
# open connection to the database
conn = pymysql.connect(host='localhost',
port=3306,
user='<your_user>',
passwd='<your_pwd>',
db='<your_db>',
charset='utf8')
cur = conn.cursor()
sql = "SELECT * FROM <your_favourite_table>"
cur.execute(sql)
# close connection to the database
cur.close()
conn.close( )

Categories