【问题标题】:In Java, How to Drop Sqlite Table在 Java 中,如何删除 Sqlite 表
【发布时间】:2014-09-23 01:18:09
【问题描述】:

嗨,我已经在我的 sqlite 数据库上尝试过这个命令,但它不会删除/删除我的数据库表, 这是我的reference

Statement stmt = conn.createStatement();
String sqlCommand = "DROP TABLE 'myTable' ";

System.out.println("output : " + stmt.executeUpdate(sqlCommand));

//Output
output : 0

没有返回错误,所以我仍然无法自己弄清楚是什么导致代码无法正常工作。

删除表的代码

Connection c = null;
Statement stmt = null;
String sql;
c = openSqlite(c);         //method i create to setup sqlite database connection
stmt = c.createStatement();

try{
System.out.println("Deleting table in given database...");

String sqlCommand = "DROP TABLE 'myTable' ";

stmt.executeUpdate(sqlCommand);
System.out.println("Table  deleted in given database...");

stmt.close();
c.commit();
c.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}

【问题讨论】:

  • 您是否检查过数据库以查看该表是否实际上已被删除?因为executeUpdate 可能会返回“(1) SQL 数据操作语言 (DML) 语句的行数或 (2) 0 用于不返回任何内容的 SQL 语句”
  • 是的,我已经多次刷新我的表格,但表格及其内容仍然存在..
  • 在命令行中使用“DROP TABLE IF EXISTS 'myDatabase.myTable”并检查会发生什么
  • 另外,请确保您启用自动提交或提交事务...
  • 表名真的是“myDatabase.myTable”吗?使用引号,myDatabase. 是表名的一部分。

标签: java sqlite


【解决方案1】:

感谢 MadProgrammer 和其他人,实际上我想在我的代码上添加提交语句..

Statement stmt = conn.createStatement();
String sqlCommand = "DROP TABLE IF EXISTS 'myDatabase.myTable' ";

System.out.println("output : " + stmt.executeUpdate(sqlCommand));

stmt.close();
conn.commit();     // commit after execute sql command
                //COMMIT TRANSACTION makes all data modifications performed since 
                //the start of the transaction a permanent part of the database, 
conn.close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2011-12-19
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    • 2021-01-24
    • 2010-12-25
    相关资源
    最近更新 更多