I want to update a row setting the result of an expression on a column, ex:
MyTable.query.filter_by(id=the_id).update({
"my_col": "(now() at time zone 'utc')"
})
This code give me the following error :
(DataError) invalid input syntax for type timestamp: "(now() at time zone 'utc')"
LINE 1: ...12bf98a-1ee9-4958-975d-ee99f87f1d4a', my_col='(now() at...
^
'UPDATE my_table SET my_col=%(redeemed_at)s WHERE id = %(id_1)s' {'my_col': "(now() at time zone 'utc')",
this should translate in sql to :
update my_table set my_col = (now() at time zone 'utc') where id = ?;
this SQL statement works when run from the console
You can use this syntax with sqlalchemy func object, as the following code:
from sqlalchemy.sql.expression import func
# get a sqlalchemy session
session.query(YourTable).filter_by(id=the_id).update(values={'my_col': func.utc_timestamp()}, synchronize_session=False)
session.commit()
The error is because the timestamp cannot be a string that is "(now() at time zone 'utc')"
You could try using the Text() function or func.now() or to get the time in UTC
db.func.timezone('utc', db.func.now())
EDIT:
You can try using synchronize_session as 'evaluate' or,
Since you are only updating one row, you can just do this:
mytable_row_obj = MyTable.query.filter_by(id=the_id).one()
mytable_row_obj.my_col = ... What ever you want to put here ...
session.commit()
Related
I am switching from SQLite to MySQL and getting some problems with my Queries. At the moment i am trying to SELECT from a Table where the PackageName is like a variable from a Text Input in my GUI.
The Query looks like this:
test = self.ms.ExecQuery("SELECT PackageID,PackageName,ServiceFee,Cost,LatestChanges FROM Package WHERE PackageName=?", (self.search_entry.GetValue(),))
and my ExecQuery looks like this:
def ExecQuery(self, sql):
cur = self._Getconnect()
cur.execute(sql)
relist = cur.fetchall()
cur.close()
self.conn.close()
return relist
And the error i am getting:
TypeError: ExecQuery() takes 2 positional arguments but 3 were given
What do i need to change to get this running?
You have sql as the 2nd parameter in your ExecQuery, yet you are passing in a sql query as the 1st parameter:
test = self.ms.ExecQuery("SELECT PackageID,PackageName,ServiceFee,Cost,LatestChanges FROM Package WHERE PackageName=?", (self.search_entry.GetValue(),))
I am having some problems with my UPDATE statement with MySQL.Connector.
My code apparently works sometimes and others not, and I don't understand why.
I made a test function, for one row, which is very similar to my other main function.
import mysql.connector
from datetime import datetime
def connect():
return mysql.connector.connect(host="xxxxx.xxx", user="xxx", passwd="xxxxxx", db="xxx")
def test():
mydb = connect()
mycursor = mydb.cursor()
sql = "SELECT MAX(value) FROM test"
mycursor.execute(sql)
date = datetime.now().strftime("%d-%m-%Y %H:%M:%S")
for x in mycursor.fetchall():
updateSql = "UPDATE test SET date=%s WHERE value=%s"
vals = (date, x[0])
mycursor.execute(updateSql, vals)
mydb.commit()
print(vals)
print(mycursor.rowcount)
test()
This code doesn't seem to be working, as the printed rowCount value is 0.
My vals are correctly displayed : ('12-01-2020 16:47:15', 'testValue')
However, what is shown on the database is : '00-00-0000 00:00:00' .
Any ideas on how to solve this?
Edit: just found the answer.
I was using the wrong formatting.
date = datetime.now().strftime("%d-%m-%Y %H:%M:%S") wasn't properly recognized.
The DATETIME type in mySql databases requires the format to be the following:
date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
with the Year in the first place, Month in the second one, and Day in the third one.
If this standard isn't followed then the date will be converted into 0000:00:00 00:00:00 .
I fount that Scala Slick can not execute multiple queries as plain sql.
For example:
val query = sql"""
SET #referenceTime = '12:00:00';
SELECT * FROM table WHERE dateTime <= #referenceTime;
""".as[ClassResult]
dbConfig.db.run(query)
In this string are 2 queries, and Slick return an error as :
You have an error in your SQL syntax; check the manual .... to use near 'SELECT * FROM
From this, I understand that all queries before "SELECT" (maybe the last query) are ignored. So, I found a solution using flatMap, but is not the perfect.
val timeQuery = sql"""SET #referenceTime = '12:00:00';""".as[String]
val dataQuery = sql"""SELECT * FROM table WHERE dateTime <= #referenceTime;""".as[ClassResult]
val composedAction = for {
timeRes <- timeQuery,
dataRes <- dataQuery
} yield dataRes
dbConfig.db.run(composedAction)
This run and in 99% of cases return the result (a list of items). But, sometimes the list is empty, even if I'm sure that must return some data). So, I think that composedAction doesn't wait and execute both queries as a group every time. How I can do this, because I need in second query the result of the first (is used as a parameter in second)
edit:
another solution is to wait for result of the first query and use it as parameter in second. But it is a good solution/practice? Using sync code.
val timeQuery = sql"""SELECT '12:00:00';""".as[String]
var defaultTime: String = ""
val tempResult = dbConfig.db.run(timeParameterQuery.head).map(res => defaultTime = res)
Await.result(tempResult, Duration.Inf)
val dataQuery = sql"""SELECT * FROM table WHERE dateTime <= $defaultTime;""".as[ClassResult]
dbConfig.db.run(dataQuery)
I guess using SQL variables in slick is problematic, could be multiple db contexts. You can chain the queries with the 'map' rather than awaiting the result. Try something like this (untested)
val timeQuery = sql"""SELECT '12:00:00';""".as[String]
dbConfig.db.run(timeQuery.head).flatMap{res =>
dbConfig.db.run(sql"""SELECT * FROM table WHERE dateTime <= $res;""".as[ClassResult])
}
I need to
1. run a select query on MYSQL DB and fetch the records.
2. Records are processed by python script.
I am unsure about the way I should proceed. Is xcom the way to go here? Also, MYSQLOperator only executes the query, doesn't fetch the records. Is there any inbuilt transfer operator I can use? How can I use a MYSQL hook here?
you may want to use a PythonOperator that uses the hook to get the data,
apply transformation and ship the (now scored) rows back some other place.
Can someone explain how to proceed regarding the same.
Refer - http://markmail.org/message/x6nfeo6zhjfeakfe
def do_work():
mysqlserver = MySqlHook(connection_id)
sql = "SELECT * from table where col > 100 "
row_count = mysqlserver.get_records(sql, schema='testdb')
print row_count[0][0]
callMYSQLHook = PythonOperator(
task_id='fetch_from_testdb',
python_callable=mysqlHook,
dag=dag
)
Is this the correct way to proceed?
Also how do we use xcoms to store the records for the following MySqlOperator?'
t = MySqlOperator(
conn_id='mysql_default',
task_id='basic_mysql',
sql="SELECT count(*) from table1 where id > 10",
dag=dag)
I was really struggling with this for the past 90 minutes, here is a more declarative way to follow for newcomers:
from airflow.hooks.mysql_hook import MySqlHook
def fetch_records():
request = "SELECT * FROM your_table"
mysql_hook = MySqlHook(mysql_conn_id = 'the_connection_name_sourced_from_the_ui', schema = 'specific_db')
connection = mysql_hook.get_conn()
cursor = connection.cursor()
cursor.execute(request)
sources = cursor.fetchall()
print(sources)
...your DAG() as dag: code
task = PythonOperator(
task_id = 'fetch_records',
python_callable = fetch_records
)
This returns to the logs the contents of your DB query.
I hope this is of use to someone else.
Sure, just create a hook or operator and call the get_records() method: https://airflow.apache.org/docs/apache-airflow/stable/_modules/airflow/hooks/dbapi.html
i have a complex problem with Date field. Describe what i want to do:
I have field date1 as Date in my db.
#Temporal(TemporalType.DATE)
private Date date1;
I want to take data from this field and compare with current date.
#Query("SELECT date1 FROM Table io WHERE io.date1 >= DATE_FORMAT(CURRENT_DATE, '%Y-%m-%e')")
Date findAll2();
public boolean CheckDate1(){
currentDate = new Date();
date1 = getInterimOrdersRepo().findAll2();
if(currentDate.before(date1) || currentDate.equals(date1)){
System.out.println("TRUE");
System.out.println("currentDate = "+currentDate);
return true;
}
else{
System.out.println("FALSE");
return false;
}
}
but i have an error:
result returns more than one elements; nested exception is javax.persistence.NonUniqueResultException
When method return false i want do Update field data1 with " " empty data.
I using jsf, what i must to do?
It seems that you are trying to read several values from the table into a single variable, and that is the error.
findall2 returns an array (most likely) and u should read one of it's values - try reading first one.
Furthermore, I believe that you can skip the "DATE_FORMAT" in your query, and this is a very strange way to write a code. Not clear what u are trying to achieve here