Quantcast
Channel: MySQL Forums - Connector/Python
Viewing all 387 articles
Browse latest View live

Django 1.10 + mysql connector 2.1.4 (no replies)

$
0
0
I have been trying to get django 1.10 to work with mysql for a while now and i have managed to get it working with mysql connector i ran into a few issues that i have been able to figure myself.
A data timezone issue happened and i just changed this in setting.py
USE_TZ = False
I then ran into this issue:
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/path/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/path/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/path/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/path/lib/python3.5/site-packages/django/core/management/base.py", line 345, in execute
    output = self.handle(*args, **options)
  File "/path/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 224, in handle
    self.verbosity, self.interactive, connection.alias, apps=post_migrate_apps, plan=plan,
  File "/path/lib/python3.5/site-packages/django/core/management/sql.py", line 53, in emit_post_migrate_signal
    **kwargs
  File "/path/lib/python3.5/site-packages/django/dispatch/dispatcher.py", line 191, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/path/lib/python3.5/site-packages/django/contrib/auth/management/__init__.py", line 83, in create_permissions
    Permission.objects.using(using).bulk_create(perms)
  File "/path/lib/python3.5/site-packages/django/db/models/query.py", line 452, in bulk_create
    ids = self._batched_insert(objs_without_pk, fields, batch_size)
  File "/path/lib/python3.5/site-packages/django/db/models/query.py", line 1068, in _batched_insert
    self._insert(item, fields=fields, using=self.db)
  File "/path/lib/python3.5/site-packages/django/db/models/query.py", line 1045, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/path/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1053, in execute_sql
    for sql, params in self.as_sql():
  File "/path/lib/python3.5/site-packages/django/db/models/sql/compiler.py", line 1038, in as_sql
    result.append(self.connection.ops.bulk_insert_sql(fields, placeholder_rows))
  File "/path/lib/python3.5/site-packages/mysql/connector/django/operations.py", line 223, in bulk_insert_sql
    return "VALUES " + ", ".join([items_sql] * num_values)
