0

我在 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 没有显示错误,但是当我打印检索到的列名时,它会打印一些条目已被删除的列名。我不想要那些列。

而且我没有以任何方式使用升级方法。

4

3 回答 3

1

您应该更正删除表 SQL 的语法( DROP TABLE IF EXISTS 是两次),没有理由它不起作用。

如果您收到权限被拒绝错误,请确保您访问的是您自己的应用程序的数据库,而不是其他应用程序的数据库。如果需要,请在创建数据库时仔细检查您是否使用了正确的包名称。

于 2013-10-15T03:29:33.087 回答
0

remove table from database.

db.execSQL("DROP TABLE IF EXISTS " + YOUR_TABLE);

于 2013-10-15T03:53:51.043 回答
0

要删除表,只需使用DROP TABLE语句。

表名应该与您在语句中使用的完全相同。CREATE TABLE在您的情况下,这将是:

SQL_DELETE_ENTRIES1 = "DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;
SQL_DELETE_ENTRIES2 = "DROP TABLE IF EXISTS " + CycDetails.TABLE_NAME;
于 2013-10-15T07:37:47.843 回答