Using sqlite database with qt - sqlite

Here is my code, there doesn't seem to be anything wrong:
QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("thedata.sqlite");
db.open();
QSqlQuery query;
query.prepare("SELECT lastname FROM people where firstname='?' ");
query.bindValue(0, lineEdit->text());
bool x = query.exec();
if(x){
lineEdit_2->setText(query.value(0).toString());
}
else {
QSqlError err;
err = query.lastError();
QMessageBox::about(this,"error",err.text() );
}
When the program is working always it gives the error parameter count mismatch I'm using qt 4.8 and its own headers for using sqlite.
I would be very thankful for any advice, though I searched in google i see many posts in this issue but nothing helped me.
Thank you.

You're prepared statement is wrong, it should be:
quary.prepare("SELECT lastname FROM people where firstname=?");
Notice that there are no single quotes (') around the placeholder. If you put the quotes, it gets passed as a literal to the database, leaving you with a parameter-less query and code that passes too many parameters.
(Changing that variable name to query would be a nice touch too.)
Also you need to check the return value if QSqlQuery::prepare, and print out/display the error message you're getting from that if it fails – otherwise QSqlQuery::exec resets the current error and you'll get a pretty meaningless error message if there was a problem parsing the prepared statement.
if(x){
lineEdit_2->setText(quary.value(0).toString());
}
This is incorrect too. The you need to call (and check the return value of) query.next() to position the result set to the first row returned (if there is one). You can't use .value(X) before you've called .next().

Related

Why does this query crash if there are no matching results instead of returning an empty result?

I am using this query to get some statistics from our DB
SELECT DateTimePart("yyyy", c.RedeemedDate) AS RedeemedInYear,
DateTimePart("m", c.RedeemedDate) AS RedeemedInMonth,
AVG(c.RedPriceStoreSummary ?? c.WhitePriceStoreSummary) AS AverageBasketValue
FROM c
WHERE c.IsRedeemed = true
AND c.Brand = 'xxx'
AND c.RedeemedDate != null
AND DateTimePart("yyyy", c.RedeemedDate) = DateTimePart("yyyy", GetCurrentDateTime())
GROUP BY DateTimePart("m", c.RedeemedDate),
DateTimePart("yyyy", c.RedeemedDate)
The problem is that query crashes with the following error if there are no results
Cannot read property 'create' of undefined
If I force the query to where it gets results then everything works fine but I dont want the query to crash if there are no results I want an empty result set.
Am I missing something here?
You need to trap queries in a try block and catch if it returns a 404. This is a hold over in behavior from the previous SDK. Alternatively you can use the stream variant of these SDK functions which will not throw exceptions when data is not found, however you still want to check the http response code and you'll also need to manually deserialize the data.

Perl dancer error execute method undefined

I was trying to make a website using perl dancer, below is my code. It seems to be correct but the page keeps loading and never enters the values in the database. When I cancel the page I get an error stating "request to POST /appform crashed: Can't call method "execute" on an undefined value". I can't figured out whats wrong in the code. If you have any other code please mention.
I am using SQLite for database.
There is a database campus.dband I am inserting the value in student table.
post '/appform' => sub {
my $q = CGI ->new;
my $name = $q->param ("firstname");
my $password = $q->param("password");
my $mobile_no = $q->param("mobile");
my $gender = $q->param("gender");
my $email = $q->param("email");
my $address = $q->param("address");
my $sslc = $q->param("SSLC");
my $hsc = $q->param("HSC");
my $cgpa = $q->param("cgpa");
my $languages = $q->param("lang");
my $internships = $q->param("intern");
my $preferred_loc = $q->param("country");
my $sql = "insert into student(name,mobile_no,gender,email,address,sslc,hsc,cgpa,languages,internships,preferred_loc,password,applied_job,company_applied) values ('?','?','?','?','?','?','?','?','?','?','?','?','?','?');";
my $sth = database->prepare($sql);
$sth->execute($name,$mobile_no,$gender,$email,$address,$sslc,$hsc,$cgpa,$languages,$internships,$preferred_loc,$password) or die $sth->errstr;
#$sth->execute();
$sth-> finish;
set_flash('New entry posted!');
redirect '/';
};
You're using the database keyword to get a database handle. I'm guessing that's coming from Dancer2::Plugin::Database (it would be useful if you could include information like this in your question).
The error says that you're calling execute() on an undefined value. You're calling execute() on the variable $sth. So $sth is undefined. You get $sth by calling prepare() on the database handle returned from database(). So it looks like the prepare() call is failing. You should check the return value from that call and throw an error if it fails.
The most common reason for prepare() to fail is that you're trying to compile an SQL statement that contains an error. I can't see any obvious error in your SQL, but it's worth checking it by running it manually against your database.
I see you're using bind params in your SQL statement. That's a great idea, but please note that you don't need to quote the question marks in your SQL - the database driver will handle that for you. I don't think that's what is causing your problem though.
I also see that you're using CGI.pm inside your Dancer app to get the request parameters. To be honest, I'm slightly surprised that it works - but it's a terrible idea. Dancer has its own keywords that will give you this information. Look at query_parameters(), body_parameters() and route_parameters() in the Dancer documentation.
In addition to the points made already, that your DBI prepare() call is probably failing (add error-checking to see why, e.g. my $sth = database->prepare('...') or die "DB error: " . database->errstr) and that you're using CGI.pm within a Dancer app (... don't do that, I'm surprised it would work at all - look at the Dancer documentation for how to access the params your app was sent), look also at the quick_insert convenience method provided by Dancer::Plugin::Database / Dancer2::Plugin::Database so that you don't have to write that SQL INSERT statement at all.

Linq returning wrong value from DB

I have a super simple select in a wcf service like
BusinessModel.Candidate candidateObject
= dcMUPView.Candidates.SingleOrDefault(dev => dev.Username == username);
But when I hit this code, and then check the candidateObject, it's candidateid value is incorrect. In the DB candidateid is the identity column and primary key. In this case it is 2572884 in the DB but when I look in the candidateObject is says the value returned is something like 0x00274254. Anyone know what might cause something like this?
I think your query is returning the correct record, since 0x00274254 (base 16) = 2572884 (base 10).
If you are checking the value using the debugger, make sure you have the Hexadecimal Display option turned off (see here for more info: Visual Studio debugger - Displaying integer values in Hex).

How to get inserted row ID with WebMatrix

There is a GetLastInsertId method in WebMatrix. However, using Reflector, I can see that it's implemented like this:
[return: Dynamic]
public object GetLastInsertId()
{
return this.QueryValue("SELECT ##Identity", new object[0]);
}
Which, as far as I can see, is a very bad way of doing it, because ##Identity returns the last insert considering every table and every session. I need this restricted to the scope and session I'm working with, so I was expecting this to use SELECT SCOPE_IDENTITY(), since that also seems to be what is most often used according to my reading.
My questions are:
Do the WebMatrix wrappers do something that makes this ok to use in my case?
If not, is there a simple way to get the inserted ID of an insert query using WebMatrix classes, or should I fall back on stuff like SqlClient for this?
I am interested in answers for SQL Server (not compact) if that makes a difference.
I had the same problem, and I read these answers but still couldn't get the code to work correctly. I possibly was misreading the solutions above, but what seemed to work for me was as follows:
var db=Database.Open(...);
var lastInsertID = db.QueryValue("INSERT name into Names; SELECT SCOPE_IDENTITY()");
To quote David Fowler, one of the devs on the Data APIs in WebMatrix: "[SCOPE_IDENITTY] doesn't work on ce". Since we wanted a data API that would work on both CE and regular SQL Server, we used ##Identity. So if you want to use SCOPE_IDENTITY specifically, you should use db.QueryValue method:
var db = Database.Open(...);
db.Insert(...);
var lastInsertId = db.QueryValue("SELECT SCOPE_IDENTITY()")
here is my solution
var db.Database.open('..');
var lastId = db.GetLastInsertId();

Does "insert" in SQLite return SQLITE_OK or SQLITE_DONE?

What does statement "insert" in SQLite return in case of success?
I always believed that it should be SQLITE_DONE, but recently in my logs I found out the following string:
sqlite3_step error: 'not an error'
And here is the code that logs the mentioned string:
prepareStatement(addTranslationStmt2, "INSERT INTO translations(lang1_wordid, lang2_wordid) VALUES(?, ?)");
if (!addTranslationStmt2) return -2;
sqlite3_bind_int(addTranslationStmt2, 1, word_id);
sqlite3_bind_int(addTranslationStmt2, 2, translation_id);
if(sqlite3_step(addTranslationStmt2) != SQLITE_DONE)
{
NSLog(#"sqlite3_step error: '%s'", sqlite3_errmsg(database));
sqlite3_reset(addTranslationStmt2);
return -1;
}
sqlite3_reset(addTranslationStmt2);
I am wondering, why does it work in most cases.
Should I change SQLITE_DONE in my code to SQLITE_OK?
Thanks.
SQLITE_DONE
http://www.sqlite.org/c3ref/step.html
You could also try printing out the error code to find out what the problem is.
In cases like these, I like to look at code samples. Here are some good ones:
http://sqlite.phxsoftware.com/forums/p/76/6659.aspx
The SQLite Result Codes Reference lists SQLITE_OK as indicating a successful result. It is also the first error code, having an error code of 0, making it the canonical result (i.e. the result I would expect on a successful operation).
You should put a breakpoint or print statement in your code to find out if it really is returning zero, and check your data to make sure you're getting the result you expect. If that all checks out, I would change your condition to check for SQLITE_OK.
The details of the behavior of the sqlite3_step() interface depend on whether the statement was prepared using the newer "v2" interface sqlite3_prepare_v2() and sqlite3_prepare16_v2() or the older legacy interface sqlite3_prepare() and sqlite3_prepare16().
In the legacy interface, the return value will be either SQLITE_BUSY, SQLITE_DONE, SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE. With the "v2" interface, any of the other result codes or extended result codes might be returned as well.
Switching from "Debug" configuration to "Release" resolved the issue for me.

Resources