Пример #1
0
void MainWidget::Process()
{
  QString sql;
  QSqlQuery *q;

  //
  // Open DB Connection
  //
  moji_db=QSqlDatabase::addDatabase(moji_db_type_box->currentText());
  if(!moji_db) {
    PrintDbError(tr("Database initialization error"));
    return;
  }
  moji_db->setDatabaseName(moji_db_dbname_edit->text());
  moji_db->setHostName(moji_db_hostname_edit->text());
  moji_db->setUserName(moji_db_username_edit->text());
  moji_db->setPassword(moji_db_password_edit->text());
  if(!moji_db->open()) {
    PrintDbError(tr("Database connection error"));
    QSqlDatabase::removeDatabase(moji_db);
    return;
  }

  //
  // (Perhaps) set the connection character set
  //
  if(!moji_charset_edit->text().isEmpty()) {
    sql=QString("set names ")+moji_charset_edit->text();
    q=new QSqlQuery(sql);
    if(!q->isActive()) {
      PrintDbError(tr("SQL SET NAMES error"));
      delete q;
      QSqlDatabase::removeDatabase(moji_db);
      return;
    }
  }

  //
  // Insert the Row
  //
  q=new QSqlQuery(InsertionSql());
  if(!q->isActive()) {
    PrintDbError(tr("SQL INSERT error"));
    delete q;
    QSqlDatabase::removeDatabase(moji_db);
    return;
  }
  delete q;

  //
  // Read back the Row
  //
  sql=QString("select MOJIFIELD from MOJITABLE ")+
    "where ID=LAST_INSERT_ID()";
  q=new QSqlQuery(sql);
  if(!q->isActive()) {
    PrintDbError(tr("SQL SELECT error"));
    QSqlDatabase::removeDatabase(moji_db);
    return;
  }
  if(q->first()) {
    moji_result_text->setText(q->value(0).toString());
  }
  delete q;

  QSqlDatabase::removeDatabase(moji_db);
}
Пример #2
0
bool OpenDb(QString dbname,QString login,QString pwd,
	    QString host,QString stationname,bool interactive)
{
  // 
  // Yeesh, this whole method really needs a rewrite!
  // They shoot horses, don't they??
  //

  QString admin_name;
  QString admin_pwd;
  QString msg;
  QString str;
  QString sql;
  QSqlQuery *q;

  //
  // Open Database
  //
  QSqlDatabase *db=QSqlDatabase::addDatabase(admin_config->mysqlDriver());
  if(!db) {
    return false;
  }
  db->setDatabaseName(dbname);
  db->setUserName(login);
  db->setPassword(pwd);
  db->setHostName(host);
  if(!db->open()) {
    RDKillDaemons();
    PrintDbError(QObject::tr("Unable to open MySQL database connection."),
		 interactive);
  }

  //
  // Identify DB
  //
  sql=QString("show tables");
  q=new QSqlQuery(sql);
  if(q->first()) {
    delete q;
    sql=QString("select DB from VERSION");
    q=new QSqlQuery(sql);
    if(q->first()) {
      if(q->value(0).toInt()!=RD_VERSION_DATABASE) {
	PrintDbError("Unsupported database version",interactive);
      }
    }
    else {
      PrintDbError("Database is corrupt",interactive);
    }
  }
  else {
    CreateDb();
    InitDb(stationname);
    if(interactive) {
      QMessageBox::information(NULL,"RDAdmin - "+QObject::tr("DB Message"),
			       QObject::tr("Created new Rivendell database."));
    }
  }
  delete q;

  return true;
}