I am trying to develop a simple method to execute sql queries on my application so I can use native sql for certain things.
This is the method I have:
Session session = getReportCsvMgr().getHibernateSession();
session.beginTransaction();
String sql = String.format("select USER_ID from Users where accountid = 'testaaa'");
Object o = session.createSQLQuery(sql).list();
System.out.println(o.toString());
session.close();
I do not get any errors but somehow the object o is empty and the sysout just prints [].
I debugged and the session works. I tested changing the name of the table and indeed it said "table does not exist". I also tried with and update statement, no errors but it does nothing.
Can anybody tell me what I need to do?
Thanks!
Change the line
Object o = session.createSQLQuery(sql).list();
to:
List<Integer> o = session.createSQLQuery(sql).list();
it the USER_ID is integer or to:
List<String> o = session.createSQLQuery(sql).list();
if the USER_ID is string.
Moreover in a query you have not passed params so you can change:
String sql = String.format("select USER_ID from Users where accountid = 'testaaa'");
to simple:
String sql = "select USER_ID from Users where accountid = 'testaaa'";
Either use .uniqueResult() instead of .list() if it only returns one row or change the return type to List<Object[]>
Related
The code is suppose to check my database to see if there are duplicates of activityName existing if that query runs I am suppose to get an error stating that the activity name is taken else if there isn't any activity name in that database with the same name then the activity name would be inserted into the database. Im suppose to execute the query and get the result as an Integer then use the result in the if and else to see if result>0 in the database
var queryCount= 'SELECT COUNT (activityName) FROM dataEntryTb WHERE activityName = "'+an+'" ';
tx.executeSql(queryCount,[]);
if(queryCount > 0){
navigator.notification.alert("Activity Name Taken");
}else{
Not sure what's the issue exactly but it should work fine. You can consider changing it like
var queryCount= "SELECT 1 FROM dataEntryTb WHERE activityName = '" + an + "'";
Again, consider using parameterized query instead of string concatenation to avoid SQL Injection (if an is coming as user input)
I am trying to make the same sql SELECT for every row.
But, I get a problem in the 'showReader = sqlShowSol.ExecuteReader();'
It says - "Syntax error in query. Incomplete query clause."
Why is it?
-- connection was established before. --
System.Data.OleDb.OleDbCommand sqlShowSol = new System.Data.OleDb.OleDbCommand();
sqlShowSol.Connection = connection;
System.Data.OleDb.OleDbDataReader showReader;
int row = 1;
while (true)
{
sqlShowSol.CommandText = "SELECT Q_A,Content FROM #userName WHERE id = #id;";
sqlShowSol.Parameters.AddWithValue("#userName", userName);
sqlShowSol.Parameters.AddWithValue("#id", row);
showReader = sqlShowSol.ExecuteReader();
|-----------------------------------------------------------------------------------------|
There is more code afterwords...
But I get the problem in the last line I typed here.
Tnx 4 help,
Etay
When you write this:
"SELECT Q_A,Content FROM #userName WHERE id = #id;";
You are using a paramater incorrectly. You are not permitted to use a parameter as the table name. The database engine cannot interpret the #userName reference.
I am trying to retrieve a specific id from a table so that I can get the correct record in my database and use that information. Is there anyway where I can get the actually numerical value of the id from the table. I have tried using linq to SQL but it just queries the database and returns the string like this:
Dim id = From data In dbServer.UserTables Where data.Username = user.username And data.userPassword = user.userPassword Select data.UserID
It does work because then if I count id if it is greater than 1 it will work. But trying to send the value on for example in session state will only have "From data In dbServer.UserTables Where data.UserID = user.username And data.userPassword = user.userPassword Select data.UserID" instead of the actual number. How can I do this correctly?
A Linq query will return a set of data, even if there is only one result.
This will get you an integer (possibly a nullable integer, I can't test right now), that you will have to check for value different than 0 (or null, if it's a nullable).
Dim id = (From data In dbServer.UserTables Where data.UserID = user.username And data.userPassword = user.userPassword Select data.UserID).FirstOrDefault()
The code I'm using deletes the last row in the database. From what I have read, once I've used ORDER BY it will set the result back to read only which makes deleterow() or anything else involving updating the database not possible.
Is there a work around so I could make the table ORDER BY Score DESC and still delete the row I need to delete?
String host = "jdbc:derby://localhost:1527/Scores";
String uName = "root";
String uPass= "root";
Connection con = DriverManager.getConnection( host, uName, uPass );
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
String SQLd = "select * from ROOT.HISCORES ORDER BY Score DESC";
ResultSet dl = stmt.executeQuery( SQLd );
dl.last();
dl.deleteRow();
}
This does not look like a good practice to me.
Instead of SELECTing data into Java and deleting it from there, just do it at the database level. Do a delete-select and skip most of the Java work.
Something like:
DELETE FROM root.hiscores
WHERE Score = (SELECT MIN(hs.Score) FROM root.hiscores hs)
I am trying to convert C# to vb.net in WCF, the given below is the line of code
var user = from u in users
where u.Key == Id
select u.Value;
On using the conversion tool I get the following result
Dim user = _Where u.Key = Id
but simultaneously I get an error 'End of statement expected'
What am I doing wrong? Can anybody help me out on this?
It's about the same using query syntax really:
Dim users = From user In users
Where user.Key = Id
Select user
The Select is degenerate and if you would prefer, you could use method syntax instead:
users.Where(Function(user) user.Key = ID)
Try this:
Dim user = From u in users
Where u.Key = Id
Select u.Value
Dim user = From u in users Where u.Key = id
Select u.Value
You could also use a lambda.
Dim user = users.FirstOrDefault(Function(u)u.Key = id)
In the Lambda I used FirstOrDefault as you are using a key. This mean when the first record has been found, no extra time is wasted on searching the rest of the collection. it also means user will be null if nothing is found. If it was just a Where clause you would potentially end up with an empty collection.