Results 1 to 4 of 4

Thread: unable to open the database

  1. #1
    Join Date
    Dec 2011
    Location
    Bangalore,India
    Posts
    38
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default unable to open the database

    hi , hope every one are doing good...
    I am trying to create a table from qt on my sql server 2008, the code which i have tried throws a error, that "unable to open the database". kindly do anyone please give me solutions and i have pasted my code below..

    Thanks

    Code:


    void sas_config:n_cLB_save_clicked()
    {
    addDataBase();
    }


    void sas_config::addDataBase()
    {

    QString driverName="QODBC",databaseName="DRIVER={SQL Server};SERVER=KULASEKARAN;DATABASE=test",hostName ="KULASEKARAN",
    userName="kulasekaran",password="1234";
    QSqlError err = addConnection(driverName,databaseName,hostName,
    userName, password);
    if (err.type() != QSqlError::NoError)
    QMessageBox::warning(this, tr("Unable to open database"), tr("An error occurred while "
    "opening the connection: ") + err.text());

    }


    SqlError sas_config::addConnection(const QString &driver, const QString &dbName, const QString &host,
    const QString &user, const QString &passwd)
    {
    QSqlError err;
    bool found =false;
    QSqlDatabase db = QSqlDatabase::addDatabase(driver);
    db.setDatabaseName(dbName);
    db.setHostName(host);
    if (!db.open(user, passwd))
    {
    err = db.lastError();
    qDebug()<<"Error"<<err;
    db = QSqlDatabase();
    QSqlDatabase::removeDatabase(QString("test"));
    return err;
    }

    if (db.isOpen()) {
    foreach (QString table, db.tables())
    {
    if(table == "kul")
    {
    found = true;
    qDebug()<<"Table Found";
    break;
    }
    }
    if(!found)
    {

    query.exec("CREATE TABLE kul (Name VARCHAR(32), ID VARCHAR(16), Height NUMERIC, Device NUMERIC)");
    qDebug()<<"Created the table";
    }
    QTreeView *view = ui->treeView;
    Q_CHECK_PTR(view);
    model = new QSqlTableModel(this,db);
    model->setTable("kul");
    model->setEditStrategy(QSqlTableModel::OnFieldChange);
    model->select();
    view->setModel(model);
    }
    }

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: unable to open the database

    Does the database specified by connection parameters exists ? What's the output of
    Qt Code:
    1. QSqlDatabase::drivers()
    To copy to clipboard, switch view to plain text mode 
    ?

  3. #3
    Join Date
    Dec 2011
    Location
    Bangalore,India
    Posts
    38
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: unable to open the database

    hi stampede ,
    The database connection specified is exists and I am trying to connect it with sql server
    The output of QSqlDatabase::drivers().. gives me the driver which I have connected that I have mentioned in the addDatabase().

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: unable to open the database

    Can you try to open that database with "sqlbrowser" application from qt examples ?

Similar Threads

  1. Unable to send database file over LAN
    By webjack in forum Newbie
    Replies: 1
    Last Post: 7th October 2013, 21:15
  2. Unable to work with database
    By Atomic_Sheep in forum Newbie
    Replies: 9
    Last Post: 18th August 2013, 18:17
  3. Unable to open Help option in Qt creator.
    By Channareddy in forum Qt Tools
    Replies: 2
    Last Post: 22nd July 2011, 10:48
  4. [SOLVED] database opened .. database not open
    By kapitanluffy in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2011, 10:39
  5. Replies: 9
    Last Post: 20th May 2010, 09:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.