Adding data to SQLite through QT - c++

I'm trying to make a registration form in Qt, I'm using SQLite database for this. When I run my program the compiler can open the database, however inserting info into this fails. I'm new to databases and will be very grateful if someone could help me to figure out what's the matter with it.
This is the part of my code about DB
database=QSqlDatabase::addDatabase("QSQLITE");
QString path = QFileDialog::getOpenFileName(0,"Open File","","DB files (*.sqlite) ;; All files (*)");
database.setDatabaseName(path);
if(database.open())
{
//creating a variable username
QString username;
//username equals to the input in the line editor
username= ui->usersname->text();
//creating a variable password
QString password ;
// password equals to the input in the line editor
password = ui->userspassword->text();
//creating a variable email
QString email;
//email equals to the input in the line editor
email = ui->usersemail->text();
//creating a variable phone number
QString phoneNumber ;
//phone number equals to the input in the line editor
phoneNumber= ui->usersphonenumber->text();
//running insert Query
QSqlQuery qry;
//put the info to the table created in the database
qry.prepare("INSERT INFO Users (Name, Password, Email, Phone)"
//select the values for inserting
"VALUES (:username, :password, :email, :phoneNumber)");
//binding values
qry.bindValue(":username", username);
qry.bindValue(":password", password);
qry.bindValue(":email", email);
qry.bindValue(":phoneNumber", phoneNumber);
//if adding info was successful
if(qry.exec())
{
//display a message box
QMessageBox::information(this, "Saved", "Your data was saved successfully, please log in");
}
//if adding info was not successful
else
{
QMessageBox::warning(this, "Failed", "Your data was not saved, please try again");
}
}
else
{
//display a warning message that the database was not connected
QMessageBox::warning(this,"Not Connected","The database is not connected");
}

C++ concatenates your query-string to:
INSERT INFO Users (Name, Password, Email, Phone)VALUES (:username, :password, :email, :phoneNumber)
The problem is that there is no space-character between "[...]Phone)" and "VALUES[...]".
This leads to the VALUES keyword cannot be parsed correctly.
To fix this, just place a space-character at the beginning of the second query-string-part like this:
//select the values for inserting
" VALUES (:username, :password, :email, :phoneNumber)");

Related

Google Script if statement does not check user vs entered data

I have a booking program in Google Sheets where a user can pick their name (uses data verification to provide a list of emails) and then the cell is then placed under protection so no other user can change the booking.
The strange thing that happens is that a person can enter in another person's email and then the cell is protected by the entered email not the user. The user can enter in a non-email string and it does not protect the cell.
The desired result would be that if the user's email and the data entered is the same, protect the cells otherwise it is free to be edited.
Any help would be appreciated!
function onEdit(e){
var CurrentUser = Session.getEffectiveUser().getEmail()
var range_DataEntered = SpreadsheetApp.getActiveRange();
var DataEntered = range_DataEntered.getValue();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SheetName = SpreadsheetApp.getActiveSheet();
var Cell = ss.getActiveCell();
if (CurrentUser = DataEntered) {
var protection = range_DataEntered.protect()
// Ensure the current user is an editor before removing others. Otherwise, if the user's edit
// permission comes from a group, the script will throw an exception upon removing the group.
protection.addEditor(CurrentUser);
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}
else {
//remove protection, set everyone to edit
range_DataEntered.protect().setDomainEdit(true);
}
}
if (CurrentUser = DataEntered) needs to be
if(CurrentUser === DataEntered)
A single = will assign a value not check for equivalency.

List of all customers

I want to create a list of all customers in my custom module I am building, but I can't seem to figure out the problem. I tried this:
public function getAllOptions($withEmpty = true, $defaultValues = false)
{
$source = Mage::getModel('eav/config')->getAttribute('customer', 'firstname');
return $source->getSource()->getAllOptions($withEmpty, $defaultValues);
}
But it gives me an error:
Source model "" not found for attribute "firstname"
#0 /var/www/c8do/app/code/core/Mage/Eav/Model/Entity/Attribute/Abstract.php(387): Mage::exception('Mage_Eav', 'Source model ""...')
When I tried to replace firstname with gender, it works properly (give female/male).
$collection = Mage::getModel('customer/customer')->getCollection()
->addAttributeToSelect('firstname')
->addAttributeToSelect('lastname')
->addAttributeToSelect('email');
If you don't need firstname, lastname or email, you can skip this lines.

Dashcode scroll area refresh with multiple writes

