How do I query a varchar and #1054 Unknown column 'variable' in 'where clause' - varchar

In MySQL, how do I query a varchar? I can query numbers not characters.
public function getRfis()
{
$projnum = $this->_item->proj_num;
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__projxrfi_request');
$query->where('project_num =' .$projnum) ;
$db->setQuery($query);
$this->rfis = $db->loadObjectlist();
return ($this->rfis);
}
The $projnum is what Im looking for.
Also, if it doesn't find a match...no records exist...I get an error message,
1054 Unknown column 'variable' in 'where clause'
How do I prevent this and return null or 0.
Thank you

Related

How to create update query with QSqlQuery

I'm trying to create an update query in Python3/PyQt5.10/Sqlite . A select/insert query made the same way runs fine. Fields & corresponding record exist.
def updateRecords():
theDict = {
"Loc": "PyQt121",
"BoekNr" : "dfdf",
"BoekTitel" : "eeee",
"BoekBedrag" : 999
}
theFilter = " WHERE Loc = 'PyQt'"
query = QSqlQuery()
columns = ', '.join(pDict.keys())
placeholders = ':'+', :'.join(pDict.keys())
sql = 'UPDATE %s SET (%s) VALUES (%s) %s' % (pTable, columns, placeholders, pFilter)
query.prepare(sql)
for key, value in pDict.items():
query.bindValue(":"+key, value)
print (sql)
query.exec_()
print(query.lastError().databaseText())
return query.numRowsAffected()
The sql generated is UPDATE tempbooks SET (Loc, BoekNr, BoekTitel, BoekBedrag) VALUES (:Loc, :BoekNr, :BoekTitel, :BoekBedrag) WHERE Loc = 'PyQt'.
query.lastError().databaseText()) give me "No Query" and updated rows is -1.
The correct syntax for an update query:
UPDATE tablename
set col1 = val1,
col2 = val2,
col3 = val3
WHERE condition
Probably query.prepare(sql) is returning False because of invalid syntax.

Reading object returned from "SELECT COUNT(*) FROM table"

let rows = db.prepare("SELECT COUNT(*) FROM table").get();
Returns the object
{ 'COUNT(*)': 2 }
I am not sure how to read that as
console.log(rows.COUNT(*));
Returns
SyntaxError: Unexpected token *
{ 'COUNT(*)': 2 }
Since COUNT(*) is a key. You can access it directly by using Bracket notation
console.log('No of rows ', row['COUNT(*)']); //logs 2
I found the solution is
Object.values(rows);
It will return a length 1 array with just the number
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Object/values

Not able to parameterize LIMIT and OFFSET in sqlite3

Why following code is giving syntax error "sqlite3.OperationalError: near "?": syntax error"
import sqlite3
connection = sqlite3.connect('data.db')
cursor = connection.cursor()
table = "device_store"
uuid = "bbebe39e-fe2e-4817-b022-a3ef13bd6283"
page = 1
POSTS_PER_PAGE = 10
query = "SELECT * FROM ? WHERE uuid=? LIMIT ? OFFSET ?"
result = cursor.execute(query, (table, uuid, POSTS_PER_PAGE, 0))
rows = result.fetchall()
connection.close()
print("==>> Printing rows <<==")
print(rows)
The error is caused by the placeholder in FROM ?, not the others. Table names can't be passed as parameters, they have to be hardcoded in the statement.

Query Parameter Format for SELECT ... IN with Cassandra using Node.js Driver

I have a Cassandra SELECT query with an IN parameter that I want to run via the Node driver, but can't figure out the syntax.
On the cqlsh console, I can run this select and get a correct result:
SELECT * FROM sourcedata WHERE company_id = 4 AND item_id in (ac943b6f-0143-0e1f-5282-2d39209f3a7a,bff421a0-c465-0434-8806-f128612b6850,877ddb6d-a164-1152-da77-1ec4c4468258);
However, trying to run this query using an array of IDs using the Cassandra Node driver, I get various errors depending on the format. Here's what I've tried:
client.execute("SELECT * FROM sourcedata WHERE company_id = ? AND item_id in (?)", [id, item_ids], function(err, rs) { ...
The error is:
ResponseError: Invalid list literal for item_id of type uuid
With this:
client.execute("SELECT * FROM sourcedata WHERE company_id = ? AND item_id in (?)", [id, item_ids], function(err, rs) { ...
The error is:
ResponseError: line 1:72 no viable alternative at input '[' (...WHERE company_id = 4 AND [item_id] in...)
item_ids is an array of string objects, and they were acquired via a select on another Cassandra table.
This is a working app, and other queries that don't use "SELECT .. IN" work fine.
I can also do make it work the "ugly" way, but would prefer not to:
client.execute("SELECT * FROM sourcedata WHERE company_id = ? AND item_id in (" + item_ids.toString() + ")", [id,], function(err, rs) { ...
You should use IN ? without parenthesis, to provide a list:
const query = 'SELECT * FROM sourcedata WHERE company_id = ? AND item_id in ?';
client.execute(query, [ id, item_ids ], { prepare: true }, callback);

Database error Duplicate entry

So i have this error when i have duplicate values while inserting data, now my query is correct, i've tried to run it in phpmyadmin, and works just fine, but kohana gives me errors, this is how my query looks like:
DB::query(Database::INSERT, 'INSERT INTO `views` ( user_id, viewer_id, username, picture, view_time, view_count)
VALUES ("'.$this->request->param('id2').'", "'.$user->id.'", "'.$user->username.'", "'.$user->picture.'", '.DB::expr('NOW()').', "1")
ON DUPLICATE KEY UPDATE `view_time` = NOW(), `view_count` = view_count +1
And in pure sql:
INSERT INTO `views` ( user_id, viewer_id, username, picture, view_time, view_count )
VALUES (
"134173", "139173", "username", "pic.jpg", NOW( ) , "1"
) ON DUPLICATE
KEY UPDATE `view_time` = NOW( ) ,
`view_count` = view_count +1
So basicly i try to insert and on duplicate values i update, but for some reason kohana gives me error:
kohana Database_Exception [ 1062 ]: Duplicate entry
How can i remove this error?
Perhaps try this approach by not specifying the query type:
$query = "
INSERT INTO `views` (user_id, viewer_id, username, picture, view_time, view_count)
VALUES (%d, %d, '%s', '%s', %s, %d)
ON DUPLICATE KEY UPDATE `view_time` = %s, `view_count` = view_count + 1";
$query = sprintf($query, $this->request->param('id2'), $user->id, $user->username, $user->picture, DB::expr('NOW()'), 1, DB::expr('NOW()'));
$result = Database::instance()->query(NULL, $query);

Resources