对于我的应用程序,我正在使用SQLite数据库。
我按照实施数据库的教程进行了操作。但后来我发现了一些东西......我错过了一张额外的桌子。
当我尝试添加另一个表时,它给了我一个错误,所以我尝试了很多不同的解决方案,但这里没有任何作用。我尝试的以下是:
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
。所以基本上如果我不能创建表我想开始新鲜,但我不知道我是如何尝试一切来摆脱旧的。
提前致谢
答案 0 :(得分:2)
你在创建表查询中忘记了逗号','。
语法应为
CREATE TABLE mytable (
col1 int,
col2 text);
编辑:会有评论,但没有足够的代表。
答案 1 :(得分:2)
尝试替换
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 " +
");";