java sqlite fetching entire table content - java

I have a SQLite database. Now I try to create a function, that fetches the entire table content and returns it as an Array or ArrayList or whatever.
How do I do that the best way?
Can I directly convert the cursor into an array of strings and add that to an arraylist? If so, how?
Should I create a map for each row the cursor traverses through, and add the strings into that, with their keys? I tried that, but it didn't work.
I haven't programmed anything in Java for many years.
Thanks for any help.

Related

Dynamic For Loop

So I've found questions similar to this one, but none that have helped me with my problem. So I have an ArrayList< ArrayList < String > >. This basically creates a table of user inputs, so you can add columns and each column can have different amounts within them. I need to cycle through the combinations that can be created without comparing objects in the same column. Ideally I could send it through a nested for loop and access each element using an if statement to separate as needed, but since it is a dynamic size I haven't been able to find a way to do this that doesn't compare within the same column as well. Thank you in advance for your help.
If I'm understanding your problem correctly, it sounds like you have a List of Lists, where the first List is kind of like a key, where each slot is a list of the data you need. I ran into a very similar problem, and I was able to use a Map to hold the values. If order matters, then you'll want to use a TreeMap.
I mention the Maps, because you mention you want to manipulate (what sounds like the rows in a table), rather than the columns. If you use a TreeMap, then the keys stay in the same order, and the value for each key will be like the rows in the table. Then, the index in each List would be the column.
Without a solid example of your data, I'm not able to really go into how to compare the "combinations", which I assume can be handled by the Lists in the values of the Map, in this situation.

Trying to properly Save/Load an Array via MySQL

So I have an array of PlayerNames that are in a specific 'clan' that I need to save, so when I load the server it will loop through all of the entries.
Here is what I currently have
MySQL Table
Not sure what I would put for the 'members' basically what I want is to store UUID's of an array I have. It can come out as a string but I just need them to be able to store like
members: uuid, uuid, uuid, uuid
I understand how to build the connection, ResultSet, and the Statement part, I just don't know how to make MySQL know that I am trying to save these list of members as an array. Any help would be appreciated, I apologize if I did something wrong.
The problem here is in your data structure. It's not Normalized where as RDBMS are designed to store normalized data. All Create/Update/Delete/Retrieve operations become a lot easier if the data is normalized.
To normalize your tables, you need to stop storing member ids in the same column as CSV.
The Gang table should
uuid
title
kills
I assume you already have a members table and it looks something like
uuid
member_name
Then you need a new table called gang_members
gang_id
member_id
And create a unique index on those two columns.
Also see https://stackoverflow.com/a/41215681/267540 , Is storing a delimited list in a database column really that bad? and https://stackoverflow.com/a/41305027/267540

Can we store records got from a query to a list in stored procedure?

I am new to stored procedure, I am using hibernate concept to retrieve data from the database. client server traffic is more so I decide to move to SP by doing simple logics in server side and return needed values to front end. Now I want to know that is there any way to store records to list, so that I can rotate the list of records in a loop and ask them to come one by one and get a single field from a record and make a process then return a value to front end like we are doing in Java? List,getter,setter and generic class to store needed entities. I am confused with this.Please advise and guide me to know well about stored procedures.
It sounds like you are wanting to use a cursor over your query results, generate a temporary table, and then select the contents of that temporary table to return from your stored procedure.
You should be able to find plenty of examples online for cursors and temporary tables.

How to simply pull a table from mysql and then put into a java 2d array, then have a result and reverse?

I know a way but it should be not a good simple solution. Any very easy solution? I will do a lot of calculation then push back a 2d array result into a mysql tablea as well.
I'd wonder why you'd do this. It seems easier to let the relational database do all that work. I'd write ORDER BY X DESC and get it in reversed order without having to do a lick of work on the middle tier.
Can you refrain from bringing all that data to the middle tier and perform all those operations on the database using a stored procedure? Could be far more efficient.
But if you must, break it up into pieces:
Do a SELECT from the database to get a ResultSet.
Map the ResultSet into a 2D array (why not a Map or List or collection of some object?)
Get your "result" - what's that?
Pass the result to the database and do an INSERT or UPDATE as appropriate.

Retrieving data from ArrayList which contains database rows

I have retrived some datas from DB and I have stored it in an ArrayList. The ArrayList contains some 50 rows returned each row containin 4 columns. How do I access a particular column of a particular object in ArrayList? Can someone help me with this?
Not sure what is the exact issue here. List is based on the index and hence you can access any data based on index. Another option is to convert use Map which allows you to refer to the data based on a key you desire.
Due to new data posted in comments on the question, this is now know to not be what OP wants. I'd delete it but, given what I've read from him/her so far, I'm afraid he/she may be forever flummoxed by the disappearance of an answer.
ORIGINAL ANSWER
If you really have an ArrayList and not a ResultSet then do this
myList.get( desiredRow*column_width /*4*/ + desiredCol);
This assumes row-major ordering.

Categories