2014-09-23 111 views
0

您好我有我的SQLite數據庫嘗試此命令,但它不會降/刪除我的數據庫表, 這裏我reference在Java中,如何刪除SQLite表

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

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

//Output 
output : 0 

沒有返回錯誤,所以我仍然我自己無法確定是什麼讓代碼無法正常工作。

守則DROP TABLE

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(); 
} 
+0

你有沒有檢查數據庫,看看錶中有實際上已經下降?由於'executeUpdate'可能會返回*「(1)SQL Data Manipulation Language(DML)語句的行數,或者(2)0返回任何不返回的SQL語句」* – MadProgrammer 2014-09-23 01:20:36

+0

是的,而且我有幾次刷新了表,表和它的內容仍然存在.. – 2014-09-23 01:22:55

+1

在命令行中使用「DROP TABLE IF EXISTS'myDatabase.myTable」並檢查會發生什麼 – 2014-09-23 01:26:57

回答

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();