I developed a module in drupal 7 that need a database table like "example_table". And i create "example_table" table using example_schema() function. Now I need to insert some default data.

Here, I can use drupal_write_record('example_table', $data); function to insert data into example_install() function.

But, I want to insert data from example_db.sql file.

How can I do it? Any Suggestions?

1 Answer 1


You can try this, hope it helps.

$db = new PDO($dsn, $user, $password);
$sql = file_get_contents('file.sql');
$qr = $db->exec($sql);

Otherwise try this, but i am also not aware of this correctly,

function my_module_install() {
  // Get the contents of the .sql file.
  $sql = file_get_contents(drupal_get_path('module', 'my_module') . '/module.sql');
  // Split into individual statements based on new lines.
  $statements = preg_split('/[\n\r]+/', $sql);
  foreach ($statements as $query) {
  • Thank you Reddy, your first solution working... :), I update my question with answer, for first commer...
    – Tazim
    Aug 12, 2016 at 3:20
  • Hi @A Ajay Kumar Reddy, sorry for removing ans mark. I m working with drupal, maybe drupal has their own system without declaring username and password. So, i just need insert data without declaring username and password.
    – Tazim
    Aug 12, 2016 at 4:17

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.