Windows의 Python 3에서 MySQL에 어떻게 연결할 수 있습니까?
Windows에서 ActiveState Python 3을 사용하고 있으며 MySQL 데이터베이스에 연결하고 싶었습니다. 그것이 mysqldb
사용할 모듈 이라고 들었습니다 . mysqldb
파이썬 3을 찾을 수 없습니다 .
바이너리가 존재하는 곳에 사용 가능한 저장소가 mysqldb
있습니까? Windows의 Python 3에서 MySQL에 어떻게 연결할 수 있습니까?
현재 mysql과 함께 Python 3을 사용하기위한 몇 가지 옵션이 있습니다.
https://pypi.python.org/pypi/mysql-connector-python
- 오라클의 공식 지원
- 순수한 파이썬
- 조금 느려
- MySQLdb와 호환되지 않습니다
https://pypi.python.org/pypi/pymysql
- 순수한 파이썬
- mysql-connector보다 빠름
MySQLdb
호출 후 와 거의 완벽하게 호환pymysql.install_as_MySQLdb()
https://pypi.python.org/pypi/cymysql
- 선택적 C 속도 향상 기능을 갖춘 pymysql 포크
https://pypi.python.org/pypi/mysqlclient
- 장고 추천 도서관.
- 원래 MySQLdb의 친절한 포크는 언젠가 다시 합류하기를 희망합니다.
- C 기반이므로 가장 빠른 구현입니다.
- 포크이므로 MySQLdb와 가장 호환됩니다.
- 데비안과 우분투는 모두 제공하기 위해 그것을 사용
python-mysqldb
하고python3-mysqldb
패키지를.
여기에 벤치 마크 : https://github.com/methane/mysql-driver-benchmarks
pymysql -Pure Python MySQL 클라이언트를 대신 사용해야 합니다.
Python 3.x에서 작동하며 종속성이 없습니다.
이 순수 Python MySQL 클라이언트는 이진 클라이언트 / 서버 프로토콜을 통해 서버와 직접 통신하여 MySQL 데이터베이스에 DB-API를 제공합니다.
예:
import pymysql conn = pymysql.connect(host='127.0.0.1', unix_socket='/tmp/mysql.sock', user='root', passwd=None, db='mysql') cur = conn.cursor() cur.execute("SELECT Host,User FROM user") for r in cur: print(r) cur.close() conn.close()
또한 pymysql (Win7 x64 컴퓨터에서 Python 3.3)을 너무 운없이 사용하려고했습니다. .tar.gz를 다운로드하고 압축을 풀고 "setup.py install"을 실행했는데 모든 것이 정상으로 보였습니다. 데이터베이스에 연결하려고 할 때까지 "KeyError [56]"이 나타납니다. 어디서나 문서를 찾을 수없는 오류.
그래서 나는 pymysql을 포기하고 Oracle MySQL 커넥터 에 정착했습니다 .
설치 패키지로 제공되며 기본적으로 작동합니다. 또한 문서화 된 것으로 보입니다.
MySQLdb를 먼저 사용하려면 Windows의 cmd를 입력하여 PC에 pymysql을 설치해야합니다
pip install pymysql
그런 다음 파이썬 쉘에서
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb
db = MySQLdb.connect("localhost" , "root" , "password")
연결이 설정됩니다.
테스트되지 않았지만 다음에서 사용할 수있는 일부 바이너리가 있습니다.
PyMySQL은 MySQLDb와 같은 인터페이스를 제공합니다. 초기화를 시도 할 수 있습니다.
import pymysql
pymysql.install_as_MySQLdb()
Also there is a port of mysql-python on github for python3.
https://github.com/davispuh/MySQL-for-Python-3
Summary
Mysqlclient is the best alternative(IMHO) because it works flawlessly with Python 3+, follows expected conventions (unlike mysql connector), uses the object name mysqldb which enables convenient porting of existing software and is used by Django for Python 3 builds
Is there a repository available where the binaries exist for mysqldb?
Yes. mysqlclient allows you to use mysqldb functions. Though, remember this is not a direct port by mysqldb, but a build by mysqlclient
How can I connect to MySQL in Python 3 on Windows?
pip install mysqlclient
Example
#!/Python36/python
#Please change above path to suit your platform. Am running it on Windows
import MySQLdb
db = MySQLdb.connect(user="my-username",passwd="my-password",host="localhost",db="my-databasename")
cursor = db.cursor()
cursor.execute("SELECT * from my-table-name")
data=cursor.fetchall()
for row in data :
print (row)
db.close()
I can't find mysqldb for Python 3.
mysqldb has not been ported yet
Oracle/MySQL provides an official, pure Python DBAPI driver: http://dev.mysql.com/downloads/connector/python/
I have used it with Python 3.3 and found it to work great. Also works with SQLAlchemy.
See also this question: Is it still too early to hop aboard the Python 3 train?
On my mac os maverick i try this:
In Terminal type:
1)mkdir -p ~/bin ~/tmp ~/lib/python3.3 ~/src 2)export TMPDIR=~/tmp
3)wget -O ~/bin/2to3
4)http://hg.python.org/cpython/raw-file/60c831305e73/Tools/scripts/2to3 5)chmod 700 ~/bin/2to3 6)cd ~/src 7)git clone https://github.com/petehunt/PyMySQL.git 8)cd PyMySQL/
9)python3.3 setup.py install --install-lib=$HOME/lib/python3.3 --install-scripts=$HOME/bin
After that, enter in the python3 interpreter and type:
-
import pymysql. If there is no error your installation is ok. For verification write a script to connect to mysql with this form:
# a simple script for MySQL connection import pymysql db = pymysql.connect(host="localhost", user="root", passwd="*", db="biblioteca") #Sure, this is information for my db # close the connection db.close ()*
Give it a name ("con.py" for example) and save it on desktop. In Terminal type "cd desktop" and then $python con.py If there is no error, you are connected with MySQL server. Good luck!
CyMySQL https://github.com/nakagami/CyMySQL
I have installed pip on my windows 7, with python 3.3 just pip install cymysql
(you don't need cython) quick and painless
This does not fully answer my original question, but I think it is important to let everyone know what I did and why.
I chose to continue using python 2.7 instead of python 3 because of the prevalence of 2.7 examples and modules on the web in general.
I now use both mysqldb and mysql.connector to connect to MySQL in Python 2.7. Both are great and work well. I think mysql.connector is ultimately better long term however.
I'm using cymysql with python3 on a raspberry pi I simply installed by: sudo pip3 install cython sudo pip3 install cymysql where cython is not necessary but should make cymysql faster
So far it works like a charm and very similar to MySQLdb
This is a quick tutorial on how to get Python 3.7 working with Mysql
Thanks to all from who I got answers to my questions
- hope this helps somebody someday.
----------------------------------------------------
My System:
Windows Version: Pro 64-bit
REQUIREMENTS.. download and install these first...
1. Download Xampp..
https://www.apachefriends.org/download.html
2. Download Python
https://www.python.org/downloads/windows/
--------------
//METHOD
--------------
Install xampp first after finished installing - install Python 3.7.
Once finished installing both - reboot your windows system.
Now start xampp and from the control panel - start the mysql server.
Confirm the versions by opening up CMD and in the terminal type
c:\>cd c:\xampp\mysql\bin
c:\xampp\mysql\bin>mysql -h localhost -v
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 10.1.21-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
This is to check the MYSQL version
c:\xampp\mysql\bin>python
Python 3.7.0b3 (v3.7.0b3:4e7efa9c6f, Mar 29 2018, 18:42:04) [MSC v.1913 64 bit (AMD64)] on win32
This is to check the Python version
Now that both have been confirmed type the following into the CMD...
c:\xampp\mysql\bin>pip install pymysql
After the install of pymysql is completed.
create a new file called "testconn.py" on your desktop or whereever for quick access.
Open this file with sublime or another text editor and put this into it.
Remember to change the settings to reflect your database.
#!/usr/bin/python
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb
db = MySQLdb.connect(user="yourusernamehere",passwd="yourpasswordhere",host="yourhosthere",db="yourdatabasehere")
cursor = db.cursor()
cursor.execute("SELECT * from yourmysqltablehere")
data=cursor.fetchall()
for row in data :
print (row)
db.close()
Now in your CMD - type
c:\Desktop>testconn.py
And thats it... your now fully connected from a python script to mysql...
Enjoy...
참고URL : https://stackoverflow.com/questions/4960048/how-can-i-connect-to-mysql-in-python-3-on-windows
'Programing' 카테고리의 다른 글
MissingManifestResourceException은 무슨 뜻이고 어떻게 해결 하죠? (0) | 2020.06.18 |
---|---|
dplyr tbl 열을 벡터로 추출 (0) | 2020.06.17 |
jQuery는 입력이 유형 체크 박스인지 확인합니까? (0) | 2020.06.17 |
문자열에 제로 패딩 추가 (0) | 2020.06.17 |
C #을 사용하여 파일에서 텍스트를 찾아 바꾸는 방법 (0) | 2020.06.17 |