我想從SQLite數據庫中刪除所有表。問題是我似乎無法通過SQLite命令執行它,並且無法在我的計算機上找到這些文件。我有以下代碼將數據插入到數據庫中。從SQLite數據庫中刪除內容
package com.example.mycoursetimetable;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBTools extends SQLiteOpenHelper {
public DBTools(Context applicationContext){
super(applicationContext, "module.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase database) {
String query = "CREATE TABLE modules (moduleId INTEGER PRIMARY KEY,ModuleCode TEXT PRIMARY KEY, ModuleName TEXT,ModuleType TEXT, DayOfWeek TEXT, StartTime TEXT, EndTime TEXT,Location TEXT,AdditionalInfo Text)";
database.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS modules";
database.execSQL(query);
onCreate(database);
}
public void insertModule(HashMap<String, String> queryValues){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("ModuleCode", queryValues.get("ModuleCode"));
values.put("ModuleName", queryValues.get("ModuleName"));
values.put("ModuleType", queryValues.get("ModuleType"));
values.put("DayOfWeek", queryValues.get("DayOfWeek"));
values.put("StartTime", queryValues.get("StartTime"));
values.put("EndTime", queryValues.get("EndTime"));
values.put("Location", queryValues.get("Location"));
values.put("AdditionalInfo", queryValues.get("AdditionalInfo"));
database.insert("modules", null, values);
database.close();
}
public int updateModule(HashMap<String, String> queryValues){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("ModuleCode", queryValues.get("ModuleCode"));
values.put("ModuleName", queryValues.get("ModuleName"));
values.put("ModuleType", queryValues.get("ModuleType"));
values.put("DayOfWeek", queryValues.get("DayOfWeek"));
values.put("StartTime", queryValues.get("StartTime"));
values.put("EndTime", queryValues.get("EndTime"));
values.put("Location", queryValues.get("Location"));
values.put("AdditionalInfo", queryValues.get("AdditionalInfo"));
return database.update("modules", values,
"moduleId" + " = ?", new String[] {queryValues.get("moduleId") });
}
public void deleteModule(String id){
SQLiteDatabase database = this.getWritableDatabase();
String deleteQuery = "DELETE FROM modules WHERE moduleId='" + id + "'";
database.execSQL(deleteQuery);
}
public ArrayList<HashMap<String, String>> getAllModules(){
ArrayList<HashMap<String, String>> moduleArrayList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM modules ORDER BY ModuleCode";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
HashMap<String, String> moduleMap = new HashMap<String, String>();
moduleMap.put("moduleId", cursor.getString(0));
moduleMap.put("ModuleCode", cursor.getString(1));
moduleMap.put("ModuleName", cursor.getString(2));
moduleMap.put("ModuleType", cursor.getString(3));
moduleMap.put("DayOfWeek", cursor.getString(4));
moduleMap.put("StartTime", cursor.getString(5));
moduleMap.put("EndTime", cursor.getString(6));
moduleMap.put("Location", cursor.getString(7));
// moduleMap.put("AdditionalInfo", cursor.getString(8));
moduleArrayList.add(moduleMap);
} while(cursor.moveToNext());
}
return moduleArrayList;
}
public HashMap<String, String> getModuleInfo(String id){
HashMap<String, String> moduleMap = new HashMap<String, String>();
SQLiteDatabase database = this.getReadableDatabase();
String selectQuery = "SELECT * FROM modules WHERE moduleId='" + id + "'";
Cursor cursor = database.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
moduleMap.put("moduleId", cursor.getString(0));
moduleMap.put("ModuleCode", cursor.getString(1));
moduleMap.put("ModuleName", cursor.getString(2));
moduleMap.put("ModuleType", cursor.getString(3));
moduleMap.put("DayOfWeek", cursor.getString(4));
moduleMap.put("StartTime", cursor.getString(5));
moduleMap.put("EndTime", cursor.getString(6));
moduleMap.put("Location", cursor.getString(7));
// moduleMap.put("AdditionalInfo", cursor.getString(8));
} while(cursor.moveToNext());
}
return moduleMap;
}
}
從SQLite的外殼,我都以各種方式來選擇我的數據庫中的表試過很多次。它一直說沒有這樣的表,但數據在我的應用程序在啓動。有沒有人知道我怎麼能擺脫這些表?
TIA
如果您確定沒有明確安裝android數據庫,那麼它將會被應用程序刪除,所以您可以重新安裝應用程序。但是如果你想在運行時執行它,你必須確保它沒有被使用,然後打開它並運行你的查詢命令 – NitroNbg
[Drop all tables command](http://stackoverflow.com/ question/525512/drop-all-tables-command) –
您的問題已經在下面的地址得到解答:http://stackoverflow.com/a/548297/845443 –