I'm pretty new to SQLite and Python and have run into a bit of confusion. I'm trying to return all elements in a column that contain a substring which is passed to a function as a variable in Python. My code is running, but it's returning an empty result instead of the correct result.
Here's the code with the names generalized:
def myFunc(cursor,myString):
return cursor.execute("""select myID from Column where name like '%'+?'%' """,(myString,))
Like I said, the code does run without error but returns an empty result instead of the result that I know it should be. I'm assuming it has something to do with my use of the wildcard and/or question mark, but I can't be sure. Anyone have any ideas? Thanks in advance for your time/help! Also, this is my first post, so I apologize in advance if I missed any of the recommended protocols for asking questions.
Well, '%'+?'%' definitely isn't going to work—you're trying to concatenate with + on the left, but with no operator…
You can compute LIKE-search fields if you do it right—'%'+?+'%', in this case. That will cause problems with some databases (from not working, to doing a less efficient search), but, at least according to CL.'s comment, sqlite3 will be fine.
But the easy thing to do is to just substitute a complete parameter, rather than part of one. You can put % into the parameters, and it'll be interpreted just fine. So:
return cursor.execute("""select myID from Column where name like ?""",
('%'+myString+'%',))
And this also has the advantage that if you want to do a search for initial substrings ('foo%'), it'll be the same SQL statement but with a different parameter.
Try this:
def myFunc(cursor,myString):
return cursor.execute('select myID from Column where name like "{0}"'.format(myString))
Related
I'm using the search_repositories() function from the Pygithub package and am looking to build a query something like this in pseudo code:
('keyword1' OR 'keyword2) AND ('keyword3' OR 'keyword4') AND last_push_date > '2020-01-01'
So far I've not worked out the correct way to break up the clauses according to the logic of the parentheses and also have found that if I have any boolean logic, the 'qualifier' (i.e. the pushdate:>'2020-01-01') seems to break the query string and return no results.
I'm aware from the docs that you cannot have more than 5 AND/OR operators but in my testing that wasn't the case.
It seems that for instance I can do something like:
'"firstword secondword" OR "thirdword fourthword"' and it treats each as a phrase correctly, but adding a qualifier afterwards then returns no results - even if I put the pushed date to way back.
Also this seems to work:
'oneword anotherword+pushed:>2020-01-01'
But combining such logic just returns 0 results.
Any thoughts?
Here is what I'm trying to do. The database is Postgres.
numbers is a Python set() or some other iterable.
session.execute(table.update().\
where(func.substring(table.c.number,1 ,5) in numbers).\
values(provider="testprovider")
The problem with this is that in numbers doesn't seem to work. It will work for a single number if I do something like where(func.substring(table.c.number,1, 5)== '12345')
I've googled a lot on how to do WHERE IN with sqlalchemy, but all of them are with a SELECT query or do use the .in_() function on the column itself. For example this answer: link
The documentation also says the .in_() only works on the columns.
So, I don't really know how to proceed here. How can I write this expression?
Ah nevermind, although the documentation doesn't say it, I can use the .in_() function on the result of func.substring.
So
where(func.substring(table.c.number, 1, 5).in_(numbers))
worked.
Here's my issue. I have a bunch of data organized like this:
(bad_table)
Name.......Thing...... Count
Dave........Houses.......1
Bob..........Cars............2
Jeff...........Boats..........1
Bob..........Houses.......1
Dave........Kids............3
Jeff..........Cars............3
And I’d like to have it organized like this:
(good_table)
Name.....Houses.......Boats.....Cars
Dave...........1................0.............0
Bob.............1................0.............2
Jeff..............0................1.............3
This is simplified; I’d actually like to make a table with hundreds of Things/Columns, but you get the idea.
My approach has been to create a good_table with columns for Name, Houses, Boats, Cars, and rows for Dave, Bob and Jeff, but with no other values filled in.
Then for each Name, I would create a list of tuples (I think) from the original bad_table by saying, for example
Items = cur.execute(“SELECT Thing, Count FROM bad_table WHERE Name = Dave”)
Which gives me an “Items” that looks like [(‘Houses’, 1), (‘Boats’, 0), (‘Cars’, 0), (‘Kids’, 3)].
Then I would say,
For item in Items:
Thing = item[0]
Count = item[1]
cur.execute(“INSERT INTO good_table ? = ? WHERE Name = Dave”, (Thing, Count))
Which should, I would think, in the first instance translate to
“INSERT INTO good_table Houses = 1 WHERE Name = Dave”
This doesn’t produce any errors when I run it, but it also doesn’t actually insert anything into my good_table.
I’ve also tried running it with
(“UPDATE good_table SET ? = ? WHERE Name = Dave”, (Name, Count))
in case the problem was that sqlite3 was treating the ‘None’ values in the (to my mind) empty columns as already filled in. But, that didn’t work either.
If you can’t tell, I’m pretty new to all of this (python, SQL, etc.) and rely a lot on trial-and-error (along with, of course, a very thorough reading of the documentation).
The potential sources of my problem that I can think of are:
1) Not every “Thing” from the bad_table corresponds to a column in the good_table (e.g. “Kids”). I would hope that sqlite would simply skip over my attempts to “INSERT INTO” the good_table data that won’t fit there, but I don’t know that.
2) More likely, though, is that I’m formatting something wrong, or leaving out a comma or a set of curly braces or something where I need them. Which is why I’ve come to you good people for advice.
It doesn’t seem like what I’m trying to do should be very complicated, but I’m completely stumped and I’d really appreciate it if anyone can point out where I’ve gone wrong or suggest a better approach.
I feel like there is a simple solution to this but I am kinda new.
stat_input= input("Hello Mr. Jenner, what are you interested in tracking today?")
I use an input like this which later is used to call upon data and uses that data to calculate statistics and produce histogram charts / normal distributions.
It works quite nicely. Here are some examples where it is used.
cur.execute('SELECT {} FROM statdata'.format(stat_input))
np.array(stat_input).astype(np.float)
sigma = math.sqrt(np.var(stat_input))
So if I type threemonthdata it will pull the array of data from my database and use it . Its great. However, I have one small problem
I understand that threemonthdata refers to an array. Since I am creating charts, I want to use the input as the title so the chart title identifies what data I am drawing and using (as a reference in the future)
ax.set_title('stat_input')
This doesn't work
ax.set_title(' + stat_input + ')
Nor does this. I want the title to say Threemonthdata. But if I input twomonthdata I want it to say twomonthdata and not give me the array of numbers.
Any ideas?
I have never played with psycopg's cursor class. But, from what I can read, it appears that this one does the job for you of turning your string in place into a list whose name is the same as the referring string.
Thus what about defining another viariable to store the string before it is overriden ? As follows
stat_input_title = stat_input.capitalize()
cur.execute('SELECT {} FROM statdata'.format(stat_input))
Henceforth, stat_input_title and stat_input can be used together withouh conflicting.
ax.set_title(stat_input_title)
It looks like the issue you are facing is that you are passing the set_title() a string 'stat_input', and not the variable stat_input. You likely simply need to use:
ax.set_title(stat_input)
I'm doing some queries ( inserts ) to a database based on some input.
However not all the times I get all the data from the user. I still would like though to insert the data that I received. I have a table with close to
10 columns but in the data I also have some arrays.
When I'm trying to insert something I get an exception that the say input['name'] does not exists and the query is not executed.
Is there some way to quickly manage that? If a variable does isn't defined simply throw a warning like in PHP and not break the whole loop.
New to python and only thing I can think of is to check for every single variable but I'd really hope there's something more simpler than this and quicker.
Do input.get('name')
From the docs https://docs.python.org/2/library/stdtypes.html#dict.get
Return the value for key if key is in the dictionary, else default.
If default is not given, it defaults to None, so that this method never raises a KeyError.
You should look into exception handling. It sounds like you need to use an try-except-else where you're making use of input['name']