我有一个包含6个表的数据库。这些表是在内容提供程序和数据库帮助程序的帮助下创建的。当我使用这6个表运行应用程序时,应用程序正常工作,所有表都已创建,我可以查询表并插入数据。
但是,当我尝试将sqlite视图添加到我的数据库帮助程序的onCreate方法时,应用程序崩溃了。
根据我的理解,视图的onCreate方法无论出于何种原因在View的Join中使用的onCreate方法之前调用。
我需要知道的是如何强制我的databaseHelper首先创建表然后创建视图。
由于问题的性质(“视图”一词用于指其他内容),我很难找到解决方案。
这是创建视图的代码:
public static final String TABLE_NAME = "recent_listens";
public static final String TIMESTAMP_ID = timestampTable.TIMESTAMP_ID;
public static final String TRACK_NAME = trackTable.TRACK_NAME;
public static final String TRACK_MBID = trackTable.TRACK_MBID;
public static final String ALBUM_NAME = albumTable.ALBUM_NAME;
public static final String ALBUM_MBID = albumTable.ALBUM_MBID;
public static final String ARTIST_NAME = artistTable.ARTIST_NAME;
public static final String ARTIST_MBID = artistTable.ARTIST_MBID;
public static final String TIMESTAMP_VALUE = timestampTable.TIMESTAMP_VALUE;
public static final String IMAGE_ID = albumTable.ALBUM_IMG_ID;
private static final String LISTEN_VIEW_CREATE = ""
+ "CREATE VIEW " + TABLE_NAME + " AS "
+ "SELECT "
+ timestampTable.TABLE_TIMESTAMP + "." + timestampTable.TIMESTAMP_ID + " AS " + TIMESTAMP_ID + ", "
+ trackTable.TRACK_NAME + " AS " + TRACK_NAME + ", "
+ trackTable.TRACK_MBID + " AS " + TRACK_MBID + ", "
+ albumTable.ALBUM_NAME + " AS " + ALBUM_NAME + ", "
+ albumTable.ALBUM_MBID + " AS " + ALBUM_MBID + ", "
+ artistTable.ARTIST_NAME + " AS " + ARTIST_NAME + ", "
+ artistTable.ARTIST_MBID + " AS " + ARTIST_MBID + ", "
+ timestampTable.TIMESTAMP_VALUE + " AS " + TIMESTAMP_VALUE + ", "
+ albumTable.ALBUM_IMG_ID + " AS " + IMAGE_ID
+ " FROM "
+ timestampTable.TABLE_TIMESTAMP
+ " INNER JOIN " + trackTable.TABLE_TRACK + " ON "
+ timestampTable.TABLE_TIMESTAMP + "." + timestampTable.TIMESTAMP_CON_TRACK_ID + " = "
+ trackTable.TABLE_TRACK + "." + trackTable.TRACK_ID
+ " INNER JOIN " + albumTable.TABLE_ALBUM + " ON "
+ timestampTable.TABLE_TIMESTAMP + "." + timestampTable.TIMESTAMP_CON_ALBUM_ID + " = "
+ albumTable.TABLE_ALBUM + "." + albumTable.ALBUM_ID;
public static synchronized void onCreate(SQLiteDatabase database) {
database.execSQL(LISTEN_VIEW_CREATE);
这是我得到的错误:
引起:android.database.sqlite.SQLiteException:没有这样的列:artist_name(代码1):,编译时:CREATE VIEW recent_listens AS SELECT timestamps_table._id AS _id,track_name AS track_name,track_mbid AS track_mbid, album_name AS album_name,album_mbid AS album_mbid,artist_name AS artist_name,artist_mbid AS artist_mbid,timestamp AS timestamp,album_img_id AS album_img_id FROM timestamps_table INNER JOIN track ON timestamps_table.track_id = tracks._id INNER JOIN album ON timestamps_table.album_id = albums._id
这是我的databaseHelper类:
public class databaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "userData.db";
private static final int DATABASE_VERSION = 10;
public databaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
artistTable.onCreate(database);
albumTable.onCreate(database);
trackTable.onCreate(database);
tagTable.onCreate(database);
similarTable.onCreate(database);
timestampTable.onCreate(database);
listenView.onCreate(database);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
artistTable.onUpgrade(database, oldVersion, newVersion);
albumTable.onUpgrade(database, oldVersion, newVersion);
trackTable.onUpgrade(database, oldVersion, newVersion);
tagTable.onUpgrade(database, oldVersion, newVersion);
similarTable.onUpgrade(database, oldVersion, newVersion);
timestampTable.onUpgrade(database, oldVersion, newVersion);
listenView.onUpgrade(database, oldVersion, newVersion);
}
}
这在我的自定义内容提供者onCreate方法中调用。
@Override
public boolean onCreate() {
database = new databaseHelper(getContext());
return false;
}
答案 0 :(得分:0)
android.database.sqlite.SQLiteException: no such column: artist_name
这很明显你的视图试图访问该表中不存在的列(我认为是timestamps_table)。很可能你得到这个,因为你添加了artist_name
表,忘了在onUpgrade()
中增加数据库的新版本。
修复:升级db的版本或卸载应用程序并再次运行它或从设置中清除应用程序的数据
答案 1 :(得分:0)
万一其他人会遇到同样的问题,我会把它留下来。
事实证明,我没有在选择中提到的联接中包含一个表。
生成的View创建字符串是:
form = forms.TypesForm({'types': [1, 2]})