Thanks for your help. Unfortunately, it still doesn't add data to my database. I created a new database with the .sqlite file extension, and with a table called table1 that has a field called field1.
I managed to get the same program to work with MySQL, so, I have no idea what is wrong.
bool createConnectionSQLite(){
db.setDatabaseName("C:\\NetBeans Projects\\SQLiteExample\\myDataBase.sqlite");
db.open();
if (!db.open()) {
db.lastError().text());
return false;
}
query.prepare("INSERT INTO table1 (field1) VALUES (:field1)");
query.bindValue(":field1", "work damn it");
query.exec();
db.close();
return true;
}
int main(int argc, char *argv[]){
if(!createConnectionSQLite()){
return false;
}
return app.exec();
}
bool createConnectionSQLite(){
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE","myConnection");
db.setDatabaseName("C:\\NetBeans Projects\\SQLiteExample\\myDataBase.sqlite");
db.open();
if (!db.open()) {
QMessageBox::critical(0, QObject::tr("Database Error"),
db.lastError().text());
return false;
}
QSqlQuery query(db);
query.prepare("INSERT INTO table1 (field1) VALUES (:field1)");
query.bindValue(":field1", "work damn it");
query.exec();
db.close();
return true;
}
int main(int argc, char *argv[]){
QApplication app(argc, argv);
if(!createConnectionSQLite()){
return false;
}
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks