Discord Java JDA | 从SQLite删除数据 / 从SQLite获取数据

huangapple go评论66阅读模式
英文:

Discord Java JDA | deleting data from SQLite / getting data from SQLite

问题

public void onMessageReactionAdd(MessageReactionAddEvent event) {
    if (!event.getUser().isBot()) {
        if (event.getChannel().getIdLong() == 747412032281772033L && event.getReactionEmote().getEmoji().equals("\uD83C\uDFAB")) {
            ResultSet set = LiteSQL.onQuery("SELECT channelid FROM ticketchans WHERE guildid = " + event.getGuild().getIdLong() + " AND userid = " + event.getUserIdLong());

            try {
                if (!set.next()) {
                    Category cat = ((GuildChannel) event.getChannel()).getParent();
                    TextChannel chan = cat.createTextChannel(event.getMember().getEffectiveName() + "'s TicketChannel").complete();

                    EmbedBuilder builder = new EmbedBuilder();
                    builder.setDescription("Hi " + event.getMember().getAsMention() + ", bitte beschreibe hier detailiert dein Anliegen. Wenn du dein ticket schliessen willst klicke auf das X");
                    builder.setColor(Color.decode("#910cc9"));
                    chan.sendMessage(builder.build()).queue(message -> {
                        message.addReaction("\u274C").queue();
                    });
                    
                    LiteSQL.onUpdate("INSERT INTO ticketchans(guildid, channelid, userid) VALUES(" +
                            event.getGuild().getIdLong() + ", " + event.getChannel().getIdLong() + ", " + event.getUserIdLong() + ")");
                    
                    event.getChannel().sendMessage(event.getUser().getAsMention() + " TicketChannel eröffnet!").complete().delete().queueAfter(4, TimeUnit.SECONDS);
                }
            } catch (SQLException e) {
            }
        }

        if (event.getReactionEmote().getEmoji().equals("\u274C")) {
            //delete data in table
            event.getGuild().getGuildChannelById(event.getChannel().getIdLong()).delete().reason("").queue();
        }
    }
}
英文:

I'm trying to make a ticket-based support system and I would like to know how to read and delete data from a SQLite table.

The system will work like this:
You click on a reaction and the bot checks if you already have a dedicated channel, if not it will create one.
If you close the ticket by clicking on a reaction in your personal channel, the channel and your data will be deleted.

That's my code so far:

public void onMessageReactionAdd(MessageReactionAddEvent event) {
		
		if(!event.getUser().isBot()) {
			if(event.getChannel().getIdLong() == 747412032281772033l && event.getReactionEmote().getEmoji().equals("\uD83C\uDFAB")) {
			
				ResultSet set = LiteSQL.onQuery("SELECT channelid FROM ticketchans WHERE guildid = " + event.getGuild().getIdLong() + " AND userid = " + event.getUserIdLong());
				
				try {
					Long user = set.getLong("userid");
					if(!(user == event.getUserIdLong())){
						
						Category cat = ((GuildChannel) event.getChannel()).getParent();
						TextChannel chan = cat.createTextChannel(event.getMember().getEffectiveName() + "'s TicketChannel").complete();
						
						EmbedBuilder builder = new EmbedBuilder();
						builder.setDescription("Hi " + event.getMember().getAsMention() + ", bitte beschreibe hier detailiert dein Anliegen. Wenn du dein ticket schliessen willst klicke auf das X");
		                builder.setColor(Color.decode("#910cc9"));
		                chan.sendMessage(builder.build()).queue(Message -> {
		                	Message.addReaction("\u274C").queue();
		                });
		                set.next();
		                LiteSQL.onUpdate("INSERT INTO ticketchans(guildid, channelid, userid) VALUES(" +
								event.getGuild().getIdLong() + ", " + event.getChannel().getIdLong() + ", " + event.getUserIdLong() + ")");
						
		                event.getChannel().sendMessage(event.getUser().getAsMention() + " TicketChannel eröffnet!").complete().delete().queueAfter(4, TimeUnit.SECONDS);
						
					}
				}catch (SQLException e) {}
			}
			
			if(event.getReactionEmote().getEmoji().equals("\u274C")) {
				
//delete data in table			event.getGuild().getGuildChannelById(event.getChannel().getIdLong()).delete().reason("").queue();
    
			}
}
}

答案1

得分: 1

从SQLite获取数据

大部分内容适用于SQL总体,不仅限于SQLite。

首先,SELECT语句由不同部分组成。

SELECT 列名 FROM 表名 WHERE 条件;

