I am trying to retrieve values from a PostgreSQL database in a variable using a WHERE clause, but I am getting an error.
The query is:
select age into x from employee where name=name.GetValue()
name is the textcontrol in which I am entering a value from wxpython GUI.
I am getting an error as name schema doesn't exist.
What is the correct method for retrieving values?
"name.GetValue()" is a literal string, you are sending that to your db which knows nothing about wxpython and nothing about the variables in your program. You need to send the value of that data to your db, probably using bound parameters. Something like:
cur.execute("select age from employee where name=%s", [name.GetValue()])
x = cur.fetchone()[0] # returns a row containing [age] from the db
is probably what you're after. This will create a query with a placeholder in the database, then bind the value of name.GetValue() to that placeholder and execute the query. The next line fetches the first row of the result of the query and assigns x to the first item in that row.
I'm not positive what you are trying to do, but I think your issue might be syntax (misuse of INTO instead of AS):
SELECT age AS x FROM employee WHERE name = ....
Related
I am trying to create a custom template fragment that builds a table of value properties. I started by creating a SQL query fragment that pulls all properties classified by a Value Type. Now I would like to pull in the default (initial) value assigned. I figured out that it's in the Description table of t_xref, with the property guid in the client field, but I don't know how to write a query that will reliably parse the default value out since the string length may be different depending on other values set. I tried using the template content selector first but I couldn't figure out how to filter to only value properties. I'm still using the default .qeax file but will be migrating to a windows based DBMS soon. Appreciate any help!
Tried using the content selector. Successfully built a query to get value properties but got stuck trying to join and query t_xref for default value.
Edited to add current query and image
Value Properties are block properties that are typed to Value Types. I'm using SysML.
This is my current query, I am no SQL expert! I don't pull anything from t_xref yet but am pulling out only the value properties with this query:
SELECT property.ea_guid AS CLASSGUID, property.Object_Type AS CLASSTYPE, property.Name, property.Note as [Notes], classifier.Name AS TYPE
FROM t_object property
LEFT JOIN t_object classifier ON property.PDATA1 = classifier.ea_guid
LEFT JOIN t_object block on property.ParentID = block.Object_ID
WHERE block.Object_ID = #OBJECTID# AND property.Object_Type = 'Part' AND classifier.Object_Type = 'DataType'
ORDER BY property.Name
I guess that Geert will come up with a more elaborate answer, but (assuming you are after the Run State) here are some details. The value for these Run States is stored in t_object.runstate as one of the crude Sparxian formats. You find something like
#VAR;Variable=v1;Value=4711;Op==;#ENDVAR;
where v1 is the name and 4711 the default in this example. How you can marry that with your template? Not the faintest idea :-/
I can't give a full answer to the original question as I can't reproduce your data, but I can provide an answer for the generic problem of "how to extract data through SQL from the name-value pair in t_xref".
Note, this is heavily dependent on the database used. The example below extracts fully qualified stereotype names from t_xref in SQL Server for custom profiles.
select
substring(
t_xref.Description, charindex('FQName=',t_xref.Description)+7,
charindex(';ENDSTEREO',t_xref.Description,charindex('FQName=',t_xref.Description))
-charindex('FQName=',t_xref.Description)-7
),
Description from t_xref where t_xref.Description like '%FQName%'
This works using:
substring(string, start, length)
The string is the xref description column, and the start and length are set using:
charindex(substring, string, [start position])
This finds the start and end tags within the xref description field, for the data you're trying to parse.
For your data, I imagine something like the below is the equivalent (I haven't tested this). It's then a case of combining it with the query you've already got.
select
substring(
t_xref.Description, #the string to search in
charindex('#VALU=',t_xref.Description,charindex('#NAME=default',t_xref.Description)+6, #the start position, find the position of the first #VALU= tag after name=default
charindex('#ENDVALU;',t_xref.Description,charindex('#VALU=',t_xref.Description))
-charindex('#VALU=',t_xref.Description,charindex('#NAME=default',t_xref.Description))-6 #the length, find the position of the first #ENDVALU tag after the start, and subtract it from the start position
),
Description from t_xref where t_xref.Description like '%#NAME=default%' #filter anything which doesn't contain this tag to avoid "out of range" index errors
in the first function, I'm making the job column lowercase and then searching through but it's not finding any data. Why? Thanks. Just FYI since you don't have the database, all records in the JOB column are uppercase (that's why isn't returning anything), but that's also why I'm making it lowercase first.
In the second function, I'm trying to concat only ename with specific criteria --anything that has an r in the ENAME column (there are multiple records with the r in it), but isn't working (no data found), why? How do I get it done? Thanks.
SELECT LOWER(JOB) FROM EMP
WHERE JOB = LOWER('MANAGER');
SELECT CONCAT('My name is ',ename)
FROM EMP
WHERE ENAME LIKE '%r%';
I tested both of your SQL statements and they work fine for me. Are you sure the records are in db? Are you sure the names of the rows are correct?
EDIT : OK, so the name of column is in lower case but in your WHERE its in uppercase. Thats all :)
I have a function that takes one database field and modifies it so it can become another field.
I'm trying to run this:
def get_text_words(text):
if not text:
return []
else:
# Return a list of words that make up text
return text.split(' ')
q = Text.update(Text.words = get_text_words(Text.text))
q.execute()
When I run it, it returns an empty list. After some investigation with print statements, the function get_text_words is receiving a peewee <TextField> object rather than the text value of the field. It's not passing the if text: statement and returning an empty list as a result.
I know that I could iterate, calculate, and save, but I'd like to see if there's a possibility of running it in one query like the above so it's fast. I've already blanked out the database (no worries, it's a duplicate of production), so the query ran, just not sure how to operate on the value of the field.
Is there a way to run that type of update statement where it takes one field and puts it through a function that operates on the value and returns the result to assign to another field?
You don't seem to realize that the update() method results in the generation of a SQL UPDATE query. The only way your code would work is if, somehow, Peewee decompiled the Python and translated it into SQL.
You want to apply a SQL function, so look in your database for the function...Seems like you want Postgres string_to_array since you're trying to get a list?
So:
q = Text.update({Text.words: fn.string_to_array(Text.text, ' ')})
I am not new to ETL and trying to become familiar with Talend. I cannot seem to get any output of a stored procedure (using tMSSqlSP) or a query (using tMSSqlRow). NOTE: Things I read indicated that tMSSqlRow doesn't produce columnar output but not sure that is correct.
The job shown below runs but no output comes from the tMSSqlSP component. The trace debug shows that the output title is null. However, manual execution of the SP in SSMS succeeds, showing both objid and title.
The SP performs a simple query accepting a single input parameter (int) and outputs two columns -- the objid (int) and the title (string):
create procedure st_sp_case_title_get
#objid int
as
select [objid], [title] from [dbo].[table_case] where [objid] = #objid
You need to use tParseRecordSet in order to retrieve and parse result sets from tMSSqlRow and tMSSqlSP :
Define a column to be your result set (mine is called result) of type Object, in addition to your input columns (my input param is personid). In tMSSqlSP parameters tab, set personid as type IN, and result to be of type RECORD SET.
tParseRecordSet schema :
It parses the result column and gets Firstname and Lastname columns (your objid, and title columns)
tMSSqlRow is very similar. Check my previous answer here for an example.
I have a WinForms C# app which uses a SqlCommand to extract info from a back end SqlServer DB. When a user types in a name, I want to inspect my DB table to look for similar names (say having the same two initial characters). I've been trying the following sql command text:
SELECT Name
FROM tblCases
WHERE Name LIKE SUBSTRING(#Name,1,2);
Where #Name is a parameter in the SqlCommand generated by the application, the value of which is set by the user.
But this returns nil query results every time even when I know it should be returning some results.
What I am trying to achieve is that is the user types say name "Smith", I want the DB to return values like 'Smythe' or 'Smithson', but I cant see to get the SQL command text right.
Help most appreciated.
You can do it this way with the LIKE clause. It adds the wildcard % at the end of you name you are passing:
SELECT Name
FROM tblCases
WHERE Name LIKE (SUBSTRING(#name,1,2) + '%')