gpt4 book ai didi

java - 表未删除

转载 作者:行者123 更新时间:2023-12-01 16:56:34 25 4
gpt4 key购买 nike

我正在尝试删除一个表,我点击了此链接 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());
}

创建表:

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;
}

删除表:

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;
}
}

获取行计数:

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;
}

最佳答案

准备语句并不运行它。调用 execute() 来运行 PreparedStatement

关于java - 表未删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31937093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com