对于列名,您需要填写您想要从表名获取的列的名称。相当简单明了。
如果您想选择多个列,只需用逗号列出它们,就像这样:

SELECT 列名1, 列名2, 列名3 FROM 表名 WHERE 条件;

要选择表的每一列,只需用*代替列名。

SELECT * FROM 表名 WHERE 条件;

注意:只有在语句中选择了列,您才能在ResultSet中访问这些列。如果您选择了channelid,除非您也选择了userid,否则您将无法获取userid。(SELECT channelid, userid FROM 表名 WHERE 条件;

您似乎理解了WHERE部分,因此我将跳过它。如果您需要更多帮助或想要进一步扩展对SQLite的使用,可以在线查阅一些教程

现在,在编写了正确的SELECT语句之后,是时候在Java中访问数据了。

因此,您必须遍历您的ResultSet

ResultSet rs = LiteSQL.onQuery(
"SELECT channelid, userid " +
"FROM ticketchans " +
"WHERE guildid = " + event.getGuild().getIdLong() +
" AND userid = " + event.getUserIdLong()
);
// 遍历结果集
while (rs.next()) {
Long userid = rs.getLong("userid");
Long channelid = rs.getLong("channelid");
}

您现在拥有了所需的数据,并可以根据您的需要使用它。

从SQLite删除数据

大部分内容适用于SQL总体,不仅限于SQLite。

DELETE语句的结构与SELECT语句类似,尽管它不包括列(当然了)。

DELETE FROM 表名 WHERE 条件;

如第一部分所述,您必须选择要从中删除数据的表,然后使用条件进一步缩小范围。

在您的情况下,删除特定的票务信息将是这样的:

DELETE FROM ticketchans WHERE guildid = GID and userid = UID and channelid = CID;

如果在条件中不使用所有三个ID,您可能会删除整个服务器的所有票务信息或某个用户的所有票务信息。由于channelid始终是唯一的,您可能可以跳过userid = UID部分,但具体细节取决于您。

如已提到的,如果您想要更具体的语句或需要一些变体,请查看您喜欢的教程。(提供的示例仅供参考,请使用您感觉舒适的内容。)

另外一个注意事项:我建议不要使用.complete(),而是改用.queue()
如果想知道原因和如何操作,请查看此页面。-Using-RestAction)

英文:

Getting Data from SQLite

Most of this applies to SQL in general and isn't specific to SQLite.

First off, a SELECT statement consists of different parts.

SELECT columns FROM table WHERE condition;

For columns you have to fill in the names of the columns you want to get from your table. Pretty self-explanatory.
If you want to select more than one column, you just have to list them with commas, like this:

SELECT column1, column2, column3 FROM table WHERE condition;

In order to select every column of your table you just write * instead of the columns.

SELECT * FROM table WHERE condition;

Note: You can only access columns in your ResultSet if you selected them in your statement. If you select channelid you won't be able to get userid, unless you select it as well. (SELECT channelid, userid FROM table WHERE condition;)

You seem to understand the WHERE part so I will skip it. In case you need some more help or want to expand your usage of SQLite even more, you may check out some tutorials online.

Now, after writing your correct SELECT statement it's time to access the data in Java.

Therefore, you have to loop through your ResultSet.

ResultSet rs = LiteSQL.onQuery(
"SELECT channelid, userid
FROM ticketchans 
WHERE guildid = " + event.getGuild().getIdLong() + " 
AND userid = " + event.getUserIdLong()
);
// loop through the result set  
while (rs.next()) {  
Long userid = rs.getLong("userid");   
Long channelid = rs.getLong("channelid");   
}  

You now have the data you need and can use it for whatever you want.

Deleting Data from SQLite

Most of this applies to SQL in general and isn't specific to SQLite.

The DELETE statement has a similar structure to the SELECT statement although it lacks the columns (of course).

DELETE FROM table WHERE condition;

As explained in the first part, you have to choose the table you want to delete data from and then narrow it down using conditions.

In your case, deleting a specific ticket would be like this:

DELETE FROM ticketchans WHERE guildid = GID and userid = UID and channelid = CID;

If you don't use all three IDs in the condition, you might end up deleting all tickets of a guild or of an user. Since the channelid is always unique you could possibly skip the userid = UID part, but the details are up to you.

As already mentioned, if you want more specific statements or need some variations, check out a tutorial of your liking. (The one provided is just an example, use whatever you are comfortable with.)

On another note: I would advice not using .complete() but .queue() instead.
If you want to know why and how, check out this page.-Using-RestAction>)

huangapple
  • 本文由 发表于 2020年8月29日 04:52:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63640736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定