how to move json datas to mysql
I have few datas which stored in user_activity.json I need to move them to the mysql and I need to use them in my controller. In screenshots you can see the type of data and also I created a model like this.
249 Replies
is this a one-time thing or on an interval or triggered via an event?
Are you using EF Core for MySQL, or just straight ADO.NET?
Regardless, most of the code will be similar. Deserialize the JSON, your models seem to be for that already, then map each event to the corresponding datatype for your MySQL table, and insert it
the insert step might look a bit different depending on how you talk to the database
one time thing I think I have the data i'll import to the mysql and for example I'll use it for getting custom methods like user1's total activity of 1 month, how many logins logouts username changes.
I'll use ado.net
then make a console app, that just does this in 3 steps
step 1: deserialize the json
step 2: map from json models to database models, if they differ at all (likely)
step 3: insert to DB
but I need to do it as api
no
it makes no sense as an API, its a one time thing
first I'll move everything to db then created my model and it'll goes like service and controller
isn't like this?
I won't add anything but i need to use these datas with my program
the database is a separate thing
you know this right?
its has a life of its own
you can connect to the same database from multiple programs
so you can have an API for accessing this data, and the console app for reading the JSON and inserting it
oh you say console for just moving datas to db
yes.
so my program doesn't need to contain these kinda codes
got you now
yeah, its not part of the API. just uses the same data
so I'll follow this steps
π
@Pobiega how does it look?
looks okay. A bit too OOP for my tastes π
I'd personally have separates the concerns a bit, so that reading and writing were two distinct steps, but it does the exact same thing
anyway it doesn't work
you. uhh..
I dont think it handles enums for you
so you gotta manually cast that enum to whatever format you want. int/string
I even tried like this
cast the enum
(int)evt.Action
still same i guess
are there mistakes here?
what do you think about this?
it's working in this way
but I think I made mistake while mapping
cuz I have 10 users and I want to collect their activities but there are so many Id's
should I a column as UserId and keep the activity id still or change the all Id's as user Id
ah, seems as if your database is treating enums as strings?
yes
but as you see I couldn't manage to classify activites by users
there are so many usernames but all of them belongs to 10 user so I need to add "Id" as userId right
I dont know what data you have or not have mate
thats on you
Stop normalizing ISO timestamps. :SCgetoutofmyhead:
can you check why I'm getting null return
?
$details
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
and please use
Enum.Parse<T>
I added the whole values to db and tried to parse here my point is to get reports of last 30 days
instead of should I use this?
here is one example from db.
no, you should use the generic overload of Parse
Parse<Action>
can you give me an example please?
nah
gotta do some work on your own
so my mistake is this?
I don't know
you have not explained your problem
you just said something about a null
my problem is: I added the whole values to db and tried to parse here my point is to get reports of last 30 days but from controller when I send the userId it returns null.
null? or an empty list?
I can't see a way this code ever returns null
sorry empty list. I think the problem is my method can't read the userId that I sent
what do you think
yeah, because you never add it as a parameter
also
yeah
this is fucked
also
jesus
why are you using ADO
im so lost
do u understand what the numbers stand for in there right?
they are 0 based index representation of your columns in the SELECT
so 0 is an Id, if its int it should be read as int
don't ask me it's not optional man
GetString(0) reading an int?
I gave the numbers based on here.
I saw an usage like this and it works
wdym
what.. what are you doing
why is there both a userid and an id?
oh wait, okay one is the PKey
but you're reading Username as string on index 0
which is Id
jesus, put the PKey on top for the love of god
id is action's id userId is users id
and you are not selecting a
userid
you are selecting Id and Usernamethen name things properly
ActionId
UserId
anyway I dont want to overflow the conversation I will leave it to my boss Pobi π
nah feel free to take over, I should be working
π
I dont understand how that code is not throwing thou
is that code wrapped in a try and catch somewhere?
where u call the method
nope
it's the whole section
well can u $paste it?
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
I cannot copy paste images
BlazeBin - vdlugdbhxtze
A tool for sharing your source code with the world!
also Event and UserActivity
BlazeBin - sgbfuwedkuwo
A tool for sharing your source code with the world!
that much you understand right?
yeha
yeah
so you can see your getting the wrong index for Username and Timestamp and details?
yes I see now
ok
what is column type for Action in your database?
int?
string?
enum
btw working rn
after fixing indexes
this is mysql? sql?
mysql
ok
so another issue is
you're creating 1 useractivity and 1 event per row
you're not creating 1 useractivity and a list of events
is this wrong way
so you have to initialize the useractivity outside the loop
and fill it in
yes because of what I just said
you're reading row by row and useractivity is inside the loop
so what happens when u create the object new every loop?
but in db it stored like this
this is essentially what you're doing
every time the loop runs
do u see why u have many useractivities instead of just 1
you need this
oh you say you have one user activity and lots of events right?
no u have lots of both
yeah because it stores randomized datas for 1 year
no
randomized number of logins logouts
because u create a new user every row
in your loop
leowest
REPL Result: Success
Console Output
Quoted by
<@1102729783969861782> from #bot-spam (click here)
Compile: 575.945ms | Execution: 76.195ms | React with β to remove this embed.
so u see how it prints 1 => 2 for 5 times in different lines?
that means you have 5 different instances of Activity user each with 1 event
and not 1 Activity with a list of 5 events
because every time u create a new user inside the loop
leowest
Quoted by
<@1102729783969861782> from #how to move json datas to mysql (click here)
React with β to remove this embed.
Oh, now I understand what are you mean
thank you. It's a bit confusing
well just ask where u dont understand
no I mean the general of ado
I mean that is not ado related
its pure c#
you're instantiating an object inside the loop
so it will be a new object every iteration
there are times u may want that, but not right now u due to how u want to fill your 2 classes
I need to instantiate a object out of loop and in the loop I'll take every data and insert it to my object
am I right
just like that
once u write the changes post the code here, preferable as code not image pls π and I will look at it again
how do I update useractivity info?
inside the loop
so I instantiate new useractivity out of loop but couldn't find how to update inside in it
yes just the class
then use inside the loop user.Id = ..., user.UserId = ...
also initialize user.Events = new List<Event>() outside
BlazeBin - epkelpilxdpm
A tool for sharing your source code with the world!
the code in the link is old
BlazeBin - pxwmdgqylmpt
A tool for sharing your source code with the world!
looks good π
is that the desired json?
also u dont need to do this
reader["Action"].ToString()
u can just do reader.GetString(4)
fairly sure the enums are read as strings in mysql.datathank you for your help really
yes that was what I want
no worries that's what we are here for
learn and help π
I converted it to the example: user2 logged in 15 times logged out 20 times
now I gotta do it same thing for all users at the same time
but I learned how to do itπ
and after that I need to learn how to convert it to json and store it in json file
backwards I mean
deserializing is the same thing
if u serialized List<UserActivity>
then u deserialize that
i deserialized it so i gotta serialize
with same steps?
u serialized it
serialize is when u generate the json
deserialize is when u read back into the object
oh I thought I deserialized it for json to db
nah
quite opposite then
althou there are database u can insert json directly into a column
and they have a feature that lets u json search the column
so its not just text inside a column
I see, when time comes for serialize I'll deeply search
thank you
$close
Use the /close command to mark a forum thread as answered
I'll come to end of my school can I ask you some things about what should I do after school?
is it propriate ask you from dm
I dont take dms sorry, you better ask on #career-talk
or u could also open a new #help with the new subject im sure people would be whiling to share what they can
What level of school are we talking?
I wouldn't quite say you're job ready just yet and need a bit more practice. Best way to get that is to just make stuff, or go some kind of apprenticeship if possible.
university
I'm thinking exactly same thing. I'm doing volunteerly internship right now. They giving me tasks and I'm trying to do it with learning as well.
and I think I need to practice more than bitπ
u need to also learn a bit more of problem solving your self
like the loop thing was something simple u could have figured your self π
problem solving is the most important skill to acquire
you're right. I'm looking for help every little problem. I should stop doing that
also learn to $debug
Tutorial: Debug C# code and inspect data - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
this is very very very important
if u see its not working the way u expect
breakpoint
step thru the code line by line
see where something undesired happens
think why
etc
so if u had debugged that loop
u would have seen than when useractivity is inserted to the list
what it looks like
u can hover the mouse
see the contents
etc
that't right I'll try to be more careful from now
and also I'll check the link
I'm going home rn today was tiring to meπ«
specially when you're writing complex things that can be a life saving or even when something breaks or throws
because u can evaluate the values and see
do u have 0 try / catch in your code?
I am still not convinced that your code with wrong indexes was not crashing your app
sometimes I can understand my mistakes but can't figure out what to do after it
my semi magical fingers π
and that's where thinking comes in hand, experimenting, if its something u haven't learned u won't figure out but if its something u did u will
there is absolute 0 try catch in your entire code?
nope
im amazed at how the library doesn't throw then,
garbage mysql
is thar make sense that doing everything in try catch or just when I expect errors?
yh you're right about that but sometimes I can forget things that I know
imo for learning u should never use try and catch
they can often hide the real problem
because they eat the exception that could have happened to tell u the issue
yea seeing the real problem is way better I think
there are cases u might need try catch or is forced to used it thou
like for example I was writing some maui and the throw error had literally no useful information
when I wrapped it in a try and catch the error was completely different
which leaded me to the answer so these are some special cases I guess
whats your job at software
i mean like backend front end
both except for web I dont yet use asp.net mainly php unhappily
I do make some internal apis in asp.net thou
are you working or student like me( i dont think ur student thou)
working
self employeed thou
and @Pobiega owns a bank in sweden
did you choose to be self?
kiddingπ€―
no, its just circustances of life
unhappily
but some things are working in their way right? that's something imo
hey @leowest can I ask you a question?
u just did
in here I'm trying to count total userId's but I couldn't manage to do it
it counting just 1 when I tried
it has to be 10
let me send my code like
BlazeBin - wzcngjmwcqza
A tool for sharing your source code with the world!
breakpoint on the foreach
check the content of usersActivities
see if the content is what u expect
my content is in userActivity
its like userActivity.userId
I tried to add this but counts 2
oh I understand why its 2 its not counting its just adding 1 to userId 1
u said u want to figure things out, so u debug
breakpoint and step in is the easiest way to see what your code is doing
with what data
and then u ask is this what I wanted, no? then what did u want that its not doing
so u can see X is increasing, Y is increasing Z is increasing but B is not
the worst part is that if the userId was 10 u would believe it was adding
π
no
userId is 1
total user number 10
I am saying IF it was
because u would had the ilusion it was ++
then why prints 2
total user number from where?
I dont know what useracitivities have
does it have 1 item with many events?
does it have many items with many events?
its like this I have 10 user and they have many events and many actions
im not referring to the class
im referring to what is the content
that is why u breakpoint
so u can see if the content is right
if its what u expect and if it would have the count u expect
oh wait
this doesn't make sense either
you're increasing the userId from useractivity
and not the report
maybe u need
where count is the actual userId count?
but since I dont know the content of useractivities at that moment I can't really tell
just for the record, this should be three queries to the database instead of fetching all the records and doing it in-app
databases are very good at their jobs. they count a lot faster than your app can do
its also a very good opportunity for him to use the debug thou
so you're saying I shouldn't make the count here just let the count db
I am. but leo has a point
learn to use the debugger, then fix it π
mainly why I didnt mention much ;x
I was waiting to see if he would figure out what is wrong and then crush his dreams
last part was a joke btw
figuring the let db does this job? or debugging
np you can have fun with me thou
you can do what your doing in code and with the db
I first wanted u to debug to see what your doing wrong in code
and then explain that u can also do it from db
which would effectively give u the List<UserActivityReport> as an output
or just UserActivityReport if its just 1 user
but u said u wanted to figure things out more often and solve problem so that issue sounded like the perfect example that u could solve your self
and debuging should tell u what is wrong or missing
but I'm doing the count in app because im using the data which comes from db is that matter
what I mean is im takin the whole data with repo from db and then im editing the data from service
is it wrong
forget about the db for now
just try to figure out why its not counting
okay let me try to fix that first
its not wrong in some scenarios u may need to count in code and it might be faster
btw do you know the solution
I have assumptions of what it could be, some of which I already pointed above
but without knowing the content it would be hard to say
ok ok I will solve it
promise
don't help me
:catlaugh:
godspeed
Just going back to what I already said:
- what is the content of userActivities
- why are you setting userActivityReport.userId = userActivity.userId; is that right?
- what is the total count u mentioned above is that suppose to be the userId in userActivityReport or is that supposed to be an extra property u didnt add to it
if you breakpoint and step in line by line u can see what is going on, what the code is doing with userActivities and what is missing
only you know
total user number 10
Quoted by
<@1102729783969861782> from #how to move json datas to mysql (click here)
React with β to remove this embed.
its the wrong function you know that right
ah its the one under?
yh
the one you sent is calculating for just 1 user with id
sure but you're not counting users
on the 2nd one
yeah im trying to deal with it rn
so basically you are missing a property
or is userId suppose to be the count?
because the naming would be awful
I know im aware
ok just clearing that up
I used same property in 2 different methods
just being lazy π«£
either way what I said above still stands the logic of what u need to debug
only thing is u not increasing users count on the 2nd method
I said im trying to increase it but couldn't figured out in which loop
as I understand second loop works till counting is done but I cant count my users there
because it's not an action
well first loop walk thru all users
second loop walks thru all events of a given user
given your sample json
that's right
okay let me try somethings again
userActivities.Count is the total users u have in useractivities
how do I use it
how do u want to use it?
that is literally a property
wdym how do I want
there is no where in here to save a total count
what is missing?
do u want to save it to userId?
temp variable?
ok and do what with it
im trying to understand what u want to do with it?
does it need to be in your report?
do u want to it for something inside the loops?
increase +1 in every userId count
but u already have the total
do u have repeated userId?
userActivityReport.userId = usersActivities.Count;
if that is what u want to do
I guess so
if u dont use count
count the first loop
rr u just ++ the userId in the first loop
my point is count every action also count user Id and I'll use it for report
would be the same outcome
its like 10 users 10 times logged in
yes,
if each user logged in once
nope its like 1 month of data
each user almost logged in more than 100
then that is my point ur content is not right
if its counting only 10 logged in times
I don't understand how it's wrong it's working fine
what's wrong thing about it
then why its not giving u 100?
what
why do I need 100 for_?
:catfacepalm:
wait
let me show somethign
0 is usercount and login, logout, usernamechanged
ok so you want to get the total amount of users registered?
I just want to count total userId's that containing in data
there is no register
data being useractivities?
yes
and useractivities does not have duplicated userId?
nope
so yes userActivities.Count is equal to usercount
now if its 1, 2, 10, 100 and that number is not right then there is something not right with how userAcitivities get its data
how do I use useractivities.count
do u understand that? U have List<UserActivities> with no repeated user id
List<> have a property called Count
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.count?view=net-8.0
I understand that
am I stupid?
no ()
and outside foreach
its not allowing me to use it without ()
what type is userActitivties
userActivities[]?
if so then .Length
$typehints
Visual Studio 2019 and above can now give you inline hints of the type in lambda, var, etc, see the animated GIF on how to enable it:
https://cdn.discordapp.com/attachments/169726586931773440/869498806343962694/2021-07-27_05-35-23.gif
See the below image for an example of what it looks like the white line was made by me to illustrate what the above does:
https://media.discordapp.net/attachments/569261465463160900/894398422365372416/unknown.png
NOTE: You can also use it by pressing ALT + F1! You might need to go on the settings and disable/reenable it for it to work!
if u hover your mouse over GetAllActivity it iwll tell u
but I looked at your code from before its IEnumerable
so in this case yes u would be doing userActivityReport.userId++; in the first loop
sorry
I am doing
it just prints 1
ok god why do u not want to breakpoint?
do u understand that if it count 1 u only have 1 entry?
and I've been saying from the begin to debug the content
I did
and did u hover the mouse over useractivities
yes ffs
ok and did u expand it to see how many items it had
did u see more than 1
what is item
I can't see the tree
tree?
screenshot too small
it doesn't show the outside
so I can't see the elements
u have 1 item
so its only right that your count will be 1
we spent all this time because u didn't want to read the debug link I sent u π
it shows all that
shows how to look at the element
shows how to step in
okay
can you stop
thank you for your help anyway