how to make sure, username and password doesn't get duplicated?
-
#include "idcreator.h"
#include "ui_idcreator.h"
#include<QMessageBox>
#include"mainwindow.h"
#include "voterid.h"
#include"QtWidgets"
MainWindow *h2;
VoterID *h;
idcreator::idcreator(QWidget *parent) :
QDialog(parent),
ui(new Ui::idcreator)
{
ui->setupUi(this);
mydb=QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("C:/SQLite/Database.db");
if(!mydb.open())
ui->connect->setText("Something is wrong, try restarting");
else
ui->connect->setText("PLEASE SIGN UP");
}idcreator::~idcreator()
{
delete ui;
}void idcreator::on_save_clicked()
{
QString username, password;
username=ui->username->text();
password=ui->password->text();QSqlQuery query; query.prepare( "INSERT INTO signup (username, password) VALUES (?, ?)" ); query.addBindValue(username); query.addBindValue(password); if(query.exec()){ QMessageBox::StandardButton reply = QMessageBox::question(this,"SignUp Successful","Use same username and password to vote ", QMessageBox:: Open | QMessageBox:: Cancel ); if (reply==QMessageBox::Cancel){ hide(); h2= new MainWindow(this); h2->show(); } else{ hide(); h=new VoterID(this); h->show(); } } else{ QMessageBox::critical(this,"SignUp Failed","Signup error, try again "); } }
-
@Sapok_5
Hello and welcome.When pasting blocks of code, please use the Code tag you can see when composing a post (looks like
</>
), or type a line with 3-backticks (```
) both above & below the code block.If mean you want to ensure that the username and/or password are not already in the
signup
table then first issue aSELECT
query to check the value(s) are not already there before executing theINSERT
statement.If
username
is the primary key into thesignup
table (which it probably/ought to be) you will not be able toINSERT
another row with ausername
which is already there anyway --- theINSERT
would return an error.As a separate issue:
mydb=QSqlDatabase::addDatabase("QSQLITE");
Your
mydb
must be member variable inidcreator
class. The docs forQSqlDatabase
tell you not to keep an instance in existence in a class:Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.
-
Hi,
For username, as @A-A-SEZEN suggests, use the unique keyword. As for the password, the fact that several of your users may have the same is not your problem. However, the real issue you have here is that you are storing password as plain text. This is wrong on so many level that I will not try to count. Please start directly with the proper logique to encrypt them.
-
@Sapok_5
Writing the text of the query requires SQL knowledge, not Qt knowledge. You will need that (or at least the basics) to get anywhere with database programming.If, say, the user enters a username and you wanted to know if there is already a row in the table with that username you might do something like:
query.prepare( "SELECT username, password FROM signup WHERE username = ?" ); query.addBindValue(username); if (query.exec()) ...
Either it returns an existing row with that same username, or it returns no rows, so that's how you know.
However, as we have said earlier, if (in SQL) you declare the
username
column in the table as either the primary key or unique it would not allow you toINSERT
a new row with a duplicateusername
value, it would raise an error instead an not insert the new row. You do not want to allow a duplicate username, so you ought do one of these to maintain the integrity of your data. -
@JonB
#include "idcreator.h"
#include "ui_idcreator.h"
#include<QMessageBox>
#include"mainwindow.h"
#include"voteme.h"
MainWindow *h2;
voteme *vm;
idcreator::idcreator(QWidget *parent) :
QDialog(parent),
ui(new Ui::idcreator)
{
ui->setupUi(this);
mydb=QSqlDatabase::addDatabase("QSQLITE");
mydb.setDatabaseName("C:/SQLite/Database.db");
if(!mydb.open())
ui->connect->setText("Something is wrong, try restarting");
else
ui->connect->setText("PLEASE SIGN UP");
}idcreator::~idcreator()
{
delete ui;
}
int i;
void idcreator::on_save_clicked()
{
QString username, password;
username=ui->username->text();
password=ui->password->text();QSqlQuery query; query.prepare( "SELECT *FROM signup WHERE username = '"+username+"' "); if(query.exec()){ if(username==""||password==""){ QMessageBox::critical(this,"SignUp Failed","Please Enter Username or password "); } else{ if(query.next()){ QMessageBox::critical(this,"SignUp Failed","Enter different Username "); } else{ query.prepare( "INSERT INTO signup (username, password) VALUES (?, ?)" ); query.addBindValue(username); query.addBindValue(password); if(query.exec()){ QMessageBox::StandardButton reply = QMessageBox::question(this,"SignUp Successful","Use same username and password to vote ", QMessageBox:: Open | QMessageBox:: Cancel ); if (reply==QMessageBox::Cancel){ hide(); delete h2; h2=new MainWindow(this); h2->show(); } else{ hide(); vm=new voteme(this); vm->show(); } } else{ QMessageBox::critical(this,"SignUp Failed","Signup error, try again "); } ui->connect->setText("verified"); } } }
}
i did this, and i reduced the duplication problem, however i still fell something is missing. it works perfectly fine, hope u can tell me something that would help reduce trouble for my application.
-
@Sapok_5
I would not expect this to work, to tell you whether there is actually a row with the username in existence. You execute theSELECT
query, but you do nothing about reading the resulting rows back. You need to do so to see whether theSELECT
returns 0 or 1 rows. -
@Sapok_5 you really should take the time to learn the basics of the tools you want to wield. You are doing your empty checks too late (your users should not even be allowed to click on the button if any of the fields are empty). Building the query the way you do it opens the door to SQL injection and all the nasty things that come with it.