top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

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

0 votes
78 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 Google+ Share Button LinkedIn Share Button Multiple Social 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

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.

0 votes

This is my dilemma, Im trying to get the generated JSON file using the bing api search.

This is the code that Im executing from inside the shell:
http://bin.cakephp.org/view/460660617 [1]

The port doesnt matter to me. Thoughts?

0 votes

I am running a python script and it will create a file name like filename0.0.0 and If I run it again then new file will create one more like filename0.0.1......
my code is-

i = 0
for i in range(1000):
 try:
 with open('filename%d.%d.%d.json'%(0,0,i,)): pass
 continue
 except IOError:
 dataFile = file('filename%d.%d.%d.json'%(0,0,i,), 'a+')
 break

But It will take more time after creating many files, So i want to store value of last var "i" in a variable so that when I run my script again then I can use it. for example-
my last created file is filename0.0.27 then it should store 27 in a variable and when i run again then new file should be created 0.0.28 according to last value "27", so that i could save time and it can create file fast..


Useful Links with Similar Problem
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...