I'm creating a Dashcode project that allows me to mount remote drives. I have a Scroll Area named "statusArea" defined to contain status text of the mounting progress, kind of a command output area.
The first time through testing I'll force the "ERROR: Must enter " text to be written. Then I'll go to the back to provide a username and password and try again, but the box is never cleared for new text.
// get configuration fields from back panel
var usernameField = document.getElementById("username");
var username = usernameField.value;
var passwordField = document.getElementById("password");
var password = passwordField.value;
// clear status
var statusArea = document.getElementById("statusArea");
var status = document.getElementById("content");
status.innerText = "";
statusArea.object.refresh();
if ( !username || !password) {
status.innerText = "ERROR: Must enter username and password on back panel!";
alert("Must enter username and password on back panel!");
}
if (username && password) {
status.innerText = "Connecting as "+username;
Am I going about this the wrong way?
Thanks,
Bill

Extracting university from Facebook education -'education' field not recognised

Hi I'm trying to extract the university of the user from their Facebook profile and found this post to be useful - Getting Education with Facebook Graph API in PHP
However, in my implementation the 'education' field is not being recognised for some reason and is throwing an error in the browser of " Undefined index: education". This is odd because the first and last name and gender are all being retrieved fine but not the 'education' field.
Does anyone know why this is happening?
My code:
// Get the app User ID
$user = $facebook->getUser();
if ($user) {
try {
// If the user has been authenticated then proceed
$user_profile = $facebook->api('/me');
// Extracting profile information to store in database
$fname = $user_profile['first_name'];
$lname = $user_profile['last_name'];
$gender = $user_profile['gender'];
$email = $user_profile['email'];
$college = null;
/* Education data is stored in an array so this iterates over
the contents of that array and checks if the entry is for "College".
*/
foreach ($user_profile['education'] as $education) { // <-ERROR HERE
if ($education['type'] == "College") {
$college = $education;
break;
}
}
}
catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
from the user education column you can read facebook users' school name, id and type. just grab the school name in a variable running fql. for further info plz check: https://developers.facebook.com/docs/reference/fql/user/

Correct way to use QVariant when extracting data from a database

I have recently begun working with QT and I'm having a small issue. I cant seem to understand how QVariant works. I've been going through the help and looking online but it is just not sinking in. In my project i am trying to populate a combo box with a list of manufacturers from a database. I have opened the database and pulled out the entries but I rename them all manValue. Now I understand why this is happening, the problem is I do not understand how to properly use QVariant to get the result I want. Originally i thought "manValue" was going to be the identifier for the string that held the actual value from the database but instead it reads the value from the database and makes sure it is not null, and then renames it. I already tried making a string before I assign the QVariant with any properties and assigning it the text i received from the database, and then inserting that string where manValue is but still no luck. Any help would be greatly appreciated. Sorry, I know this is going to be simple I'm just a noob and the help docs often confuse me.
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("LOCALHOST\\TestERPServer");
db.setDatabaseName("TestERPConnection");
if (db.open())
{
QMessageBox::information(this,"Connected","Connection to the Database was Established\n"
"\nStatus: Connected");
QSqlQuery mfrQry;
if (mfrQry.exec("SELECT * FROM erp_data.manufacturers;"))
{
if (mfrQry.value(1) == "")
{
QMessageBox::information(this,"No Connection","Nothing in the Manufacturer Database\n"
"\nError: " + db.lastError().text());
}
else
{
while (mfrQry.next())
{
ui->mfrComboBox->addItem("manValue",QVariant(mfrQry.value(1)));
}
}
}
else
{
QMessageBox::information(this,"No Connection","Connection to the Manufacturer Database could not be Established\n"
"\nError: " + db.lastError().text());
}
}
else
{
QMessageBox::information(this,"No Connection","Connection to the Database could not be Established\n"
"\nError: " + db.lastError().text());
}
The first problem in the provided code has to do with the way you manipulate the QSqlQuery.
Successfully executed SQL statements set the query's state to active
so that isActive() returns true. Otherwise the query's state is set to
inactive. In either case, when executing a new SQL statement, the
query is positioned on an invalid record. An active query must be
navigated to a valid record before values can be retrieved.
In order to move to a valid record you have to use one of the following:
next()
previous()
first()
last()
seek()
Once you are on a valid record you have to use the value() function in order to take the column you want. The returned value is QVariant so you need to convert to the desired type using one of the many toSomething functions QVariant provides.
So in your case the code should look like this:
QSqlQuery mfrQry;
if (mfrQry.exec("SELECT * FROM erp_data.manufacturers;"))
{
// This will loop through all records returned by the query
while (mfrQry.next()) {
// mfrQry.value(COLID) returns a QVariant containing the data of the current
// record in column COLID.
// Using toString we convert it to String
QString stringValue = mfrQry.value(COLID).toString();
// Now handle the QString the way you want...
}
}