Skip to content

Commit

Permalink
修复数据库插入问题
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamVoid committed Feb 2, 2024
1 parent 7363642 commit c7750c5
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions MiraiMC-Base/src/main/java/me/dreamvoid/miraimc/api/MiraiMC.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
@SuppressWarnings("unused")
public class MiraiMC {
private static final String prefix = MiraiMCConfig.Database.Settings.Prefix;

static {
try {
DatabaseHandler.executeUpdate("CREATE TABLE IF NOT EXISTS ?bind (uuid TINYTEXT NOT NULL, qqid long NOT NULL)", prefix);
DatabaseHandler.executeUpdate("CREATE TABLE IF NOT EXISTS " + prefix + "bind (uuid TINYTEXT NOT NULL, qqid long NOT NULL)");
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
}
Expand All @@ -30,29 +30,29 @@ public class MiraiMC {
* @param account 玩家QQ号
*/
public static void addBind(UUID uuid, long account) {
try(ResultSet resultSetUUID = DatabaseHandler.executeQuery("SELECT * FROM ?bind WHERE uuid=? LIMIT 1", prefix, uuid);
ResultSet resultSetAccount = DatabaseHandler.executeQuery("SELECT * FROM ?bind WHERE qqid=? LIMIT 1", prefix, account)) {
try(ResultSet resultSetUUID = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE uuid=? LIMIT 1", uuid);
ResultSet resultSetAccount = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE qqid=? LIMIT 1", account)) {

if (!resultSetUUID.isBeforeFirst() && resultSetAccount.isBeforeFirst()) {
DatabaseHandler.executeUpdate("UPDATE ?bind SET uuid=? WHERE qqid=?", prefix, uuid, account);
DatabaseHandler.executeUpdate("UPDATE " + prefix + "bind SET uuid=? WHERE qqid=?", uuid, account);
} else if (resultSetUUID.isBeforeFirst() && !resultSetAccount.isBeforeFirst()) {
DatabaseHandler.executeUpdate("UPDATE ?bind SET qqid=? WHERE uuid=?", prefix, account, uuid);
DatabaseHandler.executeUpdate("UPDATE " + prefix + "bind SET qqid=? WHERE uuid=?", account, uuid);
} else if (!resultSetUUID.isBeforeFirst() && !resultSetAccount.isBeforeFirst()) {
DatabaseHandler.executeUpdate("INSERT INTO ?bind VALUES(?,?)", prefix, uuid, account);
DatabaseHandler.executeUpdate("INSERT INTO " + prefix + "bind VALUES(?,?)", uuid, account);
}
} catch (SQLException e){
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
}
}

/**
* 移除一个Minecraft账号绑定的QQ
* @param uuid 玩家UUID
*/
public static void removeBind(UUID uuid) {
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM ?bind WHERE uuid=? LIMIT 1", prefix, uuid)){
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE uuid=? LIMIT 1", uuid)){
if (resultSet.next()) {
DatabaseHandler.executeUpdate("DELETE FROM ?bind WHERE uuid=?", prefix, uuid);
DatabaseHandler.executeUpdate("DELETE FROM " + prefix + "bind WHERE uuid=?", uuid);
}
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
Expand All @@ -64,23 +64,23 @@ public static void removeBind(UUID uuid) {
* @param account 玩家QQ号
*/
public static void removeBind(long account) {
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM ?bind WHERE qqid=? LIMIT 1", prefix, account)){
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE qqid=? LIMIT 1", account)){
if (resultSet.next()) {
DatabaseHandler.executeUpdate("DELETE FROM ?bind WHERE qqid=?", prefix, account);
DatabaseHandler.executeUpdate("DELETE FROM " + prefix + "bind WHERE qqid=?", account);
}
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
}
}

/**
* 获取Minecraft账号绑定的QQ号
* 如果不存在,返回0
* @param uuid 玩家UUID
* @return QQ号
*/
public static long getBind(UUID uuid){
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM ?bind WHERE uuid=? LIMIT 1", prefix, uuid)){
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE uuid=? LIMIT 1", uuid)){
return resultSet.next() ? resultSet.getLong("qqid") : 0L;
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
Expand All @@ -96,7 +96,7 @@ public static long getBind(UUID uuid){
*/
@Nullable
public static UUID getBind(long account) {
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM ?bind WHERE qqid=? LIMIT 1", prefix, account)){
try (ResultSet resultSet = DatabaseHandler.executeQuery("SELECT * FROM " + prefix + "bind WHERE qqid=? LIMIT 1", account)){
return resultSet.next() ? UUID.fromString(resultSet.getString("uuid")) : null;
} catch (SQLException e) {
throw new RuntimeException("处理数据时出现异常,请检查MiraiMC数据库配置是否正确", e);
Expand Down

0 comments on commit c7750c5

Please sign in to comment.