SQL query to insert binary data using Ruby - mysql

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

Related

Best way to save JSON orderbook data in a MySQL database

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

Error while inserting picture into sql table

I am trying to insert a 'png ' image into an sql table field(called barchart,which is of type blob) with the below query.
INSERT INTO disease_symptom_soc(barchart) Values ((SELECT BULKColumn FROM OPENROWSET(BULK N'/home/barchartC2936861.png', SINGLE_BLOB) AS Image)) where disease_id='C2936861';
I am getting the below error.What could be the reason?
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 '(BULK N'/home/barchart' at line 1
i guess u can use LOAD_FILE
Example:
INSERT INTO expBLOB(ID,IMAGE) VALUES(1,LOAD_FILE('/some/path/image.png'))
If you can, then change the DataType of the column to 'image'. Depending on the sql server version you are using. Otherwise if it oracle based db , you will have to create a loadfile proc and execute it as follows:
http://arjudba.blogspot.com/2008/06/how-to-insert-blob-dataimage-video-into.html
MySql OpenrowSet isn't used to copy files into the database but to query an external file (csv, xls) with a connection string, for example
OPENROWSET ('MSDASQL','mysql connection string','query')
I suggest you to load the file with a simple program made in any language (I don't know if you are developing in php, c#...) and pass it already loaded in memory to the db with a simple insert query.

Please resolve the syntax error in the mysql insert query

I am using mysql 5.5.27
At places where it looks like double quotes are two single quotes closer together, and they are actually null values in the excel sheetin the db.
I get the following error,
com.mysql.jdbc.exceptions.jdbc4.MySQLS… 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 'CASE,lname,fname,gender,dob,ssn,… at line 1
the actual query is
Insert into
work(CASE,lname,fname,gender,dob
"CASE" is a reserved word : See http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
You may escape it :
Insert into child(`CASE`,LASTNAME,FIRSTNAME,GENDER,DOB… American','1689 Crucible Street','','Pittsburgh','PA','15210','(4…
But I would personally prefer renaming the column.
CASE is a reserved word ... quote it ..
For mysql:
insert into tableName values()
Anyway you have SQL syntax errors:
Insert into child(CASE,LASTNAME,FIRSTNAME,GENDER,DOB… American','1689 Crucible Street','','Pittsburgh','PA','15210','(4…
Case is a reserwed word: http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
In DOB… American' you miss a ' ('DOB… American')

Mysql syntax error in scrapy

Hi i am working on scrapy and writing a pipeline and in that i had a query which should write the data in to mysql database
tx.execute("""INSERT INTO example_table (book_name,price)
VALUES (%s,%s)""",
(item['book_name'],
item['price'],)
)
I am getting the following errors two errors below
(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 '))' at line 2")
(1241, 'Operand should contain 1 column(s)')
I dont know whats wrong in this query but i am unable to save the data in to database.
Can any one have a idea of this.
You forgot to add % while executing
x.execute("""INSERT INTO example_table (book_name,price)
VALUES (%s,%s)""",%
(item['book_name'],
item['price'])
)
You have added an additional comma at last remove it. following is the correct statement. Kindly try.
x.execute("""INSERT INTO example_table (book_name,price)
VALUES (%s,%s,%s,%s,%s,%s)""",
(item['book_name'],
item['price'])
)

EDMX With lambda expression with MySQL

I am using EDMX with MySql 5.1. It is working fine except When I try to execute the lambda expression, it shows me the following 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 '[XYZ].[UserID] AS [UserID], [XYZ].[FirstName] A' at line 17
where [XYZ] is the table name and [UserID], [FirstName] are the columns of that table. Following is the statement, that I want to execute -
_context.XYZSet.Where(org => org.ACDID == sbuID || !(org.ACDID.HasValue)).ToList();
Please help..
I do not know anything about EDMX, but from that error it looks like it's using MS SQL Server syntax to escape table and column names, which is not supported by MySQL. MySQL uses backticks for that, not square brackets.
If you could get EDMX to stop escaping the table and columns names then you might be okay, assuming none of the table/column names are reserved words.