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

TypeError when inserting a date (no replies)

$
0
0
Hi,

As far as I know I can insert a date into MySQL as long as the format corresponds to the date field attribute.
My date column in MySQL is declared as datetime.
So if I insert a date like '2013-04-21 11:29:00' into it it should work fine.
Indeed when I insert using phpmysql it works but when I insert from my python programm it fails with the following error message :

Traceback (most recent call last):
File "C:\Users\pidlas\Documents\rudder_find.py", line 58, in <module>
extract(info)
File "C:\Users\pidlas\Documents\rudder_find.py", line 46, in extract
cursor.execute(query, (serveur, etat, directive, regle, date,))
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 183, in execute
query = query % db.literal(args)
TypeError: not all arguments converted during string formatting

I have tried different ways of formatting my python date variable but I could not until now found the right thing to do.

I would appreciate any ideas or thoughts.

Regards.

My python code follows :

# -*- coding: utf-8 -*-
# modules nécessaires
from os import chdir
import MySQLdb as mdb
import sys
import datetime

# variables pour la BDD
serveur = ''
etat = ''
regle = ''
directive = ''

# répertoire où se trouve les logs
chdir("c:/Users/pidlas/Documents/rudder/")

def extract(log):
for ligne in log:
debut_date = ligne.find('[')
fin_date = ligne.find(']')
date = ligne[debut_date+1:fin_date-5]
date = datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S")
sep_N = ligne.find('N:')
serveur = ligne[ligne.find('[',sep_N)+1:ligne.find(']',sep_N)]
sep_S = ligne.find('S:')
etat = ligne[ligne.find('[',sep_S)+1:ligne.find(']',sep_S)]
sep_R = ligne.find('R:')
regle = ligne[ligne.find('[',sep_R)+1:ligne.find(']',sep_R)]
sep_D = ligne.find('D:')
directive = ligne[ligne.find('[',sep_D)+1:ligne.find(']',sep_D)]

try:
db = mdb.connect('localhost', 'root', '', 'rudder');
cursor = db.cursor()
query = """INSERT IGNORE INTO serveur (nom_serveur) VALUES (%s)"""
cursor.execute(query, (serveur,))
query = """INSERT IGNORE INTO etat (nom_etat) VALUES (%s)"""
cursor.execute(query, (etat,))
query = """INSERT IGNORE INTO directive (nom_directive) VALUES (%s)"""
cursor.execute(query, (directive,))
query = """INSERT IGNORE INTO regle (nom_regle) VALUES (%s)"""
cursor.execute(query, (regle,))
query = """INSERT INTO serveur_etat_directive_regle (serveur, id_etat, id_directive, id_regle, date)
SELECT id_serveur, id_etat, id_directive, id_regle FROM serveur, etat, directive, regle
WHERE nom_serveur = %s AND nom_etat = %s AND nom_directive = %s AND nom_regle = %s"""
cursor.execute(query, (serveur, etat, directive, regle, date,))
except mdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
db.commit()

info.close()
db.close()
return logfile

if __name__ == "__main__":
with open('non-compliant-reports.log','r') as info:
extract(info)

INSERT with SELECT (1 reply)

$
0
0
Hi,

I run a python program which takes a flat log file as input to load it into a database in order for administrators to monitor their application.

I have five tables : server table with two columns (id and name), status table with two columns, directive table with two columns, rule table with two columns and finally a server_status_directive_rule table with five columns (id_server, id_status, id_directive, id_rule, date).

I can easily insert into the first four tables.
My problem is with trying to insert a variable into the fifth table.
The code is :
<code>
query = """INSERT INTO serveur_etat_directive_regle (id_serveur, id_etat, id_directive, id_regle, date)
SELECT id_serveur, id_etat, id_directive, id_regle, %s FROM serveur, etat, directive, regle
WHERE nom_serveur = %s AND nom_etat = %s AND nom_directive = %s AND nom_regle = %s"""
cursor.execute(query, (serveur, etat, directive, regle, date,))
</code>

The problem is with the date column. If I remove it from my INSERT and subsequently in the SELECT and cursor.execute, the table is filled in properly.

