SQLite与java不存储数据

时间:2014-06-20 00:48:15

标签: java sqlite

我正在尝试编写一个存储电视节目列表的应用程序,我正在使用SQLite来执行此操作,只要我不停止并启动程序,我似乎能够读回任何插入的数据,如果我这样做,那么数据库就像没有任何内容一样被读取。自动提交已启用。而且我很确定会写一些东西,因为当我打开.db文件时,我能够看到我保存在那里的值。这是我用来与数据库进行通信的代码

package DatabaseHelper;

import java.sql.ResultSet;
import java.sql.SQLException;

public class ShowDatabaseHelper extends DatabaseHelper<String>
{

    public ShowDatabaseHelper(String dbpath) throws SQLException 
    {
        super(dbpath);

        // Create table
        stmt = con.createStatement();
        stmt.execute("create table if not exists shows(showname varchar)");
        stmt.close();
    }

    public boolean insert(String showTitle) throws SQLException 
    {
        stmt = con.createStatement();

        // Check wether the show is allready in the table
        ResultSet rs = stmt.executeQuery("select * from shows where showname = '" + showTitle + "'");
        stmt.close();
        if( rs.getFetchSize() != 0 ) return false;

    stmt = con.createStatement();
        stmt.executeUpdate("insert into shows(showname) values('" + showTitle + "')");
        //con.commit();
        stmt.close();

        return true;
    }

    public void update( String current, String replacement ) throws SQLException 
    {
    stmt = con.createStatement();
        stmt.executeUpdate("update shows set showname = '" + replacement + "' where showname = '" + current + "'");
        //con.commit();
        stmt.close();
    }

    public void remove( String data ) throws SQLException 
    {
        stmt = con.createStatement();
        stmt.executeUpdate("delete from shows where showname = '" + data + "'");
        //con.commit();
        stmt.close();
    }

    public String[] select() throws SQLException 
    {
        String[] shows;
        stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery("select * from shows");

        stmt.close();

        if( rs.getFetchSize() == 0 ) return null;

        shows = new String[rs.getFetchSize()-1];

        //rs.first();
        for( int i = 0; i < rs.getFetchSize(); i++ )
        {
            shows[i] = rs.getString("showname");
            rs.next();
        }

        return shows;
    }

    @Override
    public void clear() throws SQLException 
    {
        stmt = con.createStatement();
        stmt.executeUpdate("truncate shows");
        //con.commit();
        stmt.close();
    }

}

这个延伸的超级类是以下

package DatabaseHelper;

import java.sql.*;

public abstract class DatabaseHelper<T>
{
    protected Connection con = null;
    protected Statement stmt = null;

    /**
     * Opens a connection to the database in question
     * 
     * @param dbpath
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public DatabaseHelper(String dbpath) throws SQLException
    {
        con = DriverManager.getConnection(dbpath);
        System.out.println(con.getAutoCommit());
    }

    /**
     * Clears the database
     * 
     * @throws SQLException
     */
    public abstract void clear() throws SQLException;

    /**
     * Closes the connection to the database
     * 
     * @throws SQLException
     */
    public void close() throws SQLException
    {
        //con.commit();
        con.close();
    }
}

1 个答案:

答案 0 :(得分:0)

我在设置自动提交的代码中看不到任何内容。我的建议是在显式提交中添加回来。

我认为如果你在启用自动提交时这样做,你会得到一个异常,所以它是一种检查断言的方法。

否则,添加代码来设置自动提交,我发现这比依赖假设更安全。

您还可以临时向close方法(和其他方法)添加调试语句,以确保它被调用。

您可能还想检查(并向我们展示)调用这些方法的代码。但是,我们无法分辨出发生了什么。就我们所知,你在退出之前用truncate调用方法: - )