I have a piece of code like this:
isbn = 4567
c.execute("SELECT * FROM book WHERE Book_d = %s;",(isbn,))
search = c.fetchone()
print(search)
But I want to change the attribute to a variable like this:
isbn = 4567
bisbn = 'Book_d'
c.execute("SELECT * FROM book WHERE %s = %s;",(bisbn, isbn,))
search = c.fetchone()
print(search)
But I guess the syntax is wrong here.
I just wanted to ask whether it is possible to do something like this and if so how?
Thanks
Please check this.
isbn = 4567
bisbn = 'Book_d'
sql_query = "SELECT * FROM book WHERE %s = %s;"%(bisbn, isbn,)
Or if you are using python3
sql_query = f"SELECT * FROM book WHERE {bisbn} = {isbn}"
print(sql_query)
Related
I have sqlite base with a table Rating.
ID|Time|Clicks|
1|2020-04-02 20:20| 250
2|2020-04-03 18:20| 50
3|2020-04-04 22:50| 100
My class looks like this:
class Rating(base):
__tablename__ = 'Rating'
id = Column('ID', Integer, primary_key = True)
clicks = Column('Clicks', Integer)
time = Column('Time', Date)
I wanna show Clicks for a specific date.
date_i_need = datetime.date(2020, 4, 2)
q = test = session.query(Rating).filter_by(time = date_i_need).first()
I tried to change a type from Date to Datetime and Timestamp, but it always returns None.
Any guess?
You can specify datetime.date in statement. I think the problem is with syntax. Use ==:
Please try this:
date_i_need = datetime.date(2020, 4, 2)
q_test = session.query(Rating).filter(Rating.time == date_i_need).first()
Previously, this was working:
$patient_story_set_photos = $wpdb->get_results('SELECT * FROM wp_before_after WHERE patientID = '.$post->ID.' AND patient_display = 1');
However, when I try to add another AND condition like this:
$patient_story_set_photos = $wpdb->get_results('SELECT * FROM wp_before_after WHERE patientID = '.$post->ID.' AND patient_display = 1 AND period_taken = '.$set->period_taken);
I get the following error on screen:
WordPress database error: [Unknown column '1hour' in 'where clause']
SELECT * FROM wp_before_after WHERE patientID = 8175 AND patient_display = 1 AND period_taken = 1hour
Can't see why there's a problem, are you not allowed to use multiple AND conditions in SQL?
The problem is not the AND, the problem is your 1hour, 1hour unquoted means a reference to an object (database, table) named 1hour, you need to quote '1hour'.
If you write
SELECT * FROM wp_before_after
WHERE patientID = 8175
AND patient_display = 1
AND period_taken = '1hour'
you will compare the field periodtaken to a string (CHAR,VARCHAR,TEXT) equal to '1hour'.
I assume period_taken is a field typed CHAR,VARCHAR or TEXT
Before anything, DO NOT CONCATENATE SQL STRINGS nowadays it is a MUST (see how to do it properly https://stackoverflow.com/a/60496/3771219)
The problem you are facing is because, I presume, that the period_taken field is some sort of Char/Varchar/String field and when you are filtering by a "Stringy" field you must sorround your literals values with single quotes:
SELECT *
FROM wp_before_after
WHERE patientID = 8175
AND patient_display = 1
AND period_taken = '1hour'
Hope this help
I'm currently using the below query;
SELECT * FROM '' WHERE Name LIKE 'argument'
OR Reg LIKE 'argument'
However, this will only show results that are LIKE Name or Reg not exact.
How do I change this query to search for results that are exact and like?
Remove the %. Try with -
SELECT * FROM cars WHERE Name LIKE '$searchq' OR Reg LIKE '$searchq'
Does
"SELECT * FROM cars WHERE Name = '$searchq' OR Reg = '$searchq'"
out of the option?
or just use an equals operator for checking the Reg...
SELECT * FROM cars WHERE (Name LIKE ('%'. $searchq .'%') OR Reg = '$searchq')
Note: It may also be worth ensuring all letters in the Reg and search string are both upper case before doing the search. e.g.
<%php $searchq = strtoupper($searchq); %>
SELECT * FROM cars WHERE (Name LIKE ('%'. $searchq .'%') OR upper(Reg) = '$searchq'
Change your SQL to this
$query = mysql_query("SELECT * FROM cars WHERE Name LIKE '%".$searchq."%' OR Reg LIKE '%".$searchq."%' OR Name = '".$searchq."' OR Reg ='".$searchq."'") or die(mysql_error());
I hope you are sanitizing your the search string. Not sanitizing it may leads to SQL Injection.
I'm trying to copy data from one table to another.
Here is my statement:
UPDATE tblMerchants T,
retailers R
SET T.linklabel = R.linklabel,
T.logo_image = R.logo_image,
T.screen_image = R.screen_image,
T.category = R.category,
T.meta_description = R.meta_description,
T.meta_title = R.meta_title,
T.meta_keywords = R.meta_keywords,
T.intro = R.intro,
T.permalink = R.permalink,
T.excerpt = R.excerpt,
T.main_link = R.main_link,
T.related_blog_post = R.related_blog_post,
T.active = R.active,
T.homepage_featured = R.homepage_featured
WHERE T.homepageurl LIKE '%R.linklabel%'
For example, T.homepageurl would look like http://www.amazon.com/ and R.linklabel would look like amazon.com. So I can't figure out why its not working. I'm not getting any errors, its just saying 0 rows affected.
You should be able to use CONCAT to do this:
WHERE T.homepageurl LIKE CONCAT('%', R.linklabel, '%');
The concat function is used to concatenate multiple strings together. The reason why it's not working is because it's trying to match "http://www.amazon.com" with "%R.linklabel%" instead of "amazon.com".
The hibernate manual says this:
String sql = "SELECT ID as {c.id}, NAME as {c.name}, " +
"BIRTHDATE as {c.birthDate}, MOTHER_ID as {c.mother}, {mother.*} " +
"FROM CAT_LOG c, CAT_LOG m WHERE {c.mother} = c.ID";
List loggedCats = sess.createSQLQuery(sql)
.addEntity("cat", Cat.class)
.addEntity("mother", Cat.class).list()
Now, what I have is basically the same. I am return two of the same type per row. I am doing a select something like this:
SELECT {ctrl1.*}, {ctrl2.*} FROM tableA AS A
LEFT JOIN tableB AS ctrl1 ON (A.controlID = ctrl1.controlID AND ctrl1.controlOptionType = ? AND ctrl1.controlOptionValue = ?)
LEFT JOIN tableB AS ctrl2 ON (A.controlID = ctrl2.controlID AND ctrl2.controlOptionType = ? AND ctrl2.controlOptionValue = ?)
And then I addEntity("ctrl1", typeof(mycontrolclass) and
addEntity("ctrl1", typeof(mycontrolclass)
Which seems exactly the same to me as their example. But I get this exception:
"Could not execute query" and the inner exception is "Could not find specified column in results".
If I copy the sql in the exception(to which it has added "AS ctrl1_1_3_3_" etc) it works fine.
Thanks.
What exactly are you trying to do? I believe you might not need using either of them.
// Using HQL:
var motherId = 25;
var hql = "select c.birthDate, c.mother from Cat c where c.mother.Id = :motherId";
var result = Session.CreateQuery(hql)
.SetParameter("motherId", motherId)
.ToList();
// Using NHibernate.LINQ:
var result = (from cat in Session.Linq<Cat>()
where cat.Mother.Id == motherId
select new { cat.birthDate, cat.mother }).ToList();
HQL query examples.
LINQ for NHibernate examples.
I dealt with your problem just for studying purposes, because you will surely
have found a solution in the meanwhile, but the problem should not lie in
the query (which is ok), but in some mapping inconsistency or somewhere else
(perhaps Database).