The other thing to know is I have no error message returned although I do manage error messages. My python code runs with no messages and my table is still empty at the end.

The complete SQL code is below :
<code>
try:
# accès et mise à jour de la BDD
db = mdb.connect('localhost', 'root', '', 'rudder');
cursor = db.cursor()
query = """INSERT IGNORE INTO serveur (nom_serveur) VALUES (%s)"""
cursor.execute(query, (serveur,))
query = """INSERT IGNORE INTO etat (nom_etat) VALUES (%s)"""
cursor.execute(query, (etat,))
query = """INSERT IGNORE INTO directive (nom_directive) VALUES (%s)"""
cursor.execute(query, (directive,))
query = """INSERT IGNORE INTO regle (nom_regle) VALUES (%s)"""
cursor.execute(query, (regle,))
query = """INSERT INTO serveur_etat_directive_regle (id_serveur, id_etat, id_directive, id_regle, date)
SELECT id_serveur, id_etat, id_directive, id_regle, %s FROM serveur, etat, directive, regle
WHERE nom_serveur = %s AND nom_etat = %s AND nom_directive = %s AND nom_regle = %s"""
cursor.execute(query, (serveur, etat, directive, regle, date,))
except mdb.Error, e:
print "Error %d: %s" % (e.args[0],e.args[1])
sys.exit(1)
db.commit()
</code>

Any help would be much appreciated.

KR.

MySQL TRIGGER and python (1 reply)

$
0
0
Hello,
I'm trying to understand how the MySQL TRIGGER work and if they can be useful for my purpose.

I would like to monitor the UPDATE of data in a table.
trying to print on screen the changed data and all the line pertaining to.

could I have an example of TRIGGER and python?

Thank you very much

Luca

sys_exec () can run a python program (no replies)

$
0
0
hello,

for test reasons, I have a python program who writes to a file. txt date and the current time.


select sys_exec('python /var/test/script/aa.py');

I gave chown mysql:mysql aa.py and chmod 777 aa.py

result is:
+-------------------------------------------+
| sys_exec('python /var/test/script/aa.py') |
+-------------------------------------------+
| 0 |
+-------------------------------------------+
1 row in set (0.23 sec)



but nothing appears in the text file

will you give me some suggestions please?

thanks

Installer can't detect Python (5 replies)

$
0
0
Hi. I'm new to this forum but I'll try to be as clear as possible.

I'm trying to install MySQL on my company's lab computer which runs Windows XP 32-bit. During the installation, however, the python connector cannot install because the installer thinks I don't have python installed on my machine, even though I do. Also note that Windows knows I have python because when I type "python" in cmd, the python shell runs just fine.

I did install Python with the option "install for all users", yet, in the windows registry, Python appears under "current user", not in "local machine", and I think that might be the problem.

I'm scared of modifying windows registry myself and reinstalling python is not an option because of all the add-ons others have installed that I don't want to mess with.

Does anyone know of a safe solution to get the MySQL installer to detect the presence of python?

thanks

How to use a new MySQL 5.6 --login-path parameter from scripts (no replies)

$
0
0
Hi,
I am trying to find a way how to use a new MySQL 5.6 encrypted credential file mylogin.cnf from python scripts.

I checked MySQLdb and MySQL connector libraries and didn't find any information so far. On the other hand all MySQl 5.6 command line tools like mysql, musqldump and etc. have this ability by specifying this parameter --login-path.

Of course I can write a wrapper to mysql command line client but I want to use native libraries such as MySQLdb or mysql connector.

Any ideas?

pip installs broken every time a new release is made (1 reply)

$
0
0
I use mysql-connector-python in a number of projects that use pip to set up virtualenvs. Every single time there is a new release of mysql-connector-python, I stop being able to build my application because the old version is removed from your CDN. Can you please fix this practice?

And then it's made worse since the updates to pypi don't happen concurrently with the release so I can't even "fix" by rolling forward. Not that rolling forward is always an option as there may be different behaviors and bugs present in the new release.

Paython and Scrapy: error in your SQL syntax (no replies)

$
0
0
Python: I need a help for insert with Scrapy to MySQL.
This is my error, Traceback:
2013-09-13 13:08:18-0700 [scrapy] Unhandled Error
Traceback (most recent call last):
File "C:\python27\lib\threading.py", line 781, in __bootstrap
self.__bootstrap_inner()
File "C:\python27\lib\threading.py", line 808, in __bootstrap_inner
self.run()
File "C:\python27\lib\threading.py", line 761, in run
self.__target(*self.__args, **self.__kwargs)
--- <exception caught here> ---
File "C:\python27\lib\site-packages\twisted\python\threadpool.py", lin
e 191, in _worker
result = context.call(ctx, function, *args, **kwargs)
File "C:\python27\lib\site-packages\twisted\python\context.py", line 1
18, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)

File "C:\python27\lib\site-packages\twisted\python\context.py", line 8
1, in callWithContext
return func(*args,**kw)
File "C:\python27\lib\site-packages\twisted\enterprise\adbapi.py", lin
e 448, in _runInteraction
result = interaction(trans, *args, **kw)
File "apple\pipelines.py", line 44, in _conditional_insert
tx.execute('select * from `job` where day = %s' % item['day'])
File "C:\python27\lib\site-packages\MySQLdb\cursors.py", line 202, in
execute
self.errorhandler(self, exc, value)
File "C:\python27\lib\site-packages\MySQLdb\connections.py", line 36,
in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQ
L syntax; check the manual that corresponds to your MySQL server version for the
right syntax to use near '[u'September 13']' at line 1")

2013-09-13 13:08:18-0700 [apple] INFO: Closing spider (finished)
2013-09-13 13:08:18-0700 [apple] INFO: Dumping Scrapy stats:
{'downloader/request_bytes': 223,
'downloader/request_count': 1,
'downloader/request_method_count/GET': 1,
'downloader/response_bytes': 14674,
'downloader/response_count': 1,
'downloader/response_status_count/200': 1,
'finish_reason': 'finished',
'finish_time': datetime.datetime(2013, 9, 13, 20, 8, 18, 579000),
'item_scraped_count': 434,
'log_count/DEBUG': 441,
'log_count/ERROR': 434,
'log_count/INFO': 4,
'response_received_count': 1,
'scheduler/dequeued': 1,
'scheduler/dequeued/memory': 1,
'scheduler/enqueued': 1,
'scheduler/enqueued/memory': 1,
'start_time': datetime.datetime(2013, 9, 13, 20, 6, 57, 904000)}
2013-09-13 13:08:18-0700 [apple] INFO: Spider closed (finished)


This is my pipelines.py


from scrapy.exceptions import DropItem
from scrapy.exceptions import DropItem
from scrapy import log
from twisted.enterprise import adbapi

import MySQLdb.cursors

class ApplePipeline(object):

def process_item(self, item, spider):
for word in self.words_to_filter:
if word in unicode(item['team1']).lower():
raise DropItem("Contains forbidden word: %s" % word)
else:
return item

class MySQLStorePipeline(object):

def __init__(self):
# @@@ hardcoded db settings
# TODO: make settings configurable through settings
self.dbpool = adbapi.ConnectionPool('MySQLdb',
host='127.0.0.1',
db='mydb',
user='myuser',
passwd='mypass',
cursorclass=MySQLdb.cursors.DictCursor,
charset='utf8',
use_unicode=True
)

def process_item(self, item, spider):
# run db query in thread pool
query = self.dbpool.runInteraction(self._conditional_insert, item)
query.addErrback(self.handle_error)

return item

def _conditional_insert(self, tx, item):
# create record if doesn't exist.
# all this block run on it's own thread
# aici extrage prima echipa
tx.execute('select * from `scoruri` where day = %s' % item['day'])
result = tx.fetchone()
if result:
log.msg("Item already stored in db: %s" % item, level=log.DEBUG)
else:
tx.execute(\
"INSERT INTO `scoruri` (competition, day, hour, score, team1, team2) "
"values (%s, %s, %s, %s, %s, %s)",
(item['competition'],
item['day'],
item['hour'],
item['score'],
item['team1'],
item['team2'],
)
)
log.msg("Item stored in db: %s" % item, level=log.DEBUG)

