>>> e=searchengine.searcher('searchindex.db')
>>> e.getmatchrows('functional programming') select w0.urlid,w0.location,w1.location from wordlocation w0,wordlocation w1 where w0.urlid=w1.urlid and w0.wordid=10 and w1.wordid=17
SyntaxError: invalid syntax
# it highlights the word select in the program
How do I correct this syntax error of the select statement? I am using Python with sqlite3.
You can't just plonk SQL into a python file. This error message is python telling you "I have no idea what select is".
To be more helpful, you'd need to share what library you got "searchengine" from, but no matter what that is, your code is not valid python, so there's no way it's going to work.
Related
I'm writing code that takes a user-inputted string and binds it to a SQLite command through a parameter.
I've gotten this functionality to work when the string is already embedded in the query (see first example under "these lines do work"), and this binding method works with integers just fine (see second example).
testname = "\"EJtheDJ\"" #this would be user-inputted, but I get the same issue when I initialize it this way
#this line doesn't work
c.execute("select lastfm from usernames where discordname=?", (testname,))
#these lines do work
#c.execute("select lastfm from usernames where discordname=\"EJtheDJ\"")
#c.execute("select integer1 from test where integer2=?", (num,)) #num=3
print(c.fetchone()) #prints out "None"
My syntax seems 100% correct according to all the tutorials and forum threads I've looked at. I don't normally make posts here (in fact this is my first time), but I'm completely stumped on this and felt it was my only option. Any help would be massively appreciated. I'm running this on a CentOS server with Python 3.6, if that helps.
Edit: Turns out I just needed to do away with the escaped quotes. testname = EJtheDJ works perfectly.
I have the following sql query:
SELECT
pc.patente,
cs.cpc_group_codigo_cpc_group
FROM
patente_pc pc
,
patente_cpc cpc,
cpc_subgroup cs,
cpc_group cg
WHERE
pc.codigo_patente_pc = cpc.patente_pc_codigo_patente_pc AND
cpc.cpc = cs.codigo_cpc_subgroup AND
cs.cpc_group_codigo_cpc_group = cg.codigo_cpc_group
GROUP BY
pc.patente, cs.cpc_group_codigo_cpc_group
I add this query to python, separating line by line the string in a tuple to not have a problem with the syntax..
and it executes correctly
but when I need to retrieve the data, I use
lista_cpcs = []
lista_patentes = []
for (pc.patente, cs.cpc_group_codigo_cpc_group) in cursor:
lista_cpcs.append(cs.cpc_group_codigo_cpc_group)
lista_patentes.append(pc.patente)
return [lista_cpcs, lista_patentes]
and I get the error Global name 'pc' is not defined
I get whats happening, it's interpreting pc and cs as python modules, but they are from the sql..
how to work in this?
Ps: I search for python mysql connector and didn't found anything with this.
The variables in your for loop: (pc.patente, cs.cpc_group_codigo_cpc_group) are user defined; the names used do not have anything to do with the names from the query. Thus, you can call them whatever you want. You may consider using (pc__patente, cs__cpc_group_codigo_cpc_group) or something.
The problem is that the dot notation in python != the dot notation from your SQL.
Got it, sorry for the post..
Searching in https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-description.html
I learned a few attributes of the cursor, and using the Eclipse debugger I saw that it comes just the name, not the table connection..so I use
for (patente, cpc_group_codigo_cpc_group) in cursor:
lista_cpcs.append(cpc_group_codigo_cpc_group)
lista_patentes.append(patente)
and it's working now, and thanks for the help #Scott
I'm running into an issue with the peewee regexp function. Queries like this work the first time I run it:
query = Table.select().where(Table.column.regexp('string'))
However, if I try to run it with a different string, I get this error: 'unicode' object has no attribute 'regexp':
query = Table.select().where(Table.column.regexp('string2'))
I usually run it twice because I need to see how specific to make the regexp string.
I've tried disconnecting/reconnecting to the db, but that doesn't help. The only thing that works is if I close ipython and restart it. Is this a memory/buffer issue? Is there a good way to get this to work? Thanks in advance.
I'm using web2py to create a page where I search for books based on title/author/keyword/etc. and ISBN, and I can't seem to figure out how to use isbntools in the webapp. I'm sure it's something basic that I'm missing out on, but this is the first webapp that I've ever created, and it's for a class project. This is the related portion of my controller:
from isbntools import *
def index():
form=SQLFORM.factory(
Field('title',label='Try entering a title:'),
Field('author',label='Or an author:'),
Field('ISBN',label='Even better if you have the ISBN'),
Field('fromDate',label='When is the earliest the book might have come out?'),
Field('toDate',label='...and the latest?'))
if form.process().accepted:
titledata = isbn_goom form.vars.title bibtex
authordata = isbn_goom form.vars.author bibtex
isbndata = isbn_meta merge form.vars.ISBN bibtex
print(titledata)
print(authordata)
print(isbndata)
return dict(form=form)
This is a portion of the ticket information I'm getting back:
Error ticket for "Bibbly"
Ticket ID
96.255.27.81.2014-05-01.21-50-27.f66e0b53-b5bd-4621-8dbd-b6f30e8a6af1
invalid syntax (default.py, line 21)
Version
web2py™ Version 2.8.2-stable+timestamp.2013.11.28.13.54.07
Python Python 2.7.5+: /usr/local/bin/uwsgi (prefix: /usr)
Traceback
line 21
titledata = isbn_goom "form.vars.title" bibtex
^
SyntaxError: invalid syntax
isbm_goom is a command line script. Is that what you want?! (you cannot use it in your code like that!)
I suggests you use the most recent version of isbntools and adapt this snipet
from isbntools.contrib.modules.goom import goom
from isbntools.dev.fmt import fmtbib
...
titledata = goom.query(form.vars.title)
for r in titledata:
print((fmtbib('bibtex', r)))
Since python is complaining about a syntax error, it means it's about the literal code you've written. The Python interpreter can't grasp what you mean because you've specified something "impossible" in the language. In this case it's about the white-space after isbn_goom and form.vars.title. Since i don't know and don't see any declaration concerning isbn_goom i assume it's from the isbntools library. To try the library it may be best to learn about it in a separate console session on your own machine. See the pypi page for some examples.
On resolving syntax errors: You can try editing the code in any decent code editor and it will give you hints on these kinds of errors. The default python editor Idle that comes with any default installation would be great.
So I have the following code, and it works:
for count in range(0,1000):
L=[random.randint(0, 127),random.randint(0, 127),random.randint(0, 127)]
random.randint(0, 127)
name=''.join(map(chr,L))
number=random.randint(0,1000)
x.execute('insert into testTable set name=(%s), number=(%s)', (name, number))
Above, x is just the cursor I made (obviously). I just create a random string from ASCII values and a random number and write it to my database (this was a purely BS example so that I knew it worked)/
Then,
I have in another script:
x.execute('insert into rooms set \
room_name=(%s),\
room_sqft=(%s),\
room_type=(%s),\
room_purpose=(%s) ,\
room_floor_number=(%s)',
(name, sqft, roomType, room_use_ranking, floor))
And I get a syntax error: invalid syntax on the first line, right at the x. part of x.execute.
What is different between the two lines? In the problem code, all arguments but name are ints (name is a string) that are gotten from a int(raw_input(...)) type prompt that catches bad input errors.
Clearly this works, but what is going wrong in the second piece of code?
Thanks,
nkk
There's a problem on the line BEFORE the x.execute. (x is unexpected at this point). Can you link more of the file?
Also, try this formatting, which can clear up this sort of thing by making the string one blob. (Your syntax highlighter should show it as one big multi-line string, too!)
sql = '''
INSERT INTO rooms
SET room_name=(%s),
room_sqft=(%s),
room_type=(%s),
room_purpose=(%s),
room_floor_number=(%s)
'''
x.execute(sql, (name, sqft, roomType, room_use_ranking, floor))