So i wanted to save some tags data to the database from the UI form,
Tags: ["male","female","kids"]
I tried everything like but it saves as a string, ialso tried checking if i can alter the data type to array in mysql, or json but i got this
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'JSON(255) NOT NULL' at line 1
I also tried json_encode and json_decode() but still no head so please what can i do?
There isn't any data type as JSON or Array in DB.
What you can do is use the tags field as TEXT as told by #Rick James and then encode your input as json by using json_encode() method before inserting and decode it after retrieving the data from DB by using json_decode() method.
JSON is basically a minimal, readable format for structuring data. Which means it can be considered as a String.
Here is a good post about JSON, in case you need.
What is JSON?
There is no datatype called JSON. Instead use TEXT without the (255).
Related
Athena (Trino SQL) parsing JSON document (table column called document 1 in Athena) using fields (dot notation)
If the underlying json (table column called document 1 in Athena) is in the form of {a={b ...
I can parse it in Athena (Trino SQL) using
document1.a.b
However, if the JSON contains {a={"text": value1 ...
the quote marks will not parse correctly.
Is there a way to do JSON parsing of a 'field' with quotes?
If not, is there an elegant way of parsing the "text" and obtain the string in value 1? [Please see my comment below].
I cannot change the quotes in the json and its Athena "table" so I would need something that works in Trino SQL syntax.
The error message is in the form of: SQL Error [100071] [HY000]: [Simba][AthenaJDBC](100071) An error has been thrown from the AWS Athena client. SYNTAX_ERROR: Expression [redacted] is not of type ROW
NOTE: This is not a duplicate of Oracle Dot Notation Question
Dot notation works only for columns types as struct<…>. You can do that for JSON data, but judging from the error and your description this seems not to be the case. I assume your column is of type string.
If you have JSON data in a string column you can use JSON functions to parse and extract parts of them with JSONPath.
How to read and write JSON data in SQL Server ?
Is it possible to add any type of index to JSON fields from SQL server ?
SQL server supports JSON from version 2016
https://learn.microsoft.com/en-us/sql/t-sql/functions/json-functions-transact-sql?view=sql-server-ver15
JSON fields are indexed as normal text fields i belive
I am receiving data into an REST API, and I want to insert it as XML code into a database. When I later read the record from the database, I expect well-formed XML code.
data=str(request.data)
cur=mydb.cursor()
currentDT = datetime.datetime.now()
val=data.replace("'","''")
cur.execute("insert into MyXMLApi(dateofInsertion,xmlData) values('%s','%s')"%(str(currentDT),val))
mydb.commit()
This is what I expect to see in the database:
"<note>
Don't forget me this weekend!
</note>"
However, this is what I actually get:
'b"<note>
Don''t forget me this weekend!
</note>"'
So I have three issues here.
I have to deal with single quotes in the XML code.
It should be stored as proper XML code.
When I read from the database, I can't get the right code.
request.data is a bytestring in Flask. (See property data and get_data() in the docs.) But you want to save it as non-byte, just plain string to your database. Converting with str() is not the way to do it.
Assuming you want a UTF-8 string, replace your first line with
data=request.data.decode('UTF-8')
Then you will be able to save it to your database.
About the single quotes, I don't think you should escape them yourself. Use parameter binding, and the library will do it for you.
(By the way, this sounds like a very strange use-case. Why not store data in your table as field note?)
I have an old table which used to save json to a string sql field using the Rails ActiveRecord serializer. I created a new table with the field datatype as JSON which was introduced in MySQL 5.7. But directly copying the data from the old field to the new one gives me JSON error saying the json structure is wrong.
More specifically the problem is with unicode characters which my database does not support as of yet and the database is too large to just migrate everything to support it.
I am looking for a way to migrate the data from the old field to the new JSON field
I have seen that replacing \ufor the unicode character to \\u in the JSON string solved the issue but I am not just able to do this:
update table_name set column_name=REPLACE(column_name, '\u', '\\u');
since it gives an error again
ERROR 3140 (22032): Invalid JSON text: "Invalid escape character in string." at position 564 in value for column 'table_name.column_name'.
for sql updating table values \u to \\u :
update table_name set column_name=REPLACE(column_name, '\\u', '\\\\u') [where condition];
I have Delphi application with mydac stored procedure component which takes an utf8 encoded xml file's content as string parameter.
It works with navicat and other db managment programs when I put this xml content copy-paste from file to procedure like
CALL sp_saveit('<xml>garry's<otherdata> data data....</xml>);
But when I try to call it from delphi it throws error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CALL sp_saveit('<?xml version=\"1.0\" encoding="UTF-8"?><Main><Detail z') at line 1
Am following those steps:
delphi finds and reads file contents into a TStringStream with LoadfromFile MyStream.LoadFromFile(Sp_xmlfile);
then am passing contents to stored procedure T_ContentsSP.ParamByName('XmlFile').AsWideString := MyStream.DataString;
Also I've tried to compress this data with delphi's zlib and send as blob, I've tried to change ' with ' but result was same.
How can I send a long string with single and double quotes inside as parameter for sp with delphi?
p.s : sorry for my english.
From your post, I see that you tried to use a constant value (I put this xml content copy-paste) and a parameter (T_ContentsSP.ParamByName('XmlFile')). Also, try to show error text for the command you are used, and not for a different command.
When you are using parameters you does not need to do any special preparation. Data access components have transparently send parameter values to a DBMS. If they have tracing output, then check the trace, what library is sending to a DBMS. Parameter usage is always prefered agains literal usage !
When you are using a constant, then you should (more):
double each single quote inside of the constant and prepend it by '\'. So, a ' will become \''.
double quotes rather not need a special handling.
probably disable macros handling, because '&' may be a macro specifier in the data access library.