Best way to save JSON orderbook data in a MySQL database - mysql

Here is the sample of the data:
{'rate': 0.008999, 'stamp': {'bids': [[117090.0, 11.78362353], [117075.0, 0.28], [116918.0, 0.5],[116820.0, 32.0]], 'asks': [[117104.0, 0.55206666], [117105.0, 1.11], [117142.0, 4.99974999], [117178.0, 1.0], [117191.0, 0.0669], [117348.0, 0.01], [117369.0, 1.05]]}}
I want to host the logger on PythonAnywhere. I'm new to mysql. As of version 5.7.8 JSON objects were added how ever pythonanywhere uses 5.6.27.
And I get this error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syn
tax to use near 'JSON)' at line 1
So I'm thinking to store it as a string VARCHAR. However you have to specify the length, this varies for each entry. So shall I just put the length to be 65,535?

Usually Json are stored as NVARCHAR in Mysql DB

You could try to change your VARCHAR to TEXT as the fixed max size is 2¹⁶-1 = 65535 characters.
When you insert your string in your query wrap it into double quotes "[JSON]" as you use simple quotes in your json.

You should use VARCHAR to TEXT type in mysql before 5.7
In mysql 5.7 you have a native datatype to store json
https://dev.mysql.com/doc/refman/5.7/en/json.html

Related

Need help inserting json literal into mysql 5.7

When I try to insert the following query I keep getting this error message:
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 '['php','javascript'])' at line 2
I am using the following query
INSERT INTO freelancer (tags) VALUES (['php','javascript']);
I tried another query, this time making it a string, and then I get this error
INSERT INTO freelancer (tags) VALUES ("['php','javascript']");
Invalid JSON text: "Invalid value." at position 1 in value for column 'freelancer.tags'.
I am not sure how to go about inserting this into mysql... if anyone can help me figure this out, I would really appreciate it!
JSON is very particular about quotes. You must use doubles:
VALUES ('["php","javascript"]')
You can test with a JSON validator if you're curious about what is or isn't JSON.

mysql: Not able to create a table

create table 5390e910_abb3_40e2_bdfa_bd9d369e6dc6 like sample_table
is failing.
ERROR: #1064 - 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 '5390e910_abb3_40e2_bdfa_bd9d369e6dc6 like
notification_sample_history' at line 1
Now I don't see any flaw with the above name as the mysql documentation says:
64 characters max
can start with any alphabet/digit
an contain underscores/alphabets/digits
Though I am able to run something like this:
create table 1f8b784f_f580_4a82_9e93_167a2d9c79f5 like sample_table
I believe it's the first "e". When I swap that character for any other letter, the query works just fine.
I'm not positive on this, but my suspicion is that MySQL is reading your string and considering it to mean the number "5,390 times 10 to the power of 910abb3[etc]". And while all-numeric table names are valid they must be quoted.
Try enclosing your table name in back-ticks. This works for me:
create table `5390e910_abb3_40e2_bdfa_bd9d369e6dc6` like abbreviations;

what is the correct datetime format for mySQL in 24 hour time?

When I tried to insert a datetime into my MAC's local mysql table from $sudo mysql>>$use DB in terminal, I get this error:
ERROR 1064 (42000): 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 '('2015-10-07 13:21:30',5)),
convert(datetime('2015-10-07 13:41:30',5)))' at line 1
This is the insert:
INSERT INTO log VALUES (1,28, convert(datetime('2015-10-07 13:21:30',5)), convert(datetime('2015-10-07 13:41:30',5)));
According to mysql's dev page (https://dev.mysql.com/doc/refman/5.1/en/datetime.html), the default format of datetime in mySQL is 'YYYY-MM-DD HH:MM:SS'. In your case, if you have a DATETIME or DATE column you don't need to call any conversion function to insert to it a datetime, just insert your date as is: '2015-10-07 13:21:30' and you should be ok.
In general, try to avoid conversions when inserting data as that affects the database performance especially if you are using it actively.
Resolved.
when i created the table i set a column to an int instead of datetime so of course it didnt accept datetime. when i changed it back it worked... orz

(ASP) MS Access -> MySQL: Error in Select, where [..] strings

I am using a portal system on my website and modified the ASP code heavily.
Since the website is growing, I want to migrate from MS Acces to MySQL.
Now, I think the portal I'm using (and some code I inputted) aren't MySQL compatable, because when I switch to the MySQL database, I get the following error.
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[MySQL][ODBC 5.1 Driver][mysqld-5.1.55-community]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 '[EzModuleID],
[ModName] From EzCore_Plugin Where IsModActive='1'' at line 1
[website]\WWWROOT\BOXX\INCLUDES../../includes/include.asp, line 3736
The SQL string regarding this line is the following:
Select [EzModuleID], [ModName] From EzCore_Plugin Where [IsModActive] = 1;
Im new to MySQL and I can't find why this is giving an error.
I've tried the quote's around 1, removing [], removing the space..
I think that when I figure out why this is causing an error, I can continue modifying the rest to make the website work on mysql.
Lose the square brackets
(I might as well post this as the answer rather than a comment)
In MySQL column and table names can be escaped with the backtick character ` or if the ANSI SQL mode is enabled with double quotes ".
Your WHERE clause (according to the error message) is Where IsModActive='1'. This works if IsModActive is a text column. If it is numeric, drop the single quotes. If IsModActive is a Boolean, change the clause to Where IsModActive IS true.
See: is operator

SQL query to insert binary data using Ruby

I'm trying to insert some binary data into database using 'mysql' gem in ruby. But as the binary data contains many single and double quotes, the following code fails. Please help me to fix it.
m = mysql.prepare("insert into data (binary) values ('#{binary_data}') ")
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 '.......' at line 1 (Mysql::Error)
binary is reserved word in mysql so wrap it with apostrophe like
insert into data (`binary`) ......
You're using prepared statements wrong. What about this?
stmnt = mysql.prepare("insert into data (`binary`) values (?)")
stmnt.execute binary