Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[solved]qt sql insert statement system not working



  • @ qDebug() << query->exec(QString("insert into accounts (username,password,website,agroup)values('%1','%2','%3','%4')").arg(username,password,website,group));@
    the following code used.
    tried a lot of things and searched a lot debug retruns true but nothing happens nothing is inserted



  • For INSERT, this solution is better (int id, QString tag):
    @
    QSqlQuery qry;
    qry.prepare( "INSERT INTO tags (id, tag) VALUES (:id, :tag)" );
    qry.bindValue( ":id", id );
    qry.bindValue( ":tag", tag );
    if( !qry.exec() )
    qFatal( "Failed to add tag" );
    @



  • idid that does not works
    @ query->prepare("INSERT INTO accounts (username,password,website,agroup) VALUES (:username,:password,:website,:agroup)");
    query->bindValue(":username" , username);
    query->bindValue(":password" , password);
    query->bindValue(":website" , website);
    query->bindValue(":agroup" , group);
    qDebug() << query->exec();//always returns true@



  • try to watch

    @qDebug () << query->lastQuery() ;@



  • "INSERT INTO accounts (username,password,website,agroup) VALUES (:username,:password,:website,:agroup)"



  • Do you have a VALID connection to the database?



  • Modify and use the test code below to locate the cause of error. Also make sure the sql module is included in your project file (QT += sql).

    @ QString tDbFile = "/path../apptempdir/db.sql";
    // Try to connect
    tQSqlDatabase = QSqlDatabase::addDatabase("QSQLITE");
    tQSqlDatabase.setDatabaseName( tDbFile );
    if (!tQSqlDatabase.open())
    {
    QMessageBox::information (this, "Error", "Db connection error");
    return false;
    }

    QString tQueryString = "INSERT...";
    QSqlQuery tSqlQuery;
    tSqlQuery.exec(tQueryString);

    QString tQueryResult = tSqlQuery.lastError().databaseText() + "\n" + tSqlQuery.lastError().driverText();
    QMessageBox::information (this, "Result", tQueryResult);
    @



  • the result:
    1:the QT += sql is included in the project file
    2:the resut of tqueryresult
    "No query
    Unable to fetch row"



  • @broadpeak yes the database.open() returns true and also i am able to create table QSqlTableModel is working fine



  • Are u using QMYSQL. Are u working in qt4



  • @gaya i am using QSQlITE yes i am working in qt4



  • Try this codesnippet:

    @

    void YourClass::init()
    {
    //...
    initDB("../../../db/mydb.db3"); // of course your db path goes here
    //...
    }

    void YourClass::initDB(QString dbPath)
    {
    vc2DB = QSqlDatabase::addDatabase("QSQLITE");
    vc2DB.setDatabaseName(dbPath);
    vc2DB.open();
    QSqlQuery query;
    query.exec("PRAGMA foreign_keys = ON;");
    bool b = query.exec("select id from role where code = 'SU'");
    if (!b) {
    std::cerr << "Failed to open database: " << dbPath.toAscii().data() << std::endl;
    exit(-1);
    }
    }
    @



  • there is not db path set @broadpeak



  • So u've to set the path for db



  • why the table has been created fine.
    also where to seta good path for ana pplication



  • [quote author="developer" date="1353499828"]there is not db path set @broadpeak[/quote]

    What? I don't understand this.

    But! If the open() function can't open your database, it will open with the given name an another database in QSQLITE! See your databaseS.



  • it can open the database @broadpeek



  • even though the file does not exist, the SQLite will try to create it.



  • [quote author="developer" date="1353500452"]it can open the database @broadpeek[/quote]

    Anyway, can you run (from your code) a simple SELECT on your database? Have you got a resultset?
    Have you grant at all for your tables?



  • ohhhhhhhhhh acutally its working with setting url a different databse opens and with just typing passwordmanager as databse name it works actaully the problem is with the table view thanks all problem solved i tried select staement and i got the result



  • it also works after selecting select() thanks all



  • Ok, set your thread to [solved] :)


Log in to reply