表未删除

时间:2015-08-11 08:42:37

标签: java sqlite jdbc

我正在尝试删除一个表格,然后我按照此链接http://www.techonthenet.com/sqlite/truncate.php作为教程。但是当我执行下面的代码时,对于第一次运行,我期待的是 sqliteFactory.getRowCount()方法会return 0行,因为方法sqliteFactory.deleteTable(SysConsts.SQLITE_DATABASE_TABLE_NAME);之前刚刚调用,但我收到的是rowCount 这不是零。

在同一代码的第二次运行中,我希望sqliteFactory.CreateTable(SysConsts.SQLITE_DATABASE_TABLE_NAME);能够显示Log.i(TAG, "CreateTable", "table: ["+tableName+"] does not exist, will be created"); 因为该表应该被删除,但我收到了Log.i(TAG, "CreateTable", "table: ["+tableName+"] already exists.");

请告诉我为什么表格没有删除?我应该在删除后commit吗?

主要代码

public static void main(String[] args) throws SQLException, ClassNotFoundException {
    SQLiteFactory sqliteFactory = new SQLiteFactory();
    sqliteFactory.newSQLiteConn(SysConsts.SQLITE_DATABASE_NAME);
    sqliteFactory.CreateTable(SysConsts.SQLITE_DATABASE_TABLE_NAME);

    sqliteFactory.insertRecord(new Record("001", "55.07435", "8.79047", "c:\\bremen_0.xml"));
    Log.d(TAG, "main", "rowCount: "+sqliteFactory.getRowCount());

    //sqliteFactory.selectAll();
    //sqliteFactory.getNodeID("53.074415", "8.788047");
    sqliteFactory.selectXMLPathFor("53.074415", "8.788047");
    Log.d(TAG, "", ""+sqliteFactory.getRowCountLatLngFor("53.074415", "8.788047"));
    Log.d(TAG, "", ""+sqliteFactory.getRowCountNodeIDFor("001"));
    Log.d(TAG, "", ""+sqliteFactory.getRowCountXMLPathFor("c:\\brem_0.xml"));

    sqliteFactory.deleteTable(SysConsts.SQLITE_DATABASE_TABLE_NAME);
    Log.d(TAG, "main", "rowCount: "+sqliteFactory.getRowCount());
}

CREATETABLE

public void CreateTable(String tableName) throws SQLException, ClassNotFoundException {
    if (!this.isTableExists(tableName)) {
        Log.i(TAG, "CreateTable", "table: ["+tableName+"] does not exist, will be created");

        Connection conn = this.getConnection();
        Statement stmt = conn.createStatement();
        stmt.executeUpdate(this.sqlTable);

        stmt.close();
        conn.close();

    } else {
        Log.i(TAG, "CreateTable", "table: ["+tableName+"] already exists.");
        return;
    }
}

isTableExists

private boolean isTableExists(String tableName) throws ClassNotFoundException, SQLException {
    Connection conn = this.getConnection();
    DatabaseMetaData dbMeta = conn.getMetaData();
    ResultSet resSet = dbMeta.getTables(null, null, tableName, null);
    boolean exists = false;

    if (resSet.next()) {
        exists = true;
    } else {
        exists = false;
    }

    resSet.close();
    conn.close();

    return exists;
}

deleteTable

public void deleteTable(String tableName) throws ClassNotFoundException, SQLException {
    if (this.isTableExists(tableName)) {
        Log.i(TAG, "deleteTable", "table: ["+tableName+"] already exists, and will be deleted.");

        Connection conn = this.getConnection();
        PreparedStatement ps = conn.prepareStatement("delete from "+this.TABLE_NAME);

        ps.close();
        conn.close();

    } else {
        Log.i(TAG, "deleteTable", "table: ["+tableName+"] does not exist, can't be deleted.");
        return;
    }
}

getRowCount

public int getRowCount() throws SQLException, ClassNotFoundException {  
    Connection conn = this.getConnection();
    Statement stmt= conn.createStatement();
    ResultSet resSet = stmt.executeQuery("SELECT COUNT(*) AS rowCount FROM "+TABLE_NAME+";");

    int cnt = resSet.getInt("rowCount");

    resSet.close();
    stmt.close();
    conn.close();

    return cnt;
}

3 个答案:

答案 0 :(得分:3)

准备语句不会运行它。致电execute()以运行PreparedStatement

答案 1 :(得分:1)

您的deleteTable方法错误。 Delete from table xyz删除表的所有行,但不删除表本身。请改用drop table xyz

当然,正如Iaalto回答的那样,你需要实际执行delete语句。 :)

答案 2 :(得分:1)

尝试更改PreparedStatement:

PreparedStatement ps = conn.prepareStatement("drop" + this.TABLE_NAME);