我以某种方式无法创建表。我检查了语法并搜索了具有相同错误的其他人,但我找不到错误是什么。我使用相同的语法创建所有表,但仅stuff
表存在问题。即使将数据库版本更改为2次并多次卸载我的应用程序。我在此处放置了一部分代码。
public class Stuff {
//*****DataBase query*****/
public static final String TABLE_NAME = "stuff";
public static final String CLM_ID = "id";
public static final String CLM_NAME = "name";
public static final String CLM_PRICE = "price";
public static final String CLM_DESCRIPTION = "description";
public static final String CLM_BRAND = "brand";
public static final String CLM_MODEL = "model";
public static final String CLM_COUNT = "count";
public static final String CLM_COLOR = "color";
public static final String CLM_CATGID = "catgid";
public static final String SQL_DROP_TABLE = String.format("DROP TABLE %s", TABLE_NAME);
public static final String SQL_CREATE_TABLE = String.format("CREATE TABLE IF NOT EXISTS %s (" +
"%s INTEGER PRIMARY KEY," +
"%s TEXT," +
"%s REAL," +
"%s TEXT NOT NULL," +
"%s TEXT NOT NULL," +
"%s TEXT," +
"%s INTEGER," +
"%s TEXT NOT NULL," +
"FOREIGN KEY (%s) REFERENCES(%s)" +
");", TABLE_NAME, CLM_ID, CLM_NAME,CLM_PRICE,CLM_DESCRIPTION,CLM_BRAND,CLM_MODEL,CLM_COUNT,CLM_COLOR,CLM_CATGID,Category.TABLE_NAME);
}
public class DBHelper extends SQLiteOpenHelper {
private static DBHelper helper;
private static SQLiteDatabase database;
public static final String DATA_BASE_NAME = "dbDigikala";
public static final int DATA_BASE_VERSION = 1;
public static SQLiteDatabase getDatabase(Context context) {
return getInstance(context).getWritableDatabase();
}
public static DBHelper getInstance(Context context) {
if (helper == null) {
helper = new DBHelper(context, DATA_BASE_NAME, null, DATA_BASE_VERSION);
}
return helper;
}
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(Category.SQL_CREATE_TABLE);
db.execSQL(Stuff.SQL_CREATE_TABLE);
db.execSQL(Rate.SQL_CREATE_TABLE);
db.execSQL(Favorit.SQL_CREATE_TABLE);
db.execSQL(Image.SQL_CREATE_TABLE);
db.execSQL(Video.SQL_CREATE_TABLE);
db.execSQL(Address.SQL_CREATE_TABLE);
db.execSQL(User.SQL_CREATE_TABLE);
db.execSQL(Comment.SQL_CREATE_TABLE);
db.execSQL(Factor.SQL_CREATE_TABLE);
db.execSQL(SoldStuff.SQL_CREATE_TABLE);
db.execSQL(Slake.SQL_CREATE_TABLE);
db.execSQL(SlakeStuff.SQL_CREATE_TABLE);
db.execSQL(SlakeUser.SQL_CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(Category.SQL_DROP_TABLE);
db.execSQL(Stuff.SQL_DROP_TABLE);
db.execSQL(Rate.SQL_DROP_TABLE);
db.execSQL(Favorit.SQL_DROP_TABLE);
db.execSQL(Image.SQL_DROP_TABLE);
db.execSQL(Video.SQL_DROP_TABLE);
db.execSQL(Address.SQL_DROP_TABLE);
db.execSQL(User.SQL_DROP_TABLE);
db.execSQL(Comment.SQL_DROP_TABLE);
db.execSQL(Factor.SQL_DROP_TABLE);
db.execSQL(SoldStuff.SQL_DROP_TABLE);
db.execSQL(Slake.SQL_DROP_TABLE);
db.execSQL(SlakeStuff.SQL_DROP_TABLE);
db.execSQL(SlakeUser.SQL_DROP_TABLE);
onCreate(db);
}
}
public class Config extends Application {
public static Context context;
public static Activity currentActivity;
public static final long SPLASH_DELAY = 2000;
public static SQLiteDatabase database;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
database = DBHelper.getDatabase(getApplicationContext());
}
}
答案 0 :(得分:1)
问题是您设置ForeignKey "FOREIGN KEY (%s) REFERENCES(%s)"
的地方。
如下更新代码:
public static final String SQL_CREATE_TABLE = String.format("CREATE TABLE IF NOT EXISTS %s (" +
"%s INTEGER PRIMARY KEY," +
"%s TEXT," +
"%s REAL," +
"%s TEXT NOT NULL," +
"%s TEXT NOT NULL," +
"%s TEXT," +
"%s INTEGER," +
"%s TEXT NOT NULL," +
"FOREIGN KEY (%s) REFERENCES %s(%s)" +
");", TABLE_NAME, CLM_ID, CLM_NAME,CLM_PRICE,CLM_DESCRIPTION,CLM_BRAND,CLM_MODEL,CLM_COUNT,CLM_COLOR,CLM_CATGID,Category.TABLE_NAME,Category.CLM_ID);
您仅传递表名并将其设置在错误的位置。
"FOREIGN KEY (%s) REFERENCES %s(%s)"
在REFERENCES之后,第一个%s
是表名,第二个(%s)
是类别表中的列ID。
答案 1 :(得分:1)
子句REFERENCES
必须使用以下语法:REFERENCES table_name(column_id)
:
public static final String SQL_CREATE_TABLE = String.format("CREATE TABLE IF NOT EXISTS %s (" +
"%s INTEGER PRIMARY KEY," +
"%s TEXT," +
"%s REAL," +
"%s TEXT NOT NULL," +
"%s TEXT NOT NULL," +
"%s TEXT," +
"%s INTEGER," +
"%s TEXT NOT NULL," +
"FOREIGN KEY (%s) REFERENCES %s(%s)" +
");", TABLE_NAME, CLM_ID, CLM_NAME,CLM_PRICE,CLM_DESCRIPTION,CLM_BRAND,CLM_MODEL,CLM_COUNT,CLM_COLOR,CLM_CATGID,Category.TABLE_NAME,Category.CLM_ID);