How to insert data from Qt to PostgreSql(DataBase) ?
-
wrote on 25 Jan 2023, 19:10 last edited by
I am trying with following code but data is not inserting in postgreSql(Database) . Where is error in my code ? Suggest please.
QSqlQuery qsql;
qsql.prepare("INSERT INTO prod.user_details(user_id, first_name, last_name, login_name, login_password)"
"VALUES (?, ?, ?, ?, ?)");
qsql.bindValue(0,23);
qsql.bindValue(1, "ranjeet");
qsql.bindValue(2, "keshari");
qsql.bindValue(3,"ranjeet789");
qsql.bindValue(4,"ranjeetmicro"); -
Hi,
With the code you show, there's at least nothing executed so that would be the main issue.
Otherwise, it would be way simpler to check the value returned by exec and if false use QSqlQuery's lastError method to get what went wrong.
-
I am trying with following code but data is not inserting in postgreSql(Database) . Where is error in my code ? Suggest please.
QSqlQuery qsql;
qsql.prepare("INSERT INTO prod.user_details(user_id, first_name, last_name, login_name, login_password)"
"VALUES (?, ?, ?, ?, ?)");
qsql.bindValue(0,23);
qsql.bindValue(1, "ranjeet");
qsql.bindValue(2, "keshari");
qsql.bindValue(3,"ranjeet789");
qsql.bindValue(4,"ranjeetmicro");wrote on 25 Jan 2023, 19:14 last edited by@Pappu-Kumar-Keshari
We don't know what else you are doing, what you do with the query, how the database is open, what the table schema is, whether you have permission....What did
QSqlQuery::lastError()
tell you? -
QSqlQuery::prepare() returns a boolean which you should check and if it fails use QSqlQuery::lastError() to retrieve the actual error string. Also your code does not show how/where you exec() your query.
-
@Pappu-Kumar-Keshari
We don't know what else you are doing, what you do with the query, how the database is open, what the table schema is, whether you have permission....What did
QSqlQuery::lastError()
tell you?wrote on 27 Jan 2023, 04:34 last edited by@JonB I am able to fetch data from data base to Qt Successfully. But I am unable to insert data from Qt to data base that's problematic. I am using this code but data is not inserting
I am trying with following code but data is not inserting in postgreSql(Database) . Where is error in my code ? Suggest please.
QSqlQuery qsql;
qsql.prepare("INSERT INTO prod.user_details(user_id, first_name, last_name, login_name, login_password)"
"VALUES (?, ?, ?, ?, ?)");
qsql.bindValue(0,23);
qsql.bindValue(1, "ranjeet");
qsql.bindValue(2, "keshari");
qsql.bindValue(3,"ranjeet789");
qsql.bindValue(4,"ranjeetmicro");0
-
QSqlQuery::prepare() returns a boolean which you should check and if it fails use QSqlQuery::lastError() to retrieve the actual error string. Also your code does not show how/where you exec() your query.
wrote on 27 Jan 2023, 05:04 last edited by@Christian-Ehrlicher I am able to fetch data from data base to Qt Successfully. But I am unable to insert data from Qt to data base that's problematic. I am using this code but data is not inserting
I am trying with following code but data is not inserting in postgreSql(Database) . Where is error in my code ? Suggest please.QSqlQuery qsql;
qsql.prepare("INSERT INTO prod.user_details(user_id, first_name, last_name, login_name, login_password)"
"VALUES (?, ?, ?, ?, ?)");
qsql.bindValue(0,23);
qsql.bindValue(1, "ranjeet");
qsql.bindValue(2, "keshari");
qsql.bindValue(3,"ranjeet789");
qsql.bindValue(4,"ranjeetmicro"); -
@Christian-Ehrlicher I am able to fetch data from data base to Qt Successfully. But I am unable to insert data from Qt to data base that's problematic. I am using this code but data is not inserting
I am trying with following code but data is not inserting in postgreSql(Database) . Where is error in my code ? Suggest please.QSqlQuery qsql;
qsql.prepare("INSERT INTO prod.user_details(user_id, first_name, last_name, login_name, login_password)"
"VALUES (?, ?, ?, ?, ?)");
qsql.bindValue(0,23);
qsql.bindValue(1, "ranjeet");
qsql.bindValue(2, "keshari");
qsql.bindValue(3,"ranjeet789");
qsql.bindValue(4,"ranjeetmicro");@Pappu-Kumar-Keshari Please read again what @Christian-Ehrlicher suggested and follow that suggestion - you have zero error handling, please add. And again: you do NOT show where and how you actually insert the data (exec()). Please don't ignore questions and suggestions if you ask for help...
-
@Pappu-Kumar-Keshari
We don't know what else you are doing, what you do with the query, how the database is open, what the table schema is, whether you have permission....What did
QSqlQuery::lastError()
tell you?wrote on 27 Jan 2023, 08:37 last edited byThis post is deleted!
1/8