def handle_error(self, e):
log.err(e)

Thanks in advance for any help!

MySQL Workbench export tool for SQLAlchemy Python ORM (no replies)

Need help retrieving boolean value using MySQLdb (1 reply)

$
0
0
I have code written in python that accesses a Mysql database via the Mysqldb library. I can succesfully connect to the database and execute a query which then returns a value representing how many rows in the table matched the query requirments. What I can't seem to figure out is how to return the actuall value of the boolean feild I'm trying to access. Here is my code snippet.

import MySQLdb

db = MySQLdb.connect(host="localhost", user="pi", passwd="raspberry", db="django_website_db")
cur = db.cursor()

while (1):

update_kill = cur.execute("SELECT preprogramUpdate FROM website_preprogram WHERE preprogramNumber= 3")
rows = cur.fetchall()
for row in rows:
print row[0]

print update_kill

So to reiterate, I'm trying to return the actuall value of "preprogramUpdate" in my query.....

Thanks in advance for any help!

mysql-python 1.2.4 has hardcoded dependency on distribute 0.6.28 (no replies)

$
0
0
We are trying to use an internal PyPi server so we can be fully self-contained and not have to go through our proxy to connect to the global server. However, we've found that mysql-python 1.2.4 has a hardcoded distribute version and URL to the global PyPi server in distribute_setup.py. This is forcing us to continue to use our proxies to be able to connect to the global server to pull in distribute 0.6.28 when we install 1.2.4 from our local PyPi server.

From MySQL-python-1.2.4/distribute_setup.py, lines 49 and 50:

DEFAULT_VERSION = "0.6.28"
DEFAULT_URL = "http://pypi.python.org/packages/source/d/distribute/&quot;

I don't see this requirement in 1.2.3.

What gives?

Unable to install Connector/Python 1.1.4 (1 reply)

$
0
0
I have Python 3.3 installed and now I want to install Connector/Python 1.1.4. The documentation says that "On Microsoft Windows systems, you can download Connector/Python as a Zip archive from http://dev.mysql.com/downloads/connector/python/.&quot;. However, there is no ZIP archive, there are only several MSI files. So, I choose mysql-connector-python-1.1.4-py3.3.msi for my purposes. If I unzip it, I see that there is no setup.py file, so this line from documentation "Inside the Connector/Python folder, perform the installation using this command: shell> python setup.py install" also has no sense. Anyway, when I doubleclock on mysql-connector-python-1.1.4-py3.3.msi the installer does not ask me where to install the package, after intallation I only see a new folder "C:\Program Files (x86)\MySQL\MySQL Connector Python" which contains only one PDF file, one HTML file and two TXT files. So, the documentation does not provide any help and I do not know what to do with all this. How to check in Python, whether it is ok now, or not.

Upgrade to Python Connector Fails (2 replies)

$
0
0
Windows 8.1
MySQL 5.6.16 64 bit

Upgrade of Connector/Python 2.7.1.0.12 to 2.7.1.1.5 fails.

It cannot find python.exe in D:\Python27

I have set the path D:\Python27 in environment variables and can run it.

Previously I had uninstalled the version of Python that was in D:\Python27 and installed Anaconda version of Python instead.
That seemed to explain why the upgrade failed.

I reinstalled a new version of Python 2.7.2, 32 bit, msi from python.org in D:\Python27 and set the path variable to D:\Python27.

I rebooted and tried again to upgrade the connector but the error remains that it still cannot find Python 2.7.2

Error Message:
2: Action 13:59:21: INSTALL.
2: 1: MySQL Connector Python v1.1.5 for Python v2.7 2: {B68B3870-6511-4D36-97BD-189A761A2728}
2: Action 13:59:21: FindRelatedProducts. Searching for related applications
2: Action 13:59:21: AppSearch. Searching for installed applications
2: Action 13:59:21: SetInstallDir.
2: Action 13:59:21: LaunchConditions. Evaluating launch conditions
2: Python v2.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.
2: 1: MySQL Connector Python v1.1.5 for Python v2.7 2: {B68B3870-6511-4D36-97BD-189A761A2728} 3: 3
2:
2: Final actions.
2: Update failed

