I have the following INSERT in a db_query, but feel like I should convert it to a db_insert, but can't figure out the syntax:

INSERT INTO {mytable} VALUES (23, 'foo', 'bar', 42)

This example is fairly short, but I do work with some legacy tables that have MANY fields.

$insert = db_insert('variables')->values(array(23, 'foo', 'bar', 42));

The above gives me "NoFieldsException: There are no fields available to insert with. in InsertQuery->preExecute()".

How can you perform an insert with db_insert without needing to specify each field name? I can make it work in D7 with db_query([INSERT...]); but drupal_write_record does not work.

1 Answer 1


You can not do this with db_insert(). You could probably dig into the InsertQuery class and get it done, but that's no different than what you're already doing IMO.

I have never seen INSERT without specifying column names, for good reason, although apparently you can (mysql api). If you do this, it makes it more difficult for someone else to understand what you've done, and impossible to alter the table structure without causing headaches.

You can look at the waydrupal_write_record() uses drupal_get_schema() to create the fields if you'd like to automate the process (although it would probably take just as long to write the $fields array one time).

  • Thanks. I was afraid that was the case, and probably is much more time to get it to work. INSERT without columns is standard SQL, but like you said if someone changes the table schema it would be a headache. The table I am using is legacy and chiseled in stone at this point :) Commented Jul 17, 2013 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.