[solved]qt sql insert statement system not working
-
wrote on 19 Nov 2012, 12:17 last edited by
@ 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 -
wrote on 19 Nov 2012, 15:06 last edited by
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" );
@ -
wrote on 20 Nov 2012, 12:33 last edited by
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@ -
wrote on 20 Nov 2012, 12:39 last edited by
try to watch
@qDebug () << query->lastQuery() ;@
-
wrote on 20 Nov 2012, 12:41 last edited by
"INSERT INTO accounts (username,password,website,agroup) VALUES (:username,:password,:website,:agroup)"
-
wrote on 20 Nov 2012, 13:11 last edited by
Do you have a VALID connection to the database?
-
wrote on 20 Nov 2012, 19:50 last edited by
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);
@ -
wrote on 21 Nov 2012, 11:29 last edited by
the result:
1:the QT += sql is included in the project file
2:the resut of tqueryresult
"No query
Unable to fetch row" -
wrote on 21 Nov 2012, 11:30 last edited by
@broadpeak yes the database.open() returns true and also i am able to create table QSqlTableModel is working fine
-
wrote on 21 Nov 2012, 11:37 last edited by
Are u using QMYSQL. Are u working in qt4
-
wrote on 21 Nov 2012, 11:38 last edited by
@gaya i am using QSQlITE yes i am working in qt4
-
wrote on 21 Nov 2012, 11:57 last edited by
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);
}
}
@ -
wrote on 21 Nov 2012, 12:10 last edited by
there is not db path set @broadpeak
-
wrote on 21 Nov 2012, 12:13 last edited by
So u've to set the path for db
-
wrote on 21 Nov 2012, 12:14 last edited by
why the table has been created fine.
also where to seta good path for ana pplication -
wrote on 21 Nov 2012, 12:17 last edited by
[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.
-
wrote on 21 Nov 2012, 12:20 last edited by
it can open the database @broadpeek
-
wrote on 21 Nov 2012, 12:24 last edited by
even though the file does not exist, the SQLite will try to create it.
-
wrote on 21 Nov 2012, 12:25 last edited by
[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? -
wrote on 21 Nov 2012, 12:51 last edited by
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
7/22