
Originally Posted by
KeineAhnung
qry.prepare("insert into table (name,password,date) values ('"+str1+"', "+str2+", "+date.toString()+")");
qry.prepare("insert into table (name,password,date) values ('"+str1+"', "+str2+", "+date.toString()+")");
To copy to clipboard, switch view to plain text mode
First of all, the Sqlite documentation shows that table is a reserved keyword, so you should avoid using a table name of "table" as you show in your insert statement. Not sure if that's allowed (I didn't actually try it), but you should avoid using reserved keywords as object names or column names, etc.
Secondly, it doesn't look to me like you have formatted the query values correctly. The name value is properly quoted in single quotes, but you're missing single quotes around the password and the date values which also need to be quoted unless they are numeric values.
As a best practice, you should not build the SQL query like that, you should either use use parameterized queries as such:
qry.prepare("insert into table (name,password,date) values (?,?,?)";
qry.bindValue(0,str1);
qry.bindValue(1,str2);
qry.bindValue(2,date.toString());
qry.exec();
qry.prepare("insert into table (name,password,date) values (?,?,?)";
qry.bindValue(0,str1);
qry.bindValue(1,str2);
qry.bindValue(2,date.toString());
qry.exec();
To copy to clipboard, switch view to plain text mode
or even better, use named query parameters:
qry.prepare("insert into table (name,password,date) values (:name,:password,:date)");
qry.bindValue(":name", str1);
qry.bindValue(":password", str2);
qry.bindValue(":date", date.toString());
qry.exec();
qry.prepare("insert into table (name,password,date) values (:name,:password,:date)");
qry.bindValue(":name", str1);
qry.bindValue(":password", str2);
qry.bindValue(":date", date.toString());
qry.exec();
To copy to clipboard, switch view to plain text mode
Lastly, when you created the QSqlQuery qry on the stack, you used the default constructor, which uses an empty query string and a default QSqlDatabase() instance. You should have specified the DB instance in the constructor. This associates your query with the correct database instance (namely, the instance you already opened). Since you have not done that, this may also be the source of your "Parameter count mismatch" error as well. Here's how you should have specified the QSqlQuery declaration:
QSqlQuery qry
(db
);
// you don't show what your QSqlDatabase variable is named, so I used db as an example
QSqlQuery qry(db); // you don't show what your QSqlDatabase variable is named, so I used db as an example
To copy to clipboard, switch view to plain text mode

Originally Posted by
KeineAhnung
I have read that there are issues if you create a SQLite DB with Qt but I did not. Does anyone see what I did wrong?
There are no issues creating or using Sqlite databases with Qt. There is no need to close/reopen the database before you write to it.
Good luck.
Jeff
Bookmarks