top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Updating a filename's counter value failed each time in python

0 votes
900 views

After a user selects a file from the form, that selection of his can be found form reading the variable 'filename'

If the filename already exists in to the database i want to update its counter and that is what i'm trying to accomplish by:

if form.getvalue('filename'):
 cur.execute('''UPDATE files SET hits = hits + 1, host = %s, lastvisit = %s WHERE url = %s''', (host, lastvisit, filename) )

For some reason this never return any data, because for troubleshooting i have tried:

data = cur.fetchone()

if data:
 print("something been returned out of this"_

Since for sure the filename the user selected is represented by a record inside 'files' table why its corresponding counter never seems to get updated?

posted Jun 17, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes
 
Best answer
%s WHERE url = %s''', (host, lastvisit, filename) )

There are (single) quotes missing around (at least) the file name (the 'url' column) which I'm rather sure is a string - you need them around all strings you use in SQL statements.

I don't know which database and interface you're using but I would guess that many have the ability to inserting quotes where neces- sary etc. E.g. with sqlite3 you would use

cur.execute('UPDATE files SET hits = hits + 1, host = ?, lastvisit = ? ' 'WHERE url = ?', (host, lastvisit, filename) )

and the quotes required around (at least) the 'filename' string will be inserted automatically.

Also take care to check the filename you insert - a malicous user might cobble together a file name that is actually a SQL statement and then do nasty things to your database. I.e. never insert values you received from a user without checking them.

> For some reason this never return any data, because for troubleshooting 
> i have tried:

> data = cur.fetchone()

There's nothing that your SQL statement (if correct) would return, so what do you expect to have returned by the fetchone() method?

Perhaps there's something like the 'rowcount' property in sqlite3 which returns the number of rows modified by an INSERT or UPDATE.

> Since for sure the filename the user selected is represented by a record 
> inside 'files' table why its corresponding counter never seems to get 
> updated?

I would guess because you forgot the uotes around string values in your SQL statement which thus wasn't executed.

answer Jun 17, 2013 by anonymous
0 votes

An UPDATE statement isn't a query. There are no results to be fetched. If you want to get results, execute a query (usually a SELECT.)

Also, that print statement is an obvious syntax error. Please post the actual code you're running; don't type it in from memory.

answer Jun 17, 2013 by anonymous
Similar Questions
0 votes

I'm new to Python and trying to run a already written code. Can someone please explain the error below? And if possible, how do I resolve this?

Traceback (most recent call last):
 File "c:Project_1regression_1.py", line 7, in  from sklearn import metrics, cross_validation, linear_model
 File "c:Python27libsite-packagessklearnmetrics__init__.py", line 31, in from . import cluster
 File "c:Python27libsite-packagessklearnmetricscluster__init__.py", line 8, in 
 from .supervised import adjusted_mutual_info_score
 File "c:Python27libsite-packagessklearnmetricsclustersupervised.py", line 19, in 
 from .expected_mutual_info_fast import expected_mutual_information
 File "expected_mutual_info_fast.pyx", line 10, in init sklearn.metrics.cluster
.expected_mutual_info_fast (sklearnmetricsclusterexpected_mutual_info_fast.c: 4886)
 File "c:Python27libsite-packagesscipyspecial__init__.py", line 529, in <module>
 from ._ufuncs import *
ImportError: DLL load failed: The specified module could not be found.
+1 vote

$ python -m pip install MySQL-python

Collecting MySQL-python
 Downloading MySQL-python-1.2.5.zip (108kB)
 100% | –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ –ˆ| 112kB 260kB/s
 930 [main] python2.7 12948 child_info_fork::abort: address space needed by 'datetime.dll' (0x870000) is already occupied
 Error [Errno 11] Resource temporarily unavailable while executing command python setup.py egg_info
Exception:
Traceback (most recent call last):
 File "/usr/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
 status = self.run(options, args)
 File "/usr/lib/python2.7/site-packages/pip/commands/install.py", line 324, in run
 requirement_set.prepare_files(finder)
 File "/usr/lib/python2.7/site-packages/pip/req/req_set.py", line 380, in prepare_files
 ignore_dependencies=self.ignore_dependencies))
 File "/usr/lib/python2.7/site-packages/pip/req/req_set.py", line 634, in _prepare_file
 abstract_dist.prep_for_dist()
 File "/usr/lib/python2.7/site-packages/pip/req/req_set.py", line 129, in prep_for_dist
 self.req_to_install.run_egg_info()
 File "/usr/lib/python2.7/site-packages/pip/req/req_install.py", line 439, in run_egg_info
 command_desc='python setup.py egg_info')
 File "/usr/lib/python2.7/site-packages/pip/utils/__init__.py", line 667, in call_subprocess
 cwd=cwd, env=env)
 File "/usr/lib/python2.7/subprocess.py", line 390, in __init__
 errread, errwrite)
 File "/usr/lib/python2.7/subprocess.py", line 917, in _execute_child
 self.pid = os.fork()
OSError: [Errno 11] Resource temporarily unavailable

Anyone hit similar issue?

+1 vote

I want to find the maximal number of elements contained in a nested dictionary, e.g.

data = {
 'violations':
 {
 'col1': {'err': [elem1, elem2, elem3]},
 'col2': {'err': [elem1, elem2]}
 }
 }

so to find the maximal number of elements in the lists for key 'err' in key 'col1' and 'col2'. Also key 'violations' may contain many keys (e.g. 'col1' , 'col2', 'col3' etc), so what's the best way to do this (using a loop)?

max = 0for col in data.violations:
 if max < len(data.violations.col.err):
 max = len(data.violations.col.err)
+1 vote

For example gcd(3, -7) returns -1, which means that a co-prime test that would work in many other languages 'if gcd(x, y) == 1' will fail in Python for negative y.

And, of course, since -|x| is less than |x|, returning -|x| rather than |x| is not returning the greatest common divisor of x and y when y is negative.

...