【问题标题】:Deleting table in sqlite including column names删除sqlite中的表,包括列名
【发布时间】: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 没有显示任何错误,但是当我打印检索到的列名时,它会打印一些条目已被删除的列名。我不想要那些列。

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

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

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

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

    【讨论】:

    • 抱歉打错了。我已经编辑了我的问题。问题不在于那个。
    • 在出现错误时显示一些用于创建数据库和 logcat 的代码。
    【解决方案2】:

    从数据库中删除表。

    db.execSQL("如果存在则删除表" + YOUR_TABLE);

    【讨论】:

    • 就像我说的,这不是删除整个表。如果我将表保存在数据库中,我应该使用点运算符访问它吗?即 db.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME + "." +YOUR_TABLE);
    【解决方案3】:

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

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

    SQL_DELETE_ENTRIES1 = "DROP TABLE IF EXISTS " + FeedEntry.TABLE_NAME;
    SQL_DELETE_ENTRIES2 = "DROP TABLE IF EXISTS " + CycDetails.TABLE_NAME;
    

    【讨论】:

      猜你喜欢
      • 2023-03-14
      • 2011-08-21
      • 1970-01-01
      • 2018-06-28
      • 2012-02-12
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多