I am new to SQL, so maybe this is a newbie question. But here is my simple code(also, I am using python):
#classmethod
def next_page(cls):
cls.cur.execute("SELECT * FROM Posts WHERE Id < 10 ORDER BY Date DESC Limit 10")
rows = cls.cur.fetchall()
return rows
When I run this I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "database.py", line 20, in next_page
rows = cls.cur.fetchall()
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 2 supplied
Any one know what the deal with this is? Any help is appreciated
Turns out(since I was running the code in the REPL) that you have to CTRL+C and enter the REPL again when you modify a file.
Related
Edit: Thank you for your helpful answers! I downloaded Python 3.7.0 but you are right, my Mac is running Python 2.7. I have homework now :) Figure out how to get it running 3.7. I will come back if I have more questions. Thank you!
Beginner here. I'm getting NameError when executing in Mac with Python Launcher. When testing in Python 3.7.0 Shell it works ok. I've read other answers to NameError questions, but do not understand what I'm doing wrong. Help is appreciated.
Code used
first_name = input ("Hi, what's your first name? ")
print ("Hi," , first_name)
Error received
Traceback (most recent call last):
File "/Users/imperio/Documents/pythonpractice/Name.py", line 1, in <module>
first_name = input ("Hi, what's your first name? ")
File "<string>", line 1, in <module>
NameError: name 'Imperio' is not defined
This is most likely because you are not executing it using Python 3+.
Please check the output of python -V to see which version you are executing your code with.
On mac you may already have both installed, Python 3 is sometimes aliased under python3 file.py
Here's your program converted to valid Python2:
first_name = raw_input ("Hi, what's your first name? ")
print ("Hi, {}".format(first_name))
You're running Python 2. In Python 2, input executes the input. raw_input just returns the string entered.
Here's an example:
>>> x = 1
>>> y = 2
>>> z = 3
>>> print input('variable? ')
variable? x # Note output is the value of the variable
1
>>> print input('variable? ')
variable? w # Variable 'w' doesn't exist
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'w' is not defined
>>> print raw_input('variable? ') # Raw input just returns the input.
variable? x
x
I have a fully working programme that I wish to run. It executes on my friend's laptop, but not mine, (I've saved it to my documents folder) the following is the program:
def DigitCount(n):
#how many decimal digits in integer 'n'
if n<0:
n=-n
digitCount=1
powerOfTen=10
while powerOfTen<=n:
digitCount+=1
powerOfTen*=10
return digitCount
But I keep getting the following error:
>>> DigitCount(100)
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
DigitCount(100)
NameError: name 'DigitCount' is not defined
Wait, are you saying you do the following from the command line?
$ python DigitCount.py
$ python
>>> DigitCount(100)
That won't work. You have to do this:
$ python
>>> import DigitCount
>>> DigitCount.DigitCount(100)
so here is my problem, I am trying to create a function in Python that refers to another function
Here is my code:
def movies_creation():
for director in directors:
for i in range((int(director.age)-20)/random.randint(1,5)):
movie = MOVIE([], random.randint(1960, 2015),
random.choice(movie_genre), 0, [], director, 0, 0, 0)
movie_title_type = random.randint(1,40)
if movie_title_type == 1:
title_colors()
director.filmography.append(movie)
def title_colors():
movie.name.append(random.choice(title_colors))
Now when I try to run this code I get this error message:
Traceback (most recent call last):
File "C:\Users\Patrick\Pictures\Python\TS\gui.py", line 7, in
movies_creation()
File "C:\Users\Patrick\Pictures\Python\TS\Movies.py", line 401, in movies_creation
title_colors()
File "C:\Users\Patrick\Pictures\Python\TS\Movies.py", line 343, in title_colors
movie.name.append(random.choice(title_colors)) NameError: global name 'movie' is not defined
Not sure what I am doing wrong...
Problem in your code is that you should pass the movie variable to the method title_colors:
title_colors(movie)
def title_colors(m):
m.name.append(random.choice(title_colors))
Thanks for your help, my problem was my function had the same name than a list in my program. changed the name and everything works fine now
I'm trying to learn Python and have problem to understand how to use the aggregate functions with peewee.
In my code I first do imports like:
import sys
from datetime import datetime, timedelta
from peewee import *
from database import (db_init, MF_Djur, MF_Logg, MF_Senaste_Koll, MF_Foderspec)
To test if peewee and database connection works, I have used the following code successfully:
antalgivor24h = MF_Logg.select() \
.where((MF_Logg.Logg_RFID_ID == tag_no) & (MF_Logg.Logg_Tid > timelastday)).count()
print(antalgivor24h)
To my problem: I would like to use sum function and this is where I get problems. I want to do this sql in peewee format:
SELECT SUM(`Logg_Giva_Foder1`)
FROM mf_logg
WHERE `Logg_RFID_ID`='752007904107005' AND `Logg_Tid` >= (CURDATE() - INTERVAL 24 HOUR)
In peewee docs (http://peewee.readthedocs.org/en/latest/peewee/querying.html#aggregating-records) I have seen the following example code:
query = (User
.select()
.annotate(
Tweet,
fn.Max(Tweet.created_date).alias('latest_tweet_date')))
Based on this I tried:
totalgiva124h = MF_Logg.select() \
.where((MF_Logg.Logg_RFID_ID == tag_no) & (MF_Logg.Logg_Tid > timelastday)) \
.annotate(MF_Logg, fn.Sum(MF_Logg.Logg_Giva_Foder1))
This code gives me the following error:
Traceback (most recent call last):
File "testscript.py", line 32, in <module>
.annotate(MF_Logg, fn.Sum(MF_Logg.Logg_Giva_Foder1))
NameError: name 'fn' is not defined
I have googled this error but for peewee aggregate records I couldn't get much help (on general Python nameerrors I found a lot but nothing that helped me). However, on one page I read that it could help importing peewee fn separately. I therefore tried adding
from peewee import *, fn
but then I get the following error, so no luck:
Traceback (most recent call last):
File "testscript.py", line 32, in <module>
.annotate(MF_Logg, fn.Sum(MF_Logg.Logg_Giva_Foder1))
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1763, in annotate
query = query.ensure_join(query._query_ctx, rel_model)
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1522, in ensure_join
query = self.switch(lm).join(rm, on=on).switch(ctx)
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 199, in inner
func(clone, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 1505, in join
self._query_ctx, model_class))
ValueError: No foreign key between <class 'database.MF_Logg'> and <class 'database.MF_Logg'>
I hope someone knows how to write the query in a correct way. Any help is appreciated.
Annotate is not what you want in this case. Instead try:
MF_Logg.select(fn.SUM(MF_Logg.Logg_Giva_Foder1)) \
.where((MF_Logg.Logg_RFID_ID == tag_no) & (MF_Logg.Logg_Tid > timelastday)) \
.scalar()
I have written a return function for my group project.
I am using python 3.4 and wrote this:
def readrouter(x, y):
conn = sqlite3.connect('server.db')
cur = conn.cursor()
cur.execute("SELECT DISTINCT command FROM router WHERE
function =? or type = ? ORDER BY key ASC",(x, y))
read = cur.fetchall()
return read;
a = input("x:")
b = input("y:")
for result in readrouter(a,b):
print (result[0])
As my major member is using 2.7 and I need to follow his version now.
After I re-input my .py into python 2.7
there is a error:
x:create vlan
Traceback (most recent call last):
File "C:/Users/f0449492/Desktop/2015225/database.py", line 322, in <module>
a = input("x")
File "<string>", line 1
create vlan
^
SyntaxError: unexpected EOF while parsing
Process finished with exit code 1
how to fix this bug?
In Python 2.7, replace input() with raw_input().
The former runs eval() on the input string and expects valid Python code as input. Your input create vlan isn't valid Python and can't be eval'ed. The latter just returns a string with no further processing.
As a follow up - to ensure compatibility with both Python branches you may use six .