TypeError: can't multiply sequence by non-int of type 'tuple'
I take it this has happened because mysql connector isn`t comp with django 1.10 atm.
Does anyone have a work around for this?

The performance is worse than pyMysql? (no replies)

$
0
0
I used pyMysql before. Now, I tried to use connection pool, and found there was a native pool implementation in the official connector. But, I compared their performance, and found MySQL Connector/Python have worse performance. Generally, I execute a select SQL, pyMysql cost 30 ms, but MySQL Connector/Python cost 100 ms.
Do you know how to tunning it?

I use the below code to get connection and execute sql.
dbconfig = {
'user' : 'xxxx',
'password' : 'xxxx',
'host' : 'localhost',
'database' :'test_db',
'pool_name' : 'mypool',
'pool_size' : 5
}
mysql.connector.connect(**dbconfig)

cursor = conn.cursor()
cursor.execute(sql)
rows = cursor.fetchall()

Duplicate PRIMARY key problem (no replies)

$
0
0
environment:
mysql 5.7 on rhl6
mysql-python/connector
python 2.7.12 on windows, python 2.6.6 on rhl6

I have a time-series fact table with primary key on ID and a timestamp column. The ID column is a mysql auto-gen column and is unique for every new record inserted into this table. (why the timestamp column is in the primary key is not import here).

I'm writing a python script to popuate the table with records for a list of objects at every 15-minute mark.

The table was initially empty. I started populating the table from 00-minute mark, and records were committed to the table without problem. But it failed on the first 15-minute mark record with a duplicate PRIMARY key error.

I truncated the table and started the script again, it failed at the same place with the same error.

I truncated the table and change the script to start from 15-minute mark, the script successfully created and committed all the 15-minute mark records, then failed at the first 30-minute mark record with the same duplicate PRIMARY key error.

I then get the "create table" statement from workbench, drop and recreate the table, rerun the script, everything works fine. The problem disappeared!

Now I'm scratching my head to try to understand this. Could somebody help me please?

Upgrade Connector/Python (no replies)

MySQLInterfaceError: SSL is required but the server doesn't support it (no replies)

$
0
0
I am using MySQL Python/Connector 2.1.4 (with C extension) on an Ubuntu 16.04 64-bit system with Python 3.5.2 / 2.7.12. I have the following code -

import _mysql_connector
conn = _mysql_connector.MySQL()
conn.connect(user='root', password='root', database='db1')
conn.close()

On running it as

python3 file.py

I get the following error -

Traceback (most recent call last):
File "file.py", line 4, in <module>
conn.connect(user='root', password='root', database='db1')
_mysql_connector.MySQLInterfaceError: SSL connection error: SSL is required but the server doesn't support it

This error appears in both versions of Python. I do not want to use SSL as of now. How do I disable it?

Connector for python 3.5 in Windows (no replies)

$
0
0
When will we get a connector for version 3.5?
Br Håkan

Error when executing MSI (no replies)

$
0
0
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 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?

mysql.connector for binary file save to blob field (2 replies)

$
0
0
Hi, everyone.

It is my first time to ask for help here.

I use Python 3.4 to try save some imaging file to the longblob field of mysql table. The codes are:

====
import mysql.connector

filename = 'd:/12.jpeg'
print(filename)

f = open(filename, 'rb')
photo = f.read()
f.close()

mydb = mysql.connector.connect(host = '192.168.1.15', database = 'simon', user = 'simon', password = '1234')
mycur = mydb.cursor()

mycur.execute("insert into brand (imaging) values ('%s')", ([photo]))

mydb.commit()

mycur.close()
mydb.close()

======

When I run the code, it is always responsed some error message:

====
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '???' at line 1
====

I really need help for this matter.

Thank you in advance.

Simon

Python Connector (1 reply)

$
0
0
Hello All,

I am having an issue with the Python connector that I was hoping someone might be able to shed some light on. First off I am running Kubuntu 14.04 LTS with Python 3.4.0 and MySQL 5.5.43. I have manually gone into MySQL to make my tables and columns:

mysql> show tables
-> ;
+---------------------+
| Tables_in_Firewalls |
+---------------------+
| Host |
| Host_History |
+---------------------+
2 rows in set (0.00 sec)

mysql> Describe Host;
+-----------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------+------+-----+---------+----------------+
| Firewall_ID | int(11) | NO | MUL | NULL | auto_increment |
| Session | varchar(30) | NO | | NULL | |
| Hostname | varchar(25) | NO | | NULL | |
| Address | varchar(15) | NO | | NULL | |
| Status | varchar(10) | NO | | NULL | |
| SmartNet | enum('Y','N') | YES | | NULL | |
| Contract_Level | varchar(15) | YES | | NULL | |
| Contract_Expire | date | YES | | NULL | |
| Site_Name | varchar(30) | NO | | NULL | |
| Installed | date | YES | | NULL | |
+-----------------+---------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

mysql> Describe Host_History;
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| Firewall_ID | smallint(6) | NO | | NULL | |
| Implemented | date | NO | | NULL | |
| Make | varchar(10) | NO | | NULL | |
| Model | varchar(10) | NO | | NULL | |
| Serial_Number | varchar(20) | NO | | NULL | |
| Version | varchar(10) | NO | | NULL | |
| Memory | smallint(6) | NO | | NULL | |
+---------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

My script connects to the database just fine with

FirewallDB = mysql.connector.connect(user = 'UserID', password = 'password', host = '127.0.0.1', database = 'Firewalls')
Cursor = FirewallDB.cursor()

Per the manual I then create a string for a query:

Query = ("SELECT Session FROM Host AS H WHERE H.Session = FileTemplate")
Cursor.execute (Query)

I originally had the Query string quoted with two individual lines similar to the example:

query = ("SELECT first_name, last_name, hire_date FROM employees "
"WHERE hire_date BETWEEN %s AND %s")

But I found that Python did not like this even if a + was inserted between the two quoted strings. After altering the Query string a few times based on various error messages, the latest errors I am seeing are:

* Unknown column 'FileTemplate' in 'where clause'

I replaced 'FileTemplate' with a %s and changed my Cursor.execute(Query, (FileTemplate)) so now I am getting:

* mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1

I added a Cursor.commit() after the execute, but this didn't seem to matter. It appears that my variable 'FileTemplate' is going to be seen by MySQL as a column unless I use a %s and substitute. I am unsure of why I am getting a syntax error.

Error 10053 (lost connection) at COMMIT after long SELECT (1 reply)

$
0
0
I'm running a long job in Python which has done a SELECT which returns about 2.2 million rows. The rows are read with "readmany" in blocks of 25,000. A block of rows is read about every 10 minutes. That works fine.

After all the records are read, the program does a COMMIT to release the state held by the SELECT. (Repeatable-read mode.) This fails:

891:58.54 [MainThread] FAILED: OperationalError(2006, "MySQL server has gone away (ConnectionAbortedError(10053, 'An established connection was aborted by the software in your host machine', None, 10053, None))")
Traceback (most recent call last):
File "c:\python34\lib\site-packages\pymysql\connections.py", line 914, in _write_bytes
self.socket.sendall(data)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

Traceback (most recent call last):
File "..\InfoCompanyDb.py", line 179, in reparseindex
self.reparsefails(dbinfo)
File "..\InfoCompanyDb.py", line 337, in reparsefails
self.db.commit()
File "c:\python34\lib\site-packages\pymysql\connections.py", line 705, in commit
self._execute_command(COMMAND.COM_QUERY, "COMMIT")
File "c:\python34\lib\site-packages\pymysql\connections.py", line 957, in _execute_command
self._write_bytes(prelude + sql[:chunk_size-1])
File "c:\python34\lib\site-packages\pymysql\connections.py", line 916, in _write_bytes
raise err.OperationalError(2006, "MySQL server has gone away (%r)" % (e,))
pymysql.err.OperationalError: (2006, "MySQL server has gone away (ConnectionAbortedError(10053, 'An established connection was aborted by the software in your host machine', None, 10053, None))")

This isn't a networking problem; host and client are on the same Windows 7 machine (this is a test environment).
The database connection hasn't been idle; it's been used every 10 minutes for almost 24 hours as rows are read with "readmany". But
no new command has been sent for almost a day. Should the connection time out like that? Is there something I can do during this big SELECT to keep the connection alive?

Windows 7
MySQL Server and client on same machine
Server version: 5.1.47-community MySQL Community Server (GPL)
Python connector PyMySQL

Configuring MySQL Connector/Python to Function with Python 3.4 (1 reply)

$
0
0
I installed MySQL Connector/Python. When I type in python and then enter "import mysql.connector" it works. Now that is with Python 2.7.6.

To access Python 3.4.0, I typed in python3. I then entered "import mysql.connector". In this instance, it failed with the error message: "ImportError: No module named 'mysql'".

How can Python 3.4.0 or the connector be tweaked to successfully import "mysql.connector"?

Adding new sproc to the exisiting database using connectors? (no replies)

$
0
0
Just a little explanation needed.

The project I'm currently working on is mostly pythonic, by this reason I'll likely use mysqldb python connector or sqlalchemy.

The question arose when I've tried to find out the way of adding new sproc to my database via those connectors. I've found a dozens of examples of how to call an existing sprocs from a database but none of how to actually add (create) a new one by them.

I kept on googling and have found text() construct in sqlalchemy, this one enables me to write raw sql code, so IMO, can be used for adding a new sproc to the existing db.

Generally speaking there always is a direct way to do so by using mysql shell or workbench editor. But how can this task be achieved with mysqldb python connector(if at all)?

Is it a good/weird practice to add new sproc that way?

Your help will be highly appreciated.

Thanks in advance, Ivan.

MySQL C API installation Fails (no replies)

$
0
0
Attempting to build mysql-connector-python with the C extensions enabled.

Python module builds correctly, but warns:

"MySQL C API installation invalid (my_config.h not found)"

I've tried with both mysql-connector-c-6.1.6-1.el17.src.rpm and mysql-connector-c-6.1.6-src

Any guidance? Any help would be greatly appreciated.

Platform is Centos6.5 with mysql-devel installed.



Also, version 2.1.1 was a February release, according to documentation. Why is the latest version available via PIP still only 2.0.4?

Connection problem, error message (no replies)

$
0
0
Hi, I would appreciate some help. I have been using a mySQL database called "evette" in my veterinary clinic. This database is installed on the reception computer and I can access it from my consulting room laptop over a wireless network.

I have recently bought new laptops with Windows 8.1 to replace these computers and have MySQL and the database installed successfully on the reception laptop. I have the 2 computers sharing a wireless network. When I try to connect to the database on the reception computer from my consulting room laptop I get the following error message:

Traceback (most recent call last):
File "launch.py", line 304, in <module>
File "db.pyc", line 177, in CreateDatabase
File "db.pyc", line 155, in SendSQL
File "MySQLdb\cursors.pyc", line 166, in execute
File "MySQL\connections.pyc", line 35 in defaulterrorhandler
_mysql_exceptions.OperationalError: (1044, "Acess denied for user 'Reception'@'%' to database 'evette'")

Can anyone advise me what the problem is? I have a limited understanding of these things! Thanks for any suggestions.

ImportError: No module named connect‏ (3 replies)

$
0
0
I have been trying to install the MySQL Connect module for a day and a half.

First I got an error saying that "No module named mysql" was installed. After a lot of trial and error I got passed that. I realized that because I have python installed in /usr/local that I needed to build and then install the library into site-packages where Python was looking. But now it appears that the submodule "connect" cannot be located.

Some people have suggested online that I needed to make sure the module was added to the system path by doing this:

import sys
sys.path.insert(1,'//usr/local/lib/python2.4/site-packages/mysql')
import mysql

Some have suggested that I needed to edit the __init__.py file in /mysql/ with this:
from pkgutil import extend_path
path = extend_path(path, name)

But after I built and then installed pointing to my site-packages directory the __init__.py file in /usr/local/lib/python2.4/site-packages/mysql/ has that code already. The __init__.py in mysql/connector/ has a lot of stuff. It uses dot notation as well which is beyond my rudimentary Python knowledge: for example:
from . import version
from .connection import MySQLConnection

It appears this is related to the install and that I am using /usr/local/lib/python2.4/site-packages. But I am running out of ideas to try. I am not Python expert as you can tell.

Any suggestions?

Again the error in my script where I try to import mysql.connection to connect to mysql is:
import mysql.connect
ImportError: No module named connect

Also I have tried installing it via yum. No go there either.

Queries with python and html (no replies)

$
0
0
I am new to scripting. I am using python and html to create a web interface to update and query mysql on CentOS 6.6. I am using MySQL version 14.14, distibution 5.1.73, python version 2.6.6, apache version 2.2.15.

Here is the python script I have so far and it does not work, error states that not all arguments converted during string formatting:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# enable debugging
import cgitb
cgitb.enable()

import cgi
import MySQLdb as mdb


db = "postfix"
host = "localhost"
port = 3306
dbuser = "dbuser"
dbpass = "dbpassword"
## Entrys from SearchRelay.py
form = cgi.FieldStorage()
DevHost = form.getvalue('DevHost') #This variable is pulled from a previous web page that takes user input


## Verification Page
print "Content-type: text/html"
print

print """
<html>
<head>
<title>Search for a Device</title>
</head>
<body style="background-color:lightgrey">

<h3>Here is your device(s):</h3>
<hr>
"""

## Searching the database
con = mdb.connect(host, dbuser, dbpass, db)

with con:
cur = con.cursor()

query = ("SELECT ip_address, hostname, allowed_external, iser, approver, note, added FROM clients WHERE hostname LIKE %s OR ip_address LIKE %s")
cur.execute(query, (DevHost, DevHost))
data = "%s,%s,%s,%s,%s,%s,%s"
for (ip_address, hostname, allowed_external, iser, approver, note, added) in cur:
print("IP: " % data[0], "Hostname: " % data[1], "External: " % data[2], "ISER: " % data[3], \
"Approver: " % data[4], "Note: " % data[5], "Added On: " % data[6])

cur.close()

MySQL Connector wont install with python 3.5 (4 replies)

$
0
0
I'm on Centos 7. I was running anaconda, but mysql connector doesnt play nice with anaconda. So, after installing the MySQLconnector rpm, I removed anaconda and installed the official Python 3.5. Python 2.7 can find and use mysql connector, but python 3.5 cannot. Is there anyway I can force python 3.5 to find it? I'm not familiar with packages not working right out of the box.

As a side note, python 3.5 installed itself under /usr/local/lib which seems like an odd place.

pip install (2 replies)

$
0
0
Hi,

I am developing one API server for my project and I am using 'pip' to install
relevant modules in python environment.

I used mysql-connector-python 2.0.4 (inside of PIP) but this gave me some error without option
'--allow-all-external'.

Now I am getting error with this option too. So I tried to install mysql-connector-python using pip but in no vein.

So I tried mysql-connector-python-rf module, and it give me an error "error: option --single-version-externally-managed not recognized"

Even though I updated PIP module to the latest, it is not working

In a nutshell, installing Oracle's official mysql-connector-python using PIP
is not supported. I think there may be a way to solve above problems but PIP install error take away so much of my time during the development.

I have environment which I can not always update server environment (AWS Elasticbeanstalk) so installing the python module itself using python command is not that easy for me. PIP install is recommended in this environment. (by Amazon)

"pip install mysql-connector-python"

I hope this just works in later version.

Thanks

MySQL-connector through SSH-tunnel (no replies)

$
0
0
Hi everybody,

I am using windows 7 with Python 3.4 (with Pycharm) and try to acces a remote mySQL-database through SSH with a private key, just like it is working with MySQL-Workbench in the following picture : http://goo.gl/TgwdYI


I use SSHTunnel (https://goo.gl/pGfD30) to setup the SSH-Tunnel as follow :


from sshtunnel import SSHTunnelForwarder

server = SSHTunnelForwarder(
("blablabla.ddns.net", 3307),
ssh_host_key=None,
ssh_username="yoann",
ssh_password=None,
ssh_private_key="E:\id_rsa",
ssh_private_key_password="pass",
remote_bind_address=('192.168.0.24', 3306))

Which works smoothly.


Then I try to connect through the MySQL-connector for Python as follow :

cnx=mysql.connector.connect(user='yoann_builder', password='pass',host="127.0.0.1", port=3307)
cnx.close()

And I got the following error :

mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1:3307' (10061 No connection could be made because the target machine actively refused it)

The problem is, to my understanding, "How to specify to the connector to use the SSH-Tunnel?"

Thanks in advance for your help.

Best Regards

python connector (no replies)

$
0
0
Why are the python connectors restricted to the exact version number? When is the connector for 3.5 comming.
Viewing all 387 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>