我在将值插入表格时遇到问题。 我做了2张相同原则的表,但第三张表引起了麻烦:/
剧集课
public Episodes (int eID,int seasonID,String episode,String date,String comment, float rating )
{
this.episodeID = eID;
this.seasonID = seasonID;
this.episode = episode;
this.date = date;
this.comment = comment;
this.rating = rating;
}
Episodes e1 = new Episodes(01, 01, "E1", "2012-01-12", "RandomComment01", 5);
dbManager.addEpisode(e1);
我的DBDesigner类
public class DBDesigner extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "SeriesIMDB.db";
public static final String TABLE_SERIES = "table_series";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_GENRE = "genre";
public static final String COLUMN_RATING = "rating";
public static final String COLUMN_DESCRIPTION = "description";
public static final String TABLE_SEASONS = "table_seasons";
public static final String COLUMN_SSID = "ssid";
public static final String COLUMN_SeriesID = "id";
public static final String COLUMN_SNAME = "sname";
public static final String TABLE_EPISODES = "table_episodes";
public static final String COLUMN_EID = "EID";
public static final String COLUMN_SEASONID = "seasonID";
public static final String COLUMN_EPISODE = "episode";
public static final String COLUMN_DATE = "date";
public static final String COLUMN_EPISODERATING = "rating";
public static final String COLUMN_COMMENT = "comment";
private static final String DATABASE_CREATE_TABLE_SERIES = "create table "
+ TABLE_SERIES + "( "
+ COLUMN_ID + " integer primary key not null, "
+ COLUMN_NAME + " text not null,"
+ COLUMN_GENRE + " text not null,"
+ COLUMN_RATING + " integer not null,"
+ COLUMN_DESCRIPTION + " text not null);";
private static final String DATABASE_CREATE_TABLE_SEASONS = "create table "
+ TABLE_SEASONS + "( "
+ COLUMN_SSID + " integer primary key not null, "
+ COLUMN_SeriesID + " integer not null,"
+ COLUMN_SNAME + " text not null,"
+" FOREIGN KEY("+COLUMN_SeriesID+") REFERENCES TABLE_SERIES("+COLUMN_ID+") ON DELETE CASCADE);";
private static final String DATABASE_CREATE_TABLE_EPISODES = "create table "
+ TABLE_EPISODES + "( "
+ COLUMN_EID + " integer primary key not null,"
+ COLUMN_SEASONID + " integer not null,"
+ COLUMN_EPISODE + " text,"
+ COLUMN_DATE + " text,"
+ COLUMN_COMMENT + " text,"
+ COLUMN_EPISODERATING + " integer,"
+" FOREIGN KEY("+COLUMN_SEASONID+") REFERENCES TABLE_SEASONS("+COLUMN_SSID+") ON DELETE CASCADE);";
private static final int DATABASE_VERSION = 1;
public DBDesigner(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE_TABLE_EPISODES);
database.execSQL(DATABASE_CREATE_TABLE_SERIES);
database.execSQL(DATABASE_CREATE_TABLE_SEASONS);
}
我的DBManager类我尝试过的值和rawQuery都没有用。
public void addEpisode(Episodes newEpisode) {
ContentValues values = new ContentValues();
Log.d("addBook", newEpisode.toString());
values.put(DBDesigner.COLUMN_EID, newEpisode.episodeID );
values.put(DBDesigner.COLUMN_SEASONID, newEpisode.seasonID);
values.put(DBDesigner.COLUMN_EPISODE, newEpisode.episode);
values.put(DBDesigner.COLUMN_DATE, newEpisode.date);
values.put(DBDesigner.COLUMN_COMMENT, newEpisode.comment);
values.put(DBDesigner.COLUMN_EPISODERATING, newEpisode.rating);
database.insert(DBDesigner.TABLE_EPISODES, null, values);
/*database.rawQuery("INSERT INTO "+DBDesigner.TABLE_EPISODES+"("
+DBDesigner.COLUMN_EID+","
+DBDesigner.COLUMN_SEASONID+","
+DBDesigner.COLUMN_EPISODE+","
+DBDesigner.COLUMN_DATE+","
+DBDesigner.COLUMN_COMMENT+","
+DBDesigner.COLUMN_EPISODERATING+")"
+ " VALUES("
+newEpisode.episodeID+","
+newEpisode.seasonID+","
+newEpisode.episode+","
+newEpisode.date+","
+newEpisode.comment+","
+newEpisode.rating+")", null);*/}
我希望这是所有数据。有人可以看到我做错了什么吗? 谢谢聪明人:))
答案 0 :(得分:0)
在您的剧集constructor
中,评级的数据类型定义为float。
public Episodes (int eID,int seasonID,String episode,String date,String comment, float rating )
但是当您为剧集创建数据库时,您已将评级定义为整数类型。
private static final String DATABASE_CREATE_TABLE_EPISODES = "create table "
+ TABLE_EPISODES + "( "
+ COLUMN_EID + " integer primary key not null,"
+ COLUMN_SEASONID + " integer not null,"
+ COLUMN_EPISODE + " text,"
+ COLUMN_DATE + " text,"
+ COLUMN_COMMENT + " text,"
+ COLUMN_EPISODERATING + " integer,"
+" FOREIGN KEY("+COLUMN_SEASONID+") REFERENCES TABLE_SEASONS("+COLUMN_SSID+") ON DELETE CASCADE);";
可能是造成问题。