我目前正在创建的插件,Bukkit Minecraft的HelpManager,需要一个高效的数据库,这是我第一次在插件级别上使用数据库。
所以我环顾四周,我看到了一些模糊的教程,以及其他需要依赖插件才能让数据库工作的教程,我不希望我的用户必须下载。
在这里,我已成功制作了6列,但我需要一种方法为每张票提供一个唯一的号码(ID),所以我尝试了以下代码:
" CREATE SEQUENCE if not exist ID" +
"(MINVALUE 1, START WITH 1, INCREMENT BY 1, CACHE 100)";
这是错误:
2013-08-13 23:23:30 [SEVERE] java.sql.SQLException: near "CREATE": syntax error
2013-08-13 23:23:30 [SEVERE] at org.sqlite.NativeDB.throwex(NativeDB.java:210)
2013-08-13 23:23:30 [SEVERE] at org.sqlite.NativeDB._exec(Native Method)
2013-08-13 23:23:30 [SEVERE] at org.sqlite.Stmt.executeUpdate(Stmt.java:152)
2013-08-13 23:23:30 [SEVERE] at com.ahellhound.bukkit.helpmanager.Main.onEnable(Main.java:43)
2013-08-13 23:23:30 [SEVERE] at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
2013-08-13 23:23:30 [SEVERE] at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
2013-08-13 23:23:30 [SEVERE] at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:382)
2013-08-13 23:23:30 [SEVERE] at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugin(CraftServer.java:286)
2013-08-13 23:23:30 [SEVERE] at org.bukkit.craftbukkit.v1_6_R2.CraftServer.enablePlugins(CraftServer.java:268)
2013-08-13 23:23:30 [SEVERE] at net.minecraft.server.v1_6_R2.MinecraftServer.l(MinecraftServer.java:319)
2013-08-13 23:23:30 [SEVERE] at net.minecraft.server.v1_6_R2.MinecraftServer.f(MinecraftServer.java:296)
2013-08-13 23:23:30 [SEVERE] at net.minecraft.server.v1_6_R2.MinecraftServer.a(MinecraftServer.java:256)
2013-08-13 23:23:30 [SEVERE] at net.minecraft.server.v1_6_R2.DedicatedServer.init(DedicatedServer.java:164)
2013-08-13 23:23:30 [SEVERE] at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:397)
2013-08-13 23:23:30 [SEVERE] at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)
现在,如果/当我开始工作时,我会将缓存设置为3000,因为我不希望服务器耗尽唯一的票证ID。
我认为有一种更有效/合乎逻辑的做法,我只是没有找到/了解它。
这是完整的课程:
public class Main extends JavaPlugin implements CommandExecutor, Listener {
Logger log = Logger.getLogger("Minecraft");
//Database Config
public String tableName = new String();
@Override
public void onEnable() {
reloadConfiguration();
saveDefaultConfig();
getServer().getPluginManager().registerEvents(this, this);
log.info("[" + getDescription().getName() + "] "
+ getDescription().getName() + " version "
+ getDescription().getVersion() + " is now enabled.");
log.info("[" + getDescription().getName() + "]" + " Made By AhellHound");
//Connecting to DB
Connection c = null;
java.sql.Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:plugins" + File.separator + "HelpManager" + File.separator + "databse.db");
stmt = c.createStatement();
c.setAutoCommit(false);
String sql = "CREATE TABLE if not exists " + tableName +
" (ID INT UNIQUE NOT NULL, PlayerName TEXT NOT NULL, Message TEXT NOT NULL, Location TEXT, Priority NOT NULL, Viewed BOOLEAN NOT NULL)" +
" CREATE SEQUENCE if not exist seq_person" +
"(MINVALUE 1, START WITH 1, INCREMENT BY 1, CACHE 100)";
stmt.executeUpdate(sql);
stmt.close();
c.close();
} catch ( Exception e ) {
e.printStackTrace();
}
}
public void reloadConfiguration() {
//Config
tableName = new String();
tableName = getConfig().getString("tableName");
}
}
如果您需要更多信息,我很乐意回答您的问题。
感谢您的帮助, AhellHound
答案 0 :(得分:1)
您可以将ID
列设为主键,然后将其设置为自动增量
"CREATE TABLE if not exists " + tableName +
" (ID INTEGER PRIMARY KEY AUTOINCREMENT,
PlayerName TEXT NOT NULL,
Message TEXT NOT NULL,
Location TEXT,
Priority NOT NULL,
Viewed BOOLEAN NOT NULL)"