Getting the output parameter after calling a stored procedure! (2 replies)

$
0
0
1. I did follow the example shown in the developer guide (page 45) as:

stored procedure:

CREATE PROCEDURE multiply(IN pFac1 INT, IN pFac2 INT, OUT pProd INT)
BEGIN
SET pProd := pFac1 * pFac2;
END

python code:

procedure_name = 'multiply'
arguments = (5, 5, 0)
cursor = mysql_connect.cursor()
cursor.callproc(procedure_name, arguments)
print(arguments)

and printing of the arguments list does not show me output parameter pPro. I got the same entry as:
(5, 5, 0)

How did you get the value ('5', '5', 25L)?

2. Same situation with a string as output parameter:

stored procedure:

CREATE PROCEDURE magazine.`string_concatenation`(IN pA varchar(50), IN pB varchar(50), OUT pC varchar(50))
BEGIN
SET pC = CONCAT(pA, pB);
END;

python code:

procedure_name = 'string_concatenation'
arguments = ('A', 'B', '')
cursor = mysql_connect.cursor()
cursor.callproc(procedure_name, arguments)
print(arguments)

This code will print: ('A', 'B', '')

How do I show the value of pC in python code?

Is the mysql_connect.commit() always require with Insert, Update and Delete records? (3 replies)

$
0
0
I’m trying to insert a new record into the magazine_type table following the code below:

sql = ('INSERT INTO magazine_type (type, description, notes, update_id) VALUES (%s, %s, %s, %s)')
paremeters = ('Lunix', 'The Lunix Magazine', 'The Notes', 300)
cursor = mysql_connect.cursor()
cursor.execute(sql, parameters)

The record will not be inserted until the commit() method of the connection object is done as:

mysql_connect.commit()

Is the mysql_connect.commit() always required with Insert, Update and Delete records?

mysql util 1.4.1 and connect-1.2.0 can not in the same host (4 replies)

$
0
0
/usr/local/lib/python2.7/site-packages/mysql/connector/errors.py

first:
mysql-connector-python-1.2.0 and mysql-utilities-1.4.1 can't in the same host????

my test found a problem,
they have the the errors.py so....

/usr/local/lib/python2.7/site-packages/mysql/connector/errors.py

How to get the value of aggregate functions using MySQL Connector/Python? (2 replies)

$
0
0
Sometimes we need get the value of the MySQL aggregate functions like COUNT, MAX, etc.
For example, if I need to know how many records has a table, I could write the following code:

mysql_connect = mysql.connector.connect(user=’ernest’, database=’magazine’)
sql = ‘SELECT COUNT(*) FROM author’
cursor = mysql_connect.cursor()
cursor.execute(sql)
data = cursor.fetchall()
print(data)

This will print:

[(20,)]

Two questions:

1. How to get the real 20 records value?

2. If this the correct code to get the values of the MySQL aggregate functions?

Thank you in advanced!

Ernest

Where was the Connector/Python installed in Windows PC? (2 replies)

$
0
0
I have downloaded and installed the Connector/Python (mysql-connector-python-1.1.6-py3.3.msi) file in Windows 7. I can’t find the location path where it was installed. Can I look at the code?

Thanks

MySQLConnection.cmd_query(statement) Example (2 replies)

$
0
0
Hi,

I would like to see an example code of the method MySQLConnection.cmd_query(statement). Is this method function as the command object in MySQL/.NET Connector?

Thanks

MySQL Connector/Python .dmg installation on osx “succeeds” but I cannot import (or even find!) the module (no replies)

$
0
0
I downloaded Mysql Connector/Python 1.1.6 .DMG for OSX http://dev.mysql.com/downloads/connector/python/

When I run it, it says "installation successful" but gives no other installation or diagnostics.

But when I try to verify the successful installation http://dev.mysql.com/doc/connector-python/en/connector-python-verification.html

it says cannot find the module


vt102:~ xxxxx$ python

Python 2.7.6 |Anaconda 1.9.2 (x86_64)| (default, Jan 10 2014, 11:23:15)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import mysql.connector

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mysql.connector
Viewing all 387 articles
Browse latest View live


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