简体   繁体   English

插入,替换/更新JDBC

[英]Insert, Replace/Update JDBC

I want to have a database where the votes of all users are stored. 我想要一个存储所有用户投票的数据库。 As an example, let's say user ABC chooses X on question Y. Then the same user ABC decides to vote on a separate question, and chooses V on question Z. The information is added to the database each time the user is pressing the submit button. 例如,假设用户ABC在问题Y上选择X。然后,同一用户ABC决定对单独的问题进行投票,然后在问题Z上选择V。每当用户按下“提交”按钮时,信息就会添加到数据库中。 Let's say that after a few days, the same user ABC decides to change one of its votes, so he goes back to question Y but chooses W this time, instead of X. A new entry should be avoided, and instead, the old one should be updated with the new choice. 假设几天后,同一位用户ABC决定更改其投票之一,因此他回到问题Y,但是这次选择W而不是X。应避免输入新的条目,而应使用旧的条目应该使用新的选择进行更新。

My Votes table holds all the votes and users and all the choices: 我的投票表保存所有投票和用户以及所有选择:

CREATE TABLE Votes(
  id integer,
  username varchar(16),
  option varchar(16)
)

I've tried different methods each without success. 我尝试过不同的方法,每种方法均未成功。

Before you start implementing these types of database connections its always good practice of thinking of a diagram to help you underatand how to organize your database tables. 在开始实现这些类型的数据库连接之前,它总是一种好的方法,即考虑图表以帮助您了解和组织数据库表。 The relational modle is a great and simple way to start. 关系模型是一个很好且简单的开始方式。 You have to see what type of entities you have, in this case it is user and question. 您必须查看所拥有实体的类型,在这种情况下,这是用户和问题。 These entities will each represent a table. 这些实体将各自代表一个表。 A user votes on a question and since many users can vote on each question and each user can vote on many questions the relationship between those entities "vote" also has to have a table. 用户对一个问题进行投票,并且由于许多用户可以对每个问题进行投票,而每个用户可以对许多问题进行投票,因此这些实体“投票”之间的关系也必须具有一个表格。 Ao we have 3 tables. we,我们有3张桌子。 The user table should have its id and user data you think is apropriate with id being its primary key. 用户表应具有其ID和您认为合适的用户数据,并且ID是其主键。 The question table ahould have its id and user data you believe to be apropriate with its id as primary key. 问题表应该具有其ID和您认为合适的用户数据,并将其ID作为主键。 And on the vote table you should have the id of user and the id of the question it was voted on with any other data you wich to have there. 在表决表上,您应具有用户ID和已被表决的问题的ID,以及您希望在其中拥有的任何其他数据。 The user id and question id will form a comppsed primary key here. 用户ID和问题ID将在此处构成一个重要的主键。 This way by acessing the vote table you will be able to know wich user voted on each question and, wich question was voted by wich user, etc... 这样,通过访问投票表,您将能够知道哪个用户对每个问题都进行了投票,并且哪个问题是由哪个用户进行了投票等。

I think you should add one more field to table for question. 我认为您应该在表中再添加一个字段以供提问。

When you do this in your app/database you should check if this user have a row with question and answer, if yes then update table or insert into if the row doesnt exits. 当您在应用程序/数据库中执行此操作时,应检查该用户是否有包含问题和答案的行,如果是,则更新表,或者如果行不退出则插入表。

Change your table as below: 如下更改表格:
CREATE TABLE votes ( id bigint(20) NOT NULL AUTO_INCREMENT, user_id bigint(20) DEFAULT NULL, question_id bigint(20) DEFAULT NULL, option varchar(255) DEFAULT NULL, PRIMARY KEY (id), UNIQUE KEY user_id (user_id,question_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8

Before you insert an new record, you should check if the user has answered this question by 在插入新记录之前,应检查用户是否已通过以下方式回答了此问题:
select * from votes where user_id = ? and question_id = ? limit 1

If there is an record exists, update it, or insert an new record; 如果存在记录,请对其进行更新或插入新记录;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM