How to setup sqlite?

Discussion in 'Plugin Development' started by alexgeek, Feb 8, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    alexgeek

    I'm having trouble trying to create a database and a small table. I get the error "Could not connect org.sqlite.JDBC".
    This is the code, if someone could look at it I'd be greatful.
    Code:
    package com.bukkit.perry.iLDR;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    
    public class iLDRDatabase {
    	public String file = "/plugins/iLDR/levers.db";
    	private Connection conn = null;
    	public iLDRDatabase()
    	{
    		System.out.println("Database class loaded");
    		loadDB();
    	}
    	private boolean try_once = false;
    	private void loadDB()
    	{
    		try {
    			Class.forName("org.sqlite.JDBC");
    			DriverManager.get
                conn = DriverManager.getConnection("jdbc:sqlite:"+file);
                Statement sql = conn.createStatement();
                ResultSet res = sql.executeQuery("CREATE TABLE IF NOT EXISTS `ildr_levers` " +
                		"(`id` INT( 4 ) NOT NULL AUTO_INCREMENT PRIMARY KEY , " +
                		"`x` INT( 6 ) NOT NULL ," +
                		"`y` INT( 6 ) NOT NULL ," +
                		"`z` INT( 6 ) NOT NULL)");
                sql = conn.createStatement();
                res = sql.executeQuery("INSERT INTO `ildr_levers` (x,y,z) VALUES ('1','2','3')");
                sql = conn.createStatement();
                res = sql.executeQuery("SELECT * FROM `ildr_levers`");
                while(res.next())
                {
                	System.out.println("Lever at: (" +
                			res.getInt("x")+','+
                			res.getInt("y")+','+
                			res.getInt("z")+')' 		);
                }
    		}
    		catch (Exception e)
    		{
    			System.out.println("Could not connect " + e.getMessage());
    			System.out.println(e.getCause());
    		}
    	}
    }
    
    Thanks
     
  2. Offline

    Redecouverte

    did you make sure that you have the sqlite.jar in your class path and the native library in your java library path?
     
  3. Offline

    alexgeek

    Yeah. I have no idea what I'm doing right or wrong though, couldn't find any source code for plugins that use sqllite
     
  4. Offline

    Redecouverte

    i'll debug it in a few min
     
  5. Offline

    Samkio

    Edit the manifest file:
    Code:
    Manifest-Version: 1.0
    Class-Path: ../sqlitejdbc-v056.jar 
    Makes sure the jdbc is in bukkit root.

    I really need to do a tutorial on this....
     
  6. Offline

    alexgeek

    Now I'm getting "Could not connect path to '/plugins/iLDR/levers.db': 'C:\plugins' does not exist"
    I think I may be passing the file path in the wrong format?
    Also do you know how I would autocreate the db if not found?
    @Samkio, yes please do :)
     
  7. Offline

    Redecouverte

    is
    Code:
    public String file = "./plugins/iLDR/levers.db";
    any better?

    it should be auto-created already, i guess
     
  8. Offline

    alexgeek

    Aha, finally getting somewhere thank you both.
    I can execute commands and query now (a lot of the above source code doesn't work).
    I'll post my helper class when its done if anyone's interested.
     
  9. Offline

    darknesschaos

    I would be terribly interested!
     
  10. Somehow this helped me with an unrelated issue. Hopefully someday I can code my own plugins... but thx for the indirect help everyone
     
Thread Status:
Not open for further replies.

Share This Page