Postgres insert syntax error - postgresql

My SQL query looks like this:
product = 'Huggies Little Movers Diaper Pants for Boys Size 5 (60 Count)'
retailer = 'Target'
query = """SELECT * FROM product_info WHERE product_name = %s AND retailer = %s""" % (product, retailer)
conn = psycopg2.connect("dbname='test1' user='postgres' host='localhost' password='123'")
cur = conn.cursor(cursor_factory = psycopg2.extras.RealDictCursor)
cur.execute(query)
When i execute that i get a error saying:
psycopg2.ProgrammingError: syntax error at or near "Basic"
I am not sure why my syntax is wrong

Your statement;
query = """SELECT * FROM product_info WHERE product_name = %s AND retailer = %s""" % (product, retailer)
...builds a complete string from the query and parameters without any quoting around your strings, which makes the entire string invalid SQL which fails at execute;
SELECT * FROM product_info
WHERE product_name = Huggies Little Movers Diaper Pants for Boys Size 5 (60 Count)
AND retailer = Target
What you're probably trying to do is parameterizing your query which is instead done in execute by passing the parameters in a tuple;
query = """SELECT * FROM product_info WHERE product_name = %s AND retailer = %s"""
...
cur.execute(query, (product, retailer))

Related

Linq order by using query expression

Is it possible to do orderby expression using linq query expression based on dynamic string parameter? because the query i have is producing weird SQL query
my linq:
var product = from prod in _context.Products
join cat in _context.Categories on prod.CategoryId equals cat.CategoryId
join sup in _context.Suppliers on prod.SupplierId equals sup.SupplierId
orderby sortParam
select new ProductViewModel
{
ProductName = prod.ProductName,
ProductId = prod.ProductId,
QuantityPerUnit = prod.QuantityPerUnit,
ReorderLevel = prod.ReorderLevel,
UnitsOnOrder = prod.UnitsOnOrder,
UnitPrice = prod.UnitPrice,
UnitsInStock = prod.UnitsInStock,
Discontinued = prod.Discontinued,
Category = cat.CategoryName,
Supplier = sup.CompanyName,
CategoryId = cat.CategoryId,
SupplierId = sup.SupplierId
};
where var sortParam = "prod.ProductName"
The code above produces weird sql where order by sortParam is being converted to (SELECT 1). Full query catched by sql profiler below:
exec sp_executesql N'SELECT [prod].[ProductName], [prod].[ProductID], [prod].[QuantityPerUnit], [prod].[ReorderLevel], [prod].[UnitsOnOrder], [prod].[UnitPrice], [prod].[UnitsInStock], [prod].[Discontinued], [cat].[CategoryName] AS [Category], [sup].[CompanyName] AS [Supplier], [cat].[CategoryID], [sup].[SupplierID]
FROM [Products] AS [prod]
INNER JOIN [Categories] AS [cat] ON [prod].[CategoryID] = [cat].[CategoryID]
INNER JOIN [Suppliers] AS [sup] ON [prod].[SupplierID] = [sup].[SupplierID]
ORDER BY (SELECT 1)
OFFSET #__p_1 ROWS FETCH NEXT #__p_2 ROWS ONLY',N'#__p_1 int,#__p_2 int',#__p_1=0,#__p_2=10
I'm seeing a lot of people doing linq order by using dynamic parameter but all of them use lambda not query expression, please enlighten me
As was already mentioned, you are passing a string value instead of an expression that reflects the column name. There are options for what you want however, see for example here.

How to search numeric by Sphinx correctly?

