My goal with this project is to create a quiz app that has the ability to quiz from different banks of data. I would like the user to have the option to select which subjects they wish to be tested on, and then once selected the different banks can be combined and randomized for testing purposes. This is my first time utilizing databases and I was wondering if I could get a little input on the most economic way for this to happen. Are multiple tables my best option? Later down the road I hope to implement the ability to keep records of how well was done on each subject. If someone could point me in the right direction or give me suggestions for this I would be very appreciative.
I literally just finished creating my quiz app and I created one table to manage the questions and another to manage the scores. As long as you use a category column, you can set the cursor to read through questions with the categories you've selected. The cursor can return how many questions are in the categories selected, and you can also quite easily shuffle the numbers to get a random assortment of questions. If you've got any specific questions, just ask
(As for the how well people have done, I suggest putting that in another different database. It's easier as that database is probably never going to be upgraded, you don't have to modify it once it is created).
Related
I am making an application for a campground that has a list of camping sites, each of which has a list of reservations. There can be any number of reservations for each camping site and any number of camping sites.
I started off by just storing this data in a csv and using that to handle everything. This is working really well because after a few columns of identifying information for each site, I just have an arbitrary number of reservations that I can go through. It's also really easy for me to add a new site if I want to add that functionality to my application since I can just add a new line and leave the reservation part blank if there aren't any reservations.
I'm thinking that I should instead use a database to store this information for the following reasons:
I need to be able to search and modify specific sites in addition to just iterating through them in order. With the CSV solution, iterating through them in order has been great when I need to use them all but I don't want to have to iterate through to find a specific reservation. Plus modifying the csv isn't looking to be that great either.
I am hoping to gain a better understanding of databases in general and I don't believe using a csv is the most professional way to do this, nor would it be the best practice.
How exactly could I do this? If, for example, I had only one camping site then I would just add another table that represents the reservations as an entry to the table but since I have at least 100 camping sites and can have an arbitrary amount, this doesn't seem to make sense. Am I just not understanding something or am I correct in coming to that conclusion?
Some more information:
Since I haven't said it yet, this is a java application with an apache derby database.
This project is not for a class or a job, I'm just trying to practice my programming skills and help someone out who runs a campground at the same time.
There is an application that I'm basically writing with Swing, JDBC and MySQL.
In DB there are tables like Article, Company, Order, Transaction, Client etc.
So also there are java classes which describes them.
User can create, update, delete information about them.
I give an example of my problem. The article characterizes with id, name, price, company, unit. And when user wants to save new article he chooses the company for this article from the list of all companies. This list in perspective could be really big.
Now I could think of two ways to solve this.
When application starts, it connects to the DB and load all the data with which then I will work.
public final class AllInformationController {
public static final Collection<Company> COMPANIES= new HashSet<>(1_000_000);
public static final Collection<Article> ARTICLES= new HashSet<>(1_000_000);
public static final Collection<Order> ORDERS= new HashSet<>(1_000_000);
public static final Collection<Transaction> transactionsHistory= new HashSet<>(10_000_000);
//etc...
private AllInformationController() {}
}
Then if user wants for example to change some Company data (like address or telephone etc.), after doing it the program should update the DB info for that company.
The second approach is basically to connect to the database every time user queries or changes some information. So then I will mostly work with ResultSet's.
I prefer the second way, but just not sure if it's the best one. Think there should be more productive ways to work with data that could be less expensive.
The 2nd approach is better, although there's probably a best case that lies somewhere between them. The 2nd approach here allows multiple applications (or users of the same application) to modify the data at the same time, as the 1st approach may end up using old data if you load all the data at once (especially if a user leaves the application on a while). I would go with the 2nd approach and then figure out what optimizations to make.
Since you think the 1st approach may be usable, I'd assume then you don't have too many users who would use the tool at the same time. If that is the case then, perhaps then you don't need to use any optimizations that the 1st method itself would give you as there's not going to be too much database usage.
When you say you working with ResultSets more often in the 2nd approach than the 1st, well it doesn't need to be that way. You can use the same methods from the 1st approach which translates your data into Java data structures to be used in the 2nd approach.
You already made a very bad decision here:
And when user wants to save new article he chooses the company for this article
from the _list_ of all companies
A list works only reasonably if the number of choices is fairly limited; below 10-20 you may get away with a combo box. For thousands of choices a list is very cumbersome, and the further it grows the slower and more unwieldly chosing from a list becomes.
This is typically solved by some kind of search field (e.g. user types customer number, presses tab and information is fetched), possibly combined with a search dialog (with more search options and a way to select a result found as "it").
Since you will typically be selecting only a few items with a search request, directly quering the DB is usually practical. For a search dialog you may need to artificially limit the number of results (using specific SQL clauses for paging).
I'm using Google App Engine and Objectify 3.1 and slowly learning about denormalization and designing entities based on their usage, but I'm still struggling with some aspects.
I'm currently building a system in which a User entity can participate in a Game entity and need to be able to find all games a user participates in. The way I see it, there's two basic solutions:
Solution 1)
Store a list of user keys (participantKeys) on the game and find the games a member participates in like this:
List<Key<User>> userList = new ArrayList<Key<User>>();
userList.add(new Key<User>(User.class, myUserId));
Collection<Game> games = ofy().query(Game.class).filter("participantKeys in" , userList).list();
Solution 2)
In addition to storing a participant list on the game entity, also store a list of games the user has participated in on the user entity and find the games like this:
User myUser = userDao.getUser(myUserId);
Collection<Game> games = user.getParticipatedGameKeys();
Solution 1 would become pretty slow once there's a lot of games in the system.
Solution 2 would make finding the games faster, but I'll need to constantly keep the list updated as users join and leave games.
The list of games would also become large once a user has been using the system for a long time. I only want to return all games the user is currently participating in, so that would require traversing the list and excluding "historical" games.
Am I missing a more elegant solution? Neither of the above seem very attractive.
Any and all help is greatly appreciated!
Edit:
I decided to try something like Mikl suggested after thinking about alternatives for a long time.. so it's good to hear a solution that's very pretty much exactly like it :-)
I have created a GameParticipation entity which contains a link to the game, a link to the user and all other information that I need to be able to get.
Every time a game is joined, I update the GameParticipation entity to reflect the current state of the game. When a game is left, I delete the entity. Also, when a game is changed, I update all related GameParticipation entities.
I've done a little performance testing and it seems to work reasonably well!
you can also have an another entity I don't know how you can call it (UserGame?) , but in this entity you will store the game key, the user key and also some information you want to access to, for instance the user name, the game name and so on. Then when the user enter a game you will create this entity.
With this entity you can easily retrieve the games a member participates in and also all the users that partipate to a game.
The inconvenient with this method is that, if a user property or a game property change you need to update also the information you stored in the USerGame entity like the userName.
I don't know if it's a good solution but it should work.
So I need to create a web service which will communicate with my Android
application. In my android app the client choose two point start and
arrival this two point will be send to my web service to find the bus
that has the shortest path between them. I have a problem with the web
service side.
I tried to use Dijkstra's algorithm to find the shortest path between
two points. To test the Dijkstra algorithm I must extract data from a
MySQL database and not put it right into my algorithm. I don't know
how can I do it though.
In my database I have two table that contains the bus route (bus num),
code (station id), pt_arret (station name). There's another table which
contains location code (id station), latitude and longitude, and
distance (is the distance between a station and the station which
precedes.
You've got to create a structure that will let you use Dijkstra's algorithm. To do that, you must read all the relavant data from the database. The transition from relational data to object oriented is always awkward.
Ideally you want to use a single, simple SQL select per table to get the data. Optimization is tricky. A single select statement can grab a million rows almost as fast as it can grab one row; one select will get a million rows faster than 10 selects will grab 10 rows (in my experience). But grabbing too many uneeded rows might take too long if your DB connection is slow (has a narrow bandwidth).
Use Maps (TreeMap or HashMap) to keep track of what you read, so you can find "station" objects that have already been read and placed in your structure and add connections to them.
Once you have your data structure set up in memory, try to keep it around as long as possible to limit delays from rereading the database.
Keep an eye on your memory and timings. You are in danger of running too slow for your users or running low on memory. You need to pay attention to performance (which does not seem to be a common need, these days). I've made some suggestions, but I can't really know what will happen with your hardware and data. (For instance, reading the DB may not be as slow as I suspect.)
Hope this helps. If you have not done anything like this before, you've got a lot of work and learning ahead of you. I worked on a major program like this (but it also wrote to the DB), and I felt like I was swimming upstream all the way.
Addition:
What you want in memory is a set of stations (Station class objects) and routes (Route class objects). Each station would have all the data you need to describe one stations including locations. Critically, it would also need an ID. The stations should go in a TreeMap using the ID as key. (This is my predjudice, many people would use a HashMap.)
Each route will have references to the two stations it links, a distance or travel time, and any other needed information.
Each station will also contain a list of routes that reference it. I'd recommend a LinkedList for flexibility. (In this case, ArrayList is apt to waste a lot of space with unused array elements.) You will want to read the stations from the db, then read route info. As you read each route's info, create the Route object, locate the two stations, add references to them to the Route, then add the Route to both stations' route lists.
Now for each station you can spot all the routes leaving it, and then spot all the stations you can get to with one bus trip. From those stations, you can work your way on, all through your network. This structure really is a "sparse array", if you want to think of it that way.
Applying Dijkstra's algorithm--or any other algorithm--is quite straightforward. You'll want various flags on the stations and routes (fields in the Station and Route classes) to track which nodes (stations) and connections (routes) you've already used for various purposes. It might help to draw the map (start with a small one!) on a sheet of paper to track what your code is doing. My experience has been that it takes very little code to do all this, but it takes a lot of careful thought.
I am developing under Java, Ejb3.0, WebLogic.
I would like to have a system design suggestain from you about feature which I am going to develop. (not too complicated)
The main goal is to have a system which takes information from couple of databases and sync between them.
for example:
let's say I have database A, Database B and Database C.
if we compare B against C: ( B is master DB)
desired target:
scenario 1. A has a record which is missing in B. action we take = B add to its table the missing record.
scenario 2. A has a record and B has also that record. action we take = B is updating the record information exactly as it shown in A.
(The same goes with Database A against Database B).
The compare method suppose to compare between specific table columns information.
Now I could take everything and drop it to objects and then compare.
Other hand I can do manually sync.
Would like to hear some design advice (could be OOP design or any other pattern). Even if it's a bit overhead for some special design. I still Would like to do it so I can learn something new, and also use this mechanism to sync other systems.
Thanks in advance,
ray.
A good answer on this does depend on the amount of data.
If the amount is little, just get all objects from all databases and put it within a collection. Thats the most easy to maintain.
With a minor load of data coming from one database and major load of data from another, maybe its a good idead to take the minor data, pass it to the database with the major data and let the database do the work.
Mostly best practice is to keep the dataflow between your application and the database low.
Maybe you can explain details about your questions a bit more...
--- edit ---
Ok, so you want sync all to your B Master DB.
There are several approaches, depending on several environment parameters you have, the two main directions would be
Make a full iteration every time (easy to program and maintain, very bad performance)
Make a full sync once and delta updates after that (harder up to very hard to maintain, very good performance)
To 1.)
If all items from a table fit into your main memory without problem, take all of them into there, and do your stuff there.
If not you have to do it bunch for bunch.
To 2.)
a)
To allow to make deltas you have to identify changed items.
For that you can use DB triggers, but this is very DB specific and very hard to maintain,
or
b)
you can introduce table columns which have version numbers only for your sync purpose, which you count up, if a entity is done.
The version number could be introduced with Frameworks like Hibernate more easily, but still you have a bigger code base to do your things, since you have to check the version, etc.
But the much better performance will make delta updates the most commonly used approach.
This just sounds like data replication, which is best handled by the database itself. Consult the documentation for your database technology, there should be a multitude of different ways to configure replication. Don't re-invent the wheel.