bulk insert in zend framework [duplicate] - mysql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I add more than one row with Zend_Db?
everyone,
I need to have a bulk insert in zend framework. For normal sql I have the below query, I want to do the same in zend framework.
INSERT INTO `dbname`.'tablename` (`id`, `user_id`, `screen_name`, `screen_name_server`) VALUES (NULL, '15', 'test', 'test'), (NULL, '15', 'test', 'test');
Thanks.

There's no way to do this, as Marcin states.
If you'd like to do some messing around with the Zend Framework you'd could try to alter the
insert.
You could try to make it so that the insert method can take an array of arrays for the data. Then you could use the arrays of data to build the bulk insert.
For example,
$data1 = array( ); //data for first insert
$data2 = array( ); //data for 2nd insert
//a zend_db_table object
$dbTable->insert( array( $data1, $data, ) );
You would have to edit the insert method a bit to detect multiple data inserts and then build the insert accordingly. Unfortunately I haven't looked into how the code is built or I would just put it up here for you to use.

There is no insert method in zend_db that would insert multiple rows. What you can do though, is to use query method of Zend_Db_Adapter and put there your own insert sql.

Related

Insert multiple VALUES into table

I try to insert multiple VALUES into a table using the fat free frameworks sql mapper.
Docs
The problem is it only shows that for one VALUE
$db->exec('INSERT INTO mytable VALUES(?,?)',array(1=>5,2=>'Jim'))
As I have a lot of records and need to speed it up I wanted to add multiple
VALUES, as in VALUES(?,?),(?,?),?,?);
But how has the array to look then?
Background. I try to speed up the import this way because i parse big 100k+ csv files and import them.
The syntax to do that is:
$db->exec("INSERT INTO `table` (`col1`,`col2`) VALUES ('val1','val2'), ('val1','val2'), ('val1', 'val2')");
Definitely you want to use prepared statements, I recommend first to generate string for placeholders
VALUES (:q1, :q2), (:q3, :q4), (:q5, :q6)
and than generate bindings
[
':q1' => $data['val1'],
':q2' => $data['val2'],
':q3' => $data['val3'],
':q4' => $data['val4'],
//...
],

How to insert bulk record

I am working on API development using AWS API getaway and lambda.
I am using Serverless MySQL https://www.npmjs.com/package/serverless-mysql package for mysql connection and operation.
but I am unable to insert multiple records. If I pass the array of records to insert function it only insert single records.
Please suggest me how would I insert multiple records without using loop.
values=[
[
"229",
25,
"objective",
[
"49"
],
"2019-07-24 08:59:39",
"2019-07-24 08:59:39"
],
[
"229",
26,
"descriptive",
[
"Yes i have long term illness that limits my daily activities. Test..."
],
"2019-07-24 08:59:39",
"2019-07-24 08:59:39"
]
];
var sql = 'INSERT INTO `answers` (`user_id`, `question_id`, `question_type`, `answer`, `created_at`, `updated_at`) VALUES (?)';
await connection.query(sql, values);
I haven't used this package before, but just going through the documentation, it doesn't seems that it provides additional capability to batch insert. So I think you still need to compose the query as you normally do batch insert for mysql.
INSERT INTO table_name (field1,field2,field3) VALUES(1,2,3),(4,5,6),(7,8,9);
The batch mode is not available in this one. So if you want to avoid the loop one option is to compose the query as shown in one answer:
INSERT INTO table_name (field1,field2) VALUES(1,2,3),(4,5,6);
But the better way to do is create a separate lambda function to insert the values by passing sequentially. It will give you more flexibility to insert values across.
https://docs.aws.amazon.com/cli/latest/reference/glue/create-user-defined-function.html

Looking for a PHP or MySQL equivalent for MariaDB's COLUMN_ADD function

I think that the title is pretty explanatory, but just to be clear, I am migrating from MariaDB to Mysql and I am trying to find a solution to rewrite this query, to work on MySQL.
UPDATE `categories` SET attrib=COLUMN_ADD(attrib, '2','48'), updated=NOW() WHERE name = 'category_name'
I've tried like this:
UPDATE `categories` SET attrib=CONCAT(attrib, '2','48'), updated=NOW() WHERE name = 'category_name'
But it just concatenates the values 2 and 48 into 248, and I need the actual value to be a json string and each time I run the query, it should push new data into that json string.
For anyone interested, I fixed it using JSON_SET instead of COLUMN_ADD
UPDATE `categories` SET attrib=JSON_SET(attrib, '$."2"', "48"), updated=NOW() WHERE name = 'category_name'

How to insert a new row into a MySQL database? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to insert values into a mysql database using the following query:
INSERT INTO TestTable (SearchTerm, SearchResult)
VALUES (?, ?), ("TestTerm", "TestResult");
I get the error message that there is an error "near '?, ?) ("TestTerm", "TestResult")' at line 1".
Would someone mind pointing out my mistake?
The ? is used with frameworks like PDO and mysqli. A normal insert looks like this
INSERT INTO TestTable (SearchTerm, SearchResult)
VALUES ('TestTerm', 'TestResult');
or
INSERT INTO TestTable (SearchTerm, SearchResult)
SELECT 'TestTerm', 'TestResult';
Script direct:
INSERT INTO TestTable (SearchTerm, SearchResult)
VALUES ("TestTerm", "TestResult");
With prepared statements:
INSERT INTO TestTable (SearchTerm, SearchResult) VALUES (?, ?);
It looks like you're trying to use a perpared statement. The idea is that the question marks stand in for values that may contain query-breaking character (namely semi-colons and quotes).
If you're using a library, you usually need to use two steps to do this. E.g., in java:
PreparedStatement ps = new PreparedStatement( "INSERT INTO TestTable (SearchTerm, SearchResult) VALUES (?, ?)");
ps.execute("TestTerm", "TestResult");
ps.execute("Escaped;Term", "TestResult"); // the semicolon will be escaped for you
Most every language has generic SQL libraries that allow you to do this. What language are you using?

Transform MySQL 'update' statements to 'insert' statements

I have an 11MB dump containing UPDATE statements, formatted like this:
UPDATE `table` SET `id` = 1,`field`='etc' WHERE `table`.`id` = 1;
However, I need to insert these values into a new database, using statements like this:
INSERT INTO `table` (`id`, `field`) VALUES ('etc', 1);
Has anyone written a tool or service to convert UPDATE statements to INSERT statements? (At first glance, it seems to be just beyond the reach of regex)
Lazy approach. If you have the ids, insert no matter what for those id's and after that run the updates.
Nothing is beyond the reach of a regex. Foreach line in the dump, use the following :
Pattern : ^.*`id` = (\d+).*`field`='(.*)'[^']*WHERE.*$
Replacement : INSERT INTO `table` (`id`, `field`) VALUES ('$2', $1);
This simple regex search and replace in notepad++ will convert any update into an insert:
Search for: UPDATE (.*) SET (.*) WHERE .*$
Replace with: INSERT INTO \1 SET \2;
This assumes that what followed the "where" was originally a unique key. It changes it so that key will auto-increment when you insert.