I need make search on billion records in MySQL and it's very long process (it's works now). May be Sphinx help me? How correctly to configure Sphinx for search numbers? Should I use integer attribute for searching (not string field)?
I need to get only row where the timestamp 'nearest or equal' to query:
CREATE TABLE test ( date TIMESTAMP(6) UNIQUE, num INT(32) );
| 2018-07-02 05:50:33.084011 | 282 |
| 2018-07-02 05:50:33.084028 | 475 |
...
(40 M such rows... all timestamps is unique, so this column are unique index so I no need in create additional index I suppose.)
sphinx.conf:
source src1
{
type = mysql
...
sql_query = SELECT * FROM test
}
indexer...
Sphinx 3.0.3
...
indexing index 'test'...
collected 40000000 docs, 0.0 MB
In my test I find nearest timestamp to query:
$start = microtime(true);
$query = '2018-07-02 05:50:33.084011';
$connMySQL = new PDO('mysql:host=localhost;dbname=test','','');
$sql = "SELECT * FROM test WHERE date <= '$search' ORDER BY date DESC LIMIT 1";
$que = $connMySQL->query($sql);
$result = $que->fetchAll(PDO::FETCH_ASSOC);
$query = $connMySQL->query('reset query cache');
$connMySQL = null;
print_r ($result);
echo 'Time MySQL:'.(microtime(true) - $start).' sec.';
$start = microtime(true);
$query = '2018-07-02 05:50:33.084029';
$connSphinxQL = new PDO('mysql:host=localhost;port=9306;dbname=test','root','');
$sql = "SELECT * FROM test WHERE date <= '$search' ORDER BY date DESC LIMIT 1";
$que = $connSphinxQL->query($sql);
$result = $que->fetchAll(PDO::FETCH_ASSOC);
$query = $connSphinxQL->query('reset query cache');
$connSphinxQL = null;
print_r ($result);
echo 'Time Sphinx:'.(microtime(true) - $start).' sec.';
Output:
[date] => 2018-07-02 05:50:33.084011 [num] => 282 Time MySQL: 0.00193 sec.
[date] => 2018-07-02 05:50:33.084028 [num] => 475 Time Sphinx: 0.00184 sec.
I suggested to see some different resuts, but noticed that before indexing I have got the same result, so I think Sphinx searches directy in MySQL by the reason of my wrong configuration.
Only ask here I found: no text search
Should I use integer attribute for searching (not string field)?
Yes. But an added complication, is a index NEEDS at least one field (sphinx isnt really designed as a general database, its intended for text queries!)
Can synthesize a fake one.
sql_query = SELECT unix_timestamp(`date`) AS id, 'a' AS field, num FROM test
sql_attr_uint = num
Also shows that need a unique integer as the first column, to be a document_id, seems as your timestamp is unique, can use that. a UNIX_TIMESTAMP is a nice easy way to represent a timestamp as a plain integer.
Can use id in queries too, for filtering, so would need to convert to a timestamp at the same time.
$query = '2018-07-02 05:50:33.084011';
$id = strtotime($query)
$sql = "SELECT * FROM test WHERE id <= '$id' ORDER BY id DESC LIMIT 1";

Openerp create function doesn't execute query

Following is my create function for roster module. The problem is that only update query is not working. The query is working fine when it run alone in pgadmin but in here it doesn't. Both Select and Insert queries are working fine.
(I know using cr.execute is not a good practice but I am in little bit hurry with deadlines).
def create(self, cr, uid, values, context=None):
#rec_id=values['id']
sub_day=values['roster_day']
ros_time=values['time_slot']
emp = values['employee']
dept = values['department_id']
sub_emp = values['sub_employee']
#sub_day = datetime.datetime.strptime(sub_day, '%Y-%m-%d')
cr.execute("""SELECT ra.id , ra.emp_id FROM roster_allocation ra, roster_days_allocation rda
WHERE rda.roster_allocation_connection=ra.id and
rda.allocation_start_day='%s' and
rda.roster_time_list=%d and
ra.emp_id=%d"""%(sub_day,ros_time,emp))
exers=cr.fetchone()[0]
cr.execute("""INSERT INTO roster_allocation (write_uid,emp_id,department_id) VALUES(%d,%d,%d)""" %(context['uid'], sub_emp, dept))
print "Employee for substitution record inserted successfully"
cr.execute("""UPDATE roster_days_allocation SET roster_allocation_connection = (SELECT MAX(ra.id) FROM roster_allocation ra, roster_substitution rs
WHERE ra.emp_id=rs.sub_employee)
WHERE allocation_start_day = '%s' AND roster_time_list = %d AND roster_allocation_connection = %d""" %(sub_day, ros_time,exers))
print "Employee for substitution record updated successfully"
return super(roster_substitution, self).create(cr, uid, values, context=context)
I have edited UPDATE query and even though it's not the best practice, it worked.
cr.execute (SELECT MAX(ra.id) FROM roster_allocation ra, roster_substitution rs
WHERE ra.emp_id=rs.sub_employee)
val=cr.fetchone()
cr.execute("""UPDATE roster_days_allocation SET roster_allocation_connection = %d
WHERE allocation_start_day = '%s' AND roster_time_list = %d AND roster_allocation_connection = %d""" %(val,sub_day, ros_time,exers))

