Get json length of a column in sql alchemy? - mysql

what is the correct way to query JSON_LENGTH(json_column) in sql alchemy for mysql?
I have tried this query but getting error : (pymysql.err.InternalError) (1305, 'FUNCTION test_db.json_array_length does not exist')
query :
self.session.query(func.json_array_length(DbModelName.data)).all()

please find this post SqlAlchemy: Querying the length json field having an array
for mysql please update method to json_length. Else we will get the above error
this is working fine

Related

JSON_SET operation using Spring data relational Update fails with invalid json text

I am using R2dbc jasync driver to generate and execute update query on my mysql db table containing a json column.
I am trying to do a JSON_SET operation on a column using R2dbcEntityTemplate and org.springframework.data.relational.core.query.Update.update and org.springframework.data.relational.core.query.Update.set methods. I am trying to generate a query equivalent to
UPDATE mytable SET json_col = JSON_SET(json_col, '$.path_to_update', CAST('{"a":"b"}' AS JSON)) WHERE id=:id
But I get the following error:
invalid json text: "the document is empty." at position 0 in value for column
From what I understand is the Update.set() operation is not delegating the JSON_SET operation to the DB but the DB is trying to interpret the generated string as json which is not valid json at position 0.
How can I achieve this using Update.set() function to delegate the MySQL function call to the DB before DB interprets the value as JSON ?
I tried using dbClient.sql().bind() and it worked but using dbTemplate.update() this is not working.

Change mysql query to laravel 5.4

I have a mysql query
SELECT * FROM Appraisal_skills WHERE INSTR(`assigned_to`, 6);
Which working well.I want to change this to laravel
I tried
$getskillset=Db::table('Appraisal_skills')->where(`assigned_to`, 6)->instr()->get();
But got an error like BadMethodCallException in Builder.php line 2508:
Call to undefined method Illuminate\Database\Query\Builder::instr()
Please correct me.Any help would be appreciated.
Laravels database query builder doesn't have instr method. But you can use whereRaw method.
So your code will be like this
$getskillset=Db::table('Appraisal_skills')->whereRaw('INSTR(`assigned_to`, ?)', [6])->get();
You can read more about raw expressions in Laravel docs
and here is api documentation

How to fix dbWriteTable error "unable to find an inherited method for function 'dbWriterTable' for signature...?"

I want to insert data in MySQL from a dataframe in R. I managed to connect without problems from R to MySQL using dbConnect, however when I try to insert my data using dbWriteTable I keep getting the error
unable to find an inherited method for function 'dbWriterTable' for signature '"integer", "character", "data.frame"'.
Now, I've tried the proposed solution mentioned here How to resolve this error--dbWriteTable() but this solution doesn't work for me. My original code was
dbWriteTable(conn, "Date", france$Date)
because my table in MySQL is called Date and my dataframe in R is called france and has a column Datecontaining dates (the type of this column is date too). After the proposed solution, my code becomes
dbWriteTable(conn, "Date", data.frame(dat=france$Date), row.names=FALSE, append=TRUE)
but I'm getting the same error. I tried adding field.types=list("date") as mentioned in the following solution RMySQL dbWriteTable with field.types but I get the same error.
Finally, I've tried to use dbSendQuery together with paste() to insert manually my data as suggested here How to insert integer values with query in MySQL in R? but I get the same error again!
This is driving me crazy. Any help will be appreciated.
I got the same error because the object I was providing to dbWriteTable() wasn't a data.frame.
To fix it, I simply convert the object to a data.frame first
dat <- as.data.frame(dat)
dbWriteTable(con, "mydbtable", dat)
Did you try connecting to your database? I was having the same error and when I checked connection, it was broken. Make sure you check the connection to db and run the dbWriteTable command next. It should work

Getting Error message from linq to entity query.

I keep receiving the error
"LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression"
on the following line in my code
var Reviewer = repository.reviewers.FirstOrDefault(t => t.ReviewerName == formCollection[3]);
formCollection[3] is a string returned from a drop down I have contained within a form. The query seems to work O.K. until it returns the value from the database. What can I do to fix this?
OK, I was trying to do too much at once, when I finally thought about it and put formCollection[3] into a string variable and then used the string variable in the linq query everything worked out ok.

Entity Framework - MySQL - Datetime format issue

I have a simple table with few date fields.
Whenever I run following query:
var docs = ( from d in base.EntityDataContext.document_reviews
select d ).ToList();
I get following exception:
Unable to convert MySQL date/time value to System.DateTime.
MySql.Data.Types.MySqlConversionException: Unable to convert MySQL date/time value to System.DateTime
The document reviews table has two date/time fields. One of them is nullable.
I have tried placing following in connection string:
Allow Zero Datetime=true;
But I am still getting exception.
Anyone with a solution?
#effkay - if you solved this it would be great if you could post the answer.
Also if anyone else has a solution that would be great too :).
Edit:
The solution can be found in the http://dev.mysql.com/doc/refman/5.1/en/connector-net-connection-options.html connector documentation.
I needed to set "Convert Zero Datetime" to true, and now it works.
hth.
You need to set Convert Zero Datetime=True in connection string of running application