【发布时间】:2013-10-15 15:37:47
【问题描述】:
我在 SqlLite 中删除表时遇到问题。
如果我使用-
db.delete(TABLE_NAME, null, null);
表的内容被删除,但列名仍然存在。如何也删除列名?
我也试过 db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME+"Name of the table");
但它根本不起作用。
我如何以编程方式删除整个表,或者有什么方法可以删除整个数据库而不使手机生根(否则我们会收到“权限被拒绝”错误)通过代码?
这是处理onCreate、创建和删除表的部分代码。
package com.example.appchecker;
import java.io.File;
import com.example.appchecker.FeedReaderContract.CycDetails;
import com.example.appchecker.FeedReaderContract.FeedEntry;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class FeedReaderDbHelper2 extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "final31.db";
public static boolean exists = false;
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ", ";
private static final String SQL_CREATE_ENTRIES =
" CREATE TABLE " +FeedEntry.TABLE_NAME + " (" +
FeedEntry._ID + " INTEGER PRIMARY KEY" + COMMA_SEP +
FeedEntry.COLUMN_NAME_NUMBER + TEXT_TYPE + COMMA_SEP +
FeedEntry.COLUMN_NAME_APP_NAME + TEXT_TYPE + COMMA_SEP +
FeedEntry.COLUMN_NAME_PKG_NAME + TEXT_TYPE +
// Any other options for the CREATE command
");";
private static final String SQL_CREATE_ENTRIES2 =
" CREATE TABLE " +CycDetails.TABLE_NAME + " (" +
CycDetails._ID + " INTEGER PRIMARY KEY" + COMMA_SEP +
CycDetails.COLUMN_NAME_CYCLENAME + TEXT_TYPE + COMMA_SEP +
CycDetails.COLUMN_NAME_DATE + TEXT_TYPE + COMMA_SEP +
CycDetails.COLUMN_NAME_OSVERSION + TEXT_TYPE + COMMA_SEP +
CycDetails.COLUMN_NAME_SWVERSION + TEXT_TYPE +
// Any other options for the CREATE command
");";
private static final String SQL_DELETE_ENTRIES1 =
"DROP TABLE IF EXISTS " + DATABASE_NAME.substring(0,DATABASE_NAME.length()-3)+".Mobile_App_Details";
private static final String SQL_DELETE_ENTRIES2 =
"DROP TABLE IF EXISTS " + DATABASE_NAME.substring(0, DATABASE_NAME.length()-3)+".Cycle_Details";
public FeedReaderDbHelper2(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
db.execSQL(SQL_CREATE_ENTRIES2);
return;
} /*Cursor cursor = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = '"+FeedEntry.TABLE_NAME+"'", null);
System.out.println("In on create1") ;
if(cursor==null)
{
System.out.println("In on create") ;
db.execSQL(SQL_CREATE_ENTRIES);
}
else
{
System.out.println("Update phase") ;
exists=true;
}
*/
}
public static void deletetable(SQLiteDatabase db)
{
db.delete(FeedEntry.TABLE_NAME, null, null);
db.delete(CycDetails.TABLE_NAME, null, null);
db.execSQL(SQL_DELETE_ENTRIES1);
db.execSQL(SQL_DELETE_ENTRIES2);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
//System.out.println("Update phase") ;
db.execSQL(SQL_DELETE_ENTRIES1);
db.execSQL(SQL_DELETE_ENTRIES2);
//db.delete(FeedEntry.TABLE_NAME, null, null);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
目前,即使在 MainActicity 中,我也在 on_click 中实例化可删除函数。 Logcat 没有显示任何错误,但是当我打印检索到的列名时,它会打印一些条目已被删除的列名。我不想要那些列。
而且我没有以任何方式使用升级方法。
【问题讨论】: