Ridzuan Adris
Ridzuan Adris

Reputation: 1272

In Java, How to Drop Sqlite Table

hi i have tried this command on my sqlite database but it wont drop/delete my database table, here my reference

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

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

//Output
output : 0

there are no return error so i still cant figure by myself what is making the code not working.

Code to 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();
}

Upvotes: 1

Views: 4572

Answers (1)

Ridzuan Adris
Ridzuan Adris

Reputation: 1272

Thanks to MadProgrammer and other, Actually i miss to put commit statement on my code..

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

Upvotes: 2

Related Questions