gpt4 book ai didi

java - SQLite 无法在 OnCreate 方法中创建数据库。 E/SQLiteLog : (1)

转载 作者:行者123 更新时间:2023-12-01 18:00:25 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





What is a stack trace, and how can I use it to debug my application errors?

(7 个回答)


5年前关闭。




对于我的应用程序,我正在使用 SQLite 数据库。

我遵循了一个实现数据库的教程并且它有效。但后来我注意到了一些东西。我错过了一张额外的 table 。

当我尝试添加另一个表时,它给了我一个错误,所以我尝试了很多不同的解决方案,但这里没有任何效果。我尝试过的以下内容是:

  • 增加 onUpgrade 的版本号方法
  • 删除数据库槽删除语句
  • 新建数据库文件
  • 新建数据库表
  • 已删除的应用程序和文件
  • 访问设备监视器以定位 .DB文件

  • 所有这些解决方案都不适合我。

    现在有趣的部分是当我删除我的应用程序并尝试上述所有其他解决方案时。我什至不能再创建我的第一个工作数据库表了。好像我的整个数据库都坏了。

    我不知道该怎么办知道。我不重视我数据库中的数据。我只想从一个干净、新鲜的数据库开始。

    我用来实现数据库的代码在故障排除之前工作,是:
        public class Database extends SQLiteOpenHelper{

    // Fields
    private final static int DATABASE_VERSION = 1;
    // Table products
    private final static String DATABASE_NAME = "product.db";
    public static final String TABLE_PRODUCTS = "products";
    public static final String ID = "_id";
    public static final String NAME = "name";
    public static final String CALORIES = "calories";
    public static final String PRODUCT_TYPE = "producttype";
    public static final String CATEGORY_TYPE = "categorytype";
    public static final String IMAGE = "image";

    // Table Calories
    /* Second table
    public static final String TABLE_CALORIES = "calories";
    public static final String CALID = "_id";
    public static final String AMOUNT_OF_CALORIES = "amountOfcalories";
    public static final String DAY_OF_THE_WEEK = "dayoftheweek";
    */
    // Table product fields
    private String prodID = null;
    private String prodname = null;
    private String prodcalories = null;
    private String prodproducttype = null;
    private String prodcategorytype = null;
    private String prodimage = null;
    private String totalCalories = null;
    private ProductType tempProductType = null;
    private ProductType newProductType = null;
    private CategoryType tempCatType = null;
    private CategoryType newCategoryType = null;
    private SQLiteDatabase database;

    // Table Calories fields
    private String calID = null;
    private String calTotalCalories = null;

    public Database(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    System.out.println("DATABASE PATH: " + context.getDatabasePath("product.db"));
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    System.out.println(db.isOpen());

    String create_table_products = "create table " + TABLE_PRODUCTS + "(" +
    ID + " integer primary key auto increment " +
    NAME + " text " +
    CALORIES + " text " +
    PRODUCT_TYPE + " text " +
    CATEGORY_TYPE + " text " +
    IMAGE+ " text " +
    ");";
    db.execSQL(create_table_products);

    /* Second table
    String create_table_calories = "create table " + TABLE_CALORIES + "(" +
    CALID + " integer primary key auto increment " +
    AMOUNT_OF_CALORIES + " text " +
    DAY_OF_THE_WEEK + " text " +
    ");";
    db.execSQL(create_table_calories);
    */
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE " + TABLE_PRODUCTS);
    //db.execSQL("DROP TABLE IF EXISTS " + TABLE_CALORIES);
    onCreate(db);
    }

    }

    代码暂时停在 OnCreate()创建第一个表。它给了我一个模糊的 E/SQLite: (1) error .所以基本上如果我不能创建表格,我想重新开始,但我不知道我是如何尝试一切来摆脱旧的。

    提前致谢

    最佳答案

    尝试更换

    String create_table_products = "create table " + TABLE_PRODUCTS + "(" +
    ID + " integer primary key auto increment " +
    NAME + " text " +
    CALORIES + " text " +
    PRODUCT_TYPE + " text " +
    CATEGORY_TYPE + " text " +
    IMAGE+ " text " +
    ");";


     String create_table_products = "create table " + TABLE_PRODUCTS + "(" +
    ID + " integer primary key autoincrement, " +
    NAME + " text, " +
    CALORIES + " text, " +
    PRODUCT_TYPE + " text, " +
    CATEGORY_TYPE + " text, " +
    IMAGE+ " text " +
    ");";

    关于java - SQLite 无法在 OnCreate 方法中创建数据库。 E/SQLiteLog : (1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41415499/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com