Report Builder - Parameter Conversion Error

Using Report builder and trying to "move" a dataset filter over to the dataset query in order to improve report performance.
"An error occurred while reading data from the query result set. Conversion failed when converting the varchar value '70048-' to data type int."
This is my Where Statement that works (without parameter)
WHERE (JCCD.CostType = 1) AND JCCD.Job = '88800-'
This is my Where Statement that gives error after providing the parameter
WHERE (JCCD.CostType = 1) AND JCCD.Job = #JobParam
Complete SQL Statement
SELECT HQCO.Name, JCCD.JCCo, JCCD.Job, JCCD.Phase, JCJM.Description, JCJP.Description AS PhaseDescription, CAST(JCCD.EstHours AS Numeric) AS EstHours,
CAST(JCCD.EstCost AS Numeric) AS EstCost, CAST(JCCD.ActualHours AS Numeric) AS ActualHours, CAST(JCCD.ActualUnits AS Numeric) AS ActualUnits,
CAST(JCCD.ActualCost AS Numeric) AS ActualCost, JCCD.CostType, JCCD.UM, JCCD.CostTrans, JCCD.Mth, JCCD.JCTransType, JCJM.Job AS Expr2, JCCD.ActualDate,
JCCH.UM AS UM2, CAST(JCCD.EstUnits AS Numeric) AS EstUnits, JCCD.ProjUnits, JCCH.CostType AS Expr4, JCCD.ProjHours, JCCD.ForecastHours,
JCCD.ForecastUnits, JCCD.ForecastCost, CONVERT(INT,LEFT(JCCD.Phase,3)) AS PhasePreFix
FROM JCCD AS JCCD INNER JOIN
JCJM AS JCJM ON JCCD.JCCo = JCJM.JCCo AND JCCD.Job = JCJM.Job INNER JOIN
HQCO AS HQCO ON JCCD.JCCo = HQCO.HQCo INNER JOIN
JCJP AS JCJP ON JCCD.JCCo = JCJP.JCCo AND JCCD.Job = JCJP.Job AND JCCD.PhaseGroup = JCJP.PhaseGroup AND JCCD.Phase = JCJP.Phase INNER JOIN
JCCH AS JCCH ON JCCD.JCCo = JCCH.JCCo AND JCCD.Job = JCCH.Job AND JCCD.PhaseGroup = JCCH.PhaseGroup AND JCCD.Phase = JCCH.Phase AND
JCCD.CostType = JCCH.CostType
WHERE
(JCCD.CostType = 1) AND (JCCD.Job = '88800-')
ORDER BY JCCD.JCCo, JCCD.Job, JCCD.Phase

How to execute union query in zf2 where i m using tablegateway?

How to execute the following query
select * from table1 union select * from table2
in zend-framework2 where i am using tablegateway? In the documentation of zf2,they didn't give any details about union query.
Try -
$select1 = new Select('table1');
[.... rest of the code ....]
$select2 = new Select('table2');
[.... rest of the code ....]
$select1->combine($select2); //This will create the required SQL union statement.
To get count of the two tables you have to use a bit of SQL rather then tableGateway -
$sql = new Sql($this->tableGateway->adapter);
$select_string = $sql->getSqlStringForSqlObject($select1);
$sql_string = 'SELECT * FROM (' . $select_string . ') AS select_union';
$statement = $this->tableGateway->adapter->createStatement($sql_string);
$resultSet = $statement->execute();
$total_records = count($resultSet);
$resultSet gives data.
$total_records gives total no. of records.