|
how to retrive data from database in android
|
|
|
|
|
|
Hello,
I'm trying to call the following MySQL SP:
DROP PROCEDURE IF EXISTS tgc.UpdateCustomerBalance;
CREATE PROCEDURE tgc.`UpdateCustomerBalance`(customerId varchar(50), cost Decimal(8,2))
BEGIN
UPDATE Customers SET Balance = Balance - cost WHERE CustomerId = customerId;
END;
with the following C# code:
try
{
using (var context = new Db_Entities())
{
object[] parameters =
{
new MySql.Data.MySqlClient.MySqlParameter("@customerId", customerId),
new MySql.Data.MySqlClient.MySqlParameter("@cost", cost)
};
await context.Database.ExecuteSqlCommandAsync("UpdateCustomerBalance @customerId,@cost", parameters);
}
}
catch (Exception e)
{
throw;
}
But I receive the error:
"Message": "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UpdateCustomerBalance 'xyz',0.03' at line 1",
Can anyone help shed some light on this? I've tried all sort and am now seeing tunnel vision.
Incidentally, this works:
await context.Database.ExecuteSqlCommandAsync("UPDATE Customers SET Balance = Balance - " + cost + " WHERE CustomerId = '" + customerId + "'");
All help appreciated.
|
|
|
|
|
|
Richard,
Worked a treat, thank you.
I've trawled the sites looking for a solution but didn't come across the CALL keyword - so very frustrating.
On a side note, from a performance perspective, is there a better way of this being implemented? The solution could take a large throughput and I'm keen to understand any improvements.
Again, thanks for the tip.
JMY.
|
|
|
|
|
How can I convert this query to a view
The query work but i get error message if I try to save as a view - the errors talk about duplicate names ?
SELECT DISTINCT
b.event_id AS a,
b.user_ID AS a,
b.date AS a,
b.Time AS a
FROM quadrantids2017 AS a
JOIN quadrantids2017 AS b
WHERE a.`date` = b.`date`
AND a.user_ID != b.user_ID
AND time_to_sec(a.`Time`) - time_to_sec(b.`Time`) BETWEEN -30 AND 30
Thanks in advance
John B
|
|
|
|
|
Every column in a view needs to have a unique name. You've given all four columns the alias "a", which is obviously not unique.
Just remove the four As a alias from the SELECT part of the query, and you should be able to create a view.
SELECT DISTINCT
b.event_id,
b.user_ID,
b.date,
b.Time
FROM
quadrantids2017 AS a
JOIN quadrantids2017 AS b
WHERE
a.`date` = b.`date`
AND
a.user_ID != b.user_ID
AND
time_to_sec(a.`Time`) - time_to_sec(b.`Time`) BETWEEN -30 AND 30
Alternatively, give each column a unique alias.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
So this query now works really well
SELECT DISTINCT
b.event_id AS a,
b.user_ID AS a,
b.date AS a,
b.Time AS a
FROM quadrantids2017 AS a
JOIN quadrantids2017 AS b
WHERE a.`date` = b.`date`
AND a.user_ID != b.user_ID
AND time_to_sec(a.`Time`) - time_to_sec(b.`Time`) BETWEEN -30 AND 30
ORDER BY
a.date ASC,
a.Time ASC,
a.user_ID ASC
I have two issues
1 - Its slow, there are 3144 records and its taking 37 seconds to get the result which is 1519 records so I added an index to event,_id, user_id, date and time and its reduced to 24 secs
2 -
It returns the results like this
33501 1 2017-01-01 01:14:59
47829 3 2017-01-01 01:24:49
33503 1 2017-01-01 01:24:48
37787 2 2017-01-01 02:21:13
33504 1 2017-01-01 02:20:43
47837 3 2017-01-01 03:06:19
33505 1 2017-01-01 03:06:17
47838 3 2017-01-01 03:07:30
33506 1 2017-01-01 03:07:28
33507 1 2017-01-01 03:07:42
47840 3 2017-01-01 04:35:12
33510 1 2017-01-01 04:35:11
Im wondering how I might show the related results
ie
47829 3 2017-01-01 01:24:49
33503 1 2017-01-01 01:24:48
37787 2 2017-01-01 02:21:13
33504 1 2017-01-01 02:20:43
47837 3 2017-01-01 03:06:19
33505 1 2017-01-01 03:06:17
47838 3 2017-01-01 03:07:30
33506 1 2017-01-01 03:07:28
33507 1 2017-01-01 03:07:42
47840 3 2017-01-01 04:35:12
33510 1 2017-01-01 04:35:11
Is there any way I can add a group number to each record or something like that ?
John B
|
|
|
|
|
I consider myself a novice with databases. I want to learn more about MySql and would like your recommendations for a good book and other resources. I can find my way around a database and can use some features of Workbench. My most recent task was to create a second table to log changes to a single table in the database. After much searching, I have yet to find clear explanations as how to do this simple task. So, a book that contains examples like this would best suit my needs. The MySql documentation is way above me. So, one objective in learning is to be able to understand and apply the documentation. Thank you for your help.
|
|
|
|
|
|
So this query is driving me a little crazy
I have a table called quadrantids2017 which has a number of events submitted by different contributors, key fields are event_id which is unique and user_ID and datetime
what I want is a query that will bring back all events which ocoured on the same date within 30 seconds of each other but different contributors so here is some data
event ID, user_id, datetime
37775 2 2017-01-01 00:01:23
33500 1 2017-01-01 01:07:56
37776 2 2017-01-01 01:08:45
47827 3 2017-01-01 01:09:07
37777 2 2017-01-01 01:09:44
33501 1 2017-01-01 01:14:59
47828 3 2017-01-01 01:15:00
33502 1 2017-01-01 01:16:10
37778 2 2017-01-01 01:16:47
37779 2 2017-01-01 01:19:29
37780 2 2017-01-01 01:19:36
33503 1 2017-01-01 01:24:48
So i would want a result that looks like this
33500 1 2017-01-01 01:07:56
37776 2 2017-01-01 01:07:45
33501 1 2017-01-01 01:14:59
47828 3 2017-01-01 01:15:00
37780 2 2017-01-01 01:19:36
33503 1 2017-01-01 01:19:48
On issue I have is that my current attempts bring back matches from the same contributor
Help really appreciated
Regards
John B
|
|
|
|
|
Can you post your query and explain what records are returned?
|
|
|
|
|
ok my query is like this
SELECT
b.event_id AS a,
b.user_ID AS a,
b.date AS a,
b.Time AS a
FROM quadrantids2017 AS a
JOIN quadrantids2017 AS b
WHERE a.`date` = b.`date`
AND time_to_sec(a.`Time`) - time_to_sec(b.`Time`) BETWEEN -30 AND 30
Note in this version I am using a separate date and time field
here are some of the results I have spaced and marked what should have been returned
37775 2 2017-01-01 00:01:23
33500 1 2017-01-01 01:07:56
37776 2 2017-01-01 01:08:45
37776 2 2017-01-01 01:08:45 - correct
47827 3 2017-01-01 01:09:07 - correct
47827 3 2017-01-01 01:09:07 - correct
37777 2 2017-01-01 01:09:44 - correct
33501 1 2017-01-01 01:14:59
33501 1 2017-01-01 01:14:59
47828 3 2017-01-01 01:15:00
47828 3 2017-01-01 01:15:00
33502 1 2017-01-01 01:16:10
John B
|
|
|
|
|
Looks like you're missing:
AND a.event_id != b.event_id from your WHERE clause.
(You might want to change that to an ON clause for the JOIN .)
If you want to exclude matches from the same user, you'll also need:
AND a.user_ID != b.user_ID
Or, to exclude matches for different users:
AND a.user_ID = b.user_ID
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
so this works fine
SELECT
b.event_id AS a,
b.user_ID AS a,
b.date AS a,
b.Time AS a
FROM quadrantids2017 AS a
JOIN quadrantids2017 AS b
WHERE a.`date` = b.`date`
AND a.event_id != b.event_id
AND a.user_ID != b.user_ID
AND time_to_sec(a.`Time`) - time_to_sec(b.`Time`) BETWEEN -30 AND 30
my only query now is the length of time it took the record set its self has 3133 records and the query returned 2990 records but it took some 40 seconds to do ? - I understand its actually doing quite a lot
John B
|
|
|
|
|
spoke to soon, there are duplicates
37919 2 2017-01-01 11:43:02
37920 2 2017-01-01 11:43:23
47890 3 2017-01-01 11:42:56
47890 3 2017-01-01 11:42:56
47894 3 2017-01-01 12:13:31
33558 1 2017-01-01 12:13:29
47896 3 2017-01-01 13:24:44
37937 2 2017-01-01 13:24:17
37945 2 2017-01-01 14:09:53
37945 2 2017-01-01 14:09:53
sorry
|
|
|
|
|
I want to make sure I get the basic design correct from the beginning so help really appreciated.
Im creating a dbase (MySql) that will store Radio Meteor Events.
Individuals collate data on a monthly basis and a contributor may collect anywhere between 4,000 to 8,000 unique events per month, we currently have
6 contributors out of a possible 30 or so.
So planning ahead if all 30 contributed each month we could have around 250,00 event per month
The key purpose is to correlate event capture, so did anyone else capture the same event as me
Each event per contributor is unique and amongst other things each event records a full date yyyy/mm/dd and time HH:MM:SS
Once all this data is in a database there would be quite general queries like how many events on a particular date across all contributors say in the month of Jan how many events across all contributors match with regard to both date and time
Another common slightly more complex query would be to get all events for a particular date where the time matches with a tolerance of say 10 seconds either way
so if we had
Contributor 1 15:00:10
Contributor 1 15:00:30
Contributor 2 15:00:05
Contributor 2 15:01:10
Contributor 3 15:06:10
Contributor 3 15:00:10
It would return
Contributor 1 15:00:10
Contributor 2 15:00:05
Contributor 3 15:00:10
so i was planning two tables
1 - Contributors
2 - Events
Event ID would be unique and the primary key
the link between the two tables would be the Contributor ID
I’m not sure about date and time
I thought for flexibility I could have
Date
Time
Datetime
The data its self is quite simple but given this could grow I want to store it in the most appropriate way to ensure that queries are efficient
Help appreciated
Regards
John Berman
|
|
|
|
|
It is a very simple structure and the relationship between the table is correct.
There would be no benefit to storing the date and time in separate fields.
You may, in the future, want to look at creating aggregated summary tables to facilitate reporting but not for a year or more (assuming 250k per month).
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
I'm surprised, that the COLLATION used in a DB Create Statement does have influence in executing SQL statements. Following I testet:
A. Insensitive: Creating a DB explicitely "Case Insensitive"
CREATE DATABASE CIDB COLLATE Latin1_General_CI_AI;
GO
USE CIDB;
CREATE TABLE TEST (ID INTEGER);
SELECT * FROM TEST;
select * FROM TEST;
SELECT * FROM Test;
select * FROM Test;
:) --> Everything works like expected, MSSQL does not bother about "TEST" or "Test".
B. Sensitive: Creating a DB explicitely "Case Sensitive"
CREATE DATABASE CSDB COLLATE Latin1_General_CS_AI;
GO
USE CSDB;
CREATE TABLE TEST (ID INTEGER);
SELECT * FROM TEST;
Everyting up to here is working fine. But ...
SELECT * FROM Test;
??--> fails with error message "Msg 208, Level 16, State 1, Line 1 Invalid object name 'Test'."
... obviously because the database is created "Sensitive".
What's really surprising for me, that the collation sequence now also
makes the SQL Language a case sensitive, not the language it self but
the objects (table-, field- names and I assume also constraint names).
Am I the only one who is surprised by this?
Thank you in advance for some high lighting comments.
modified 19-Jan-21 21:04pm.
|
|
|
|
|
It might seem odd at first glance, but it makes sense when you consider the fact that the names of all the objects within the database are stored in system tables within the database itself.
If the default collation for the database is case-sensitive, then the collation for all of the system tables is case-sensitive. So SELECT * FROM sys.tables WHERE name = 'Test' isn't going to match a row where name = 'TEST' .
Whilst Microsoft could have chosen to make the system tables always case-insensitive, that would have been a breaking change. Earlier versions of SQL Server didn't allow you to mix collations within a single database, so introducing that change in a later version could have broken existing databases or scripts.
There are effectively two "solutions":
- Always create your databases using a case-insensitive collation, and use a case-sensitive collation on individual columns;
- Always write your scripts using the case of the table and columns names as defined when they were created;
SQL Server Case Sensitive Collations and DMVs[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Wow, great! Thank you for your speedy Response.
I think I will go with "1.". Little bit more work, but from my Point of view the most transparent way.
Thank you again.
Bruno
modified 19-Jan-21 21:04pm.
|
|
|
|
|
Quote: that would have been a breaking change
After thinking again and again about it (I'm an old man and need therefor more time), is it not MS who is breaking the SQL Standard with this?
Not a big thing, only a thought.
Finally, it is like it is (with MSQL) and I have to live with it.
modified 19-Jan-21 21:04pm.
|
|
|
|
|
0x01AA wrote: is it not MS who is breaking the SQL Standard with this?
Yes. It looks like SQL92 says that object names should be case-insensitive unless they are quoted.
I guess they decided the dangers of making the change outweighed the inconvenience of not following the standards.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks again to give me some confidence on this
modified 19-Jan-21 21:04pm.
|
|
|
|