Set a Drupal module's weight on install using db_update

The weight of a module determines the ordering in which it will be called during callback hooks. eg Given 2 modules which implement hook_menu_alter(), the one with weight 0 will run before the one with weight 10.

If you know the weight that a module needs to be, and you can be very sure that no other module might get in there and change it, it can just be set statically.

/**
 * Implements hook_install().
 */
function MYMODULE_install() {
  db_update('system')
    ->fields(array(
      'weight' => 5,
    ))
    ->condition('name', 'MYMODULE')
    ->execute();
}

If on the other hand, the weight needs to be relative to another module then the weight of that module needs first to be queried.

/**
 * Implements hook_install().
 */
function MYMODULE_install() {
  $weight = db_select('system', 's')
    ->fields('s', array('weight'))
    ->condition('name', 'ANOTHER_MODULE')
    ->execute()
    ->fetchField();
 
  db_update('system')
    ->fields(array(
      'weight' => $weight + 1,
    ))
    ->condition('name', 'MYMODULE')
    ->execute();
}

Drupal 6 has slightly different SQL functions;

/**
 * Implements hook_install().
 */
function MYMODULE_install() {
  $weight = db_result(db_query("SELECT weight FROM {system} WHERE name = '%s'", 'ANOTHER_MODULE'));
 
  db_query("UPDATE {system} SET weight = %d WHERE name = '%s'", $weight + 5, 'MYMODULE');
}