我想在SQLite中创建2个(以及更多)表,但是存在问题。我之前创建了一个表并且它正在工作但是现在当我尝试制作第二个表并插入一些数据时,它说“没有这样的表”
为什么这不起作用?
@Override
public void onCreate(SQLiteDatabase db) {
// create wedstrijden table
String CREATE_WEDSTRIJD_TABLE = "CREATE TABLE wedstrijd ( " +
"id INTEGER PRIMARY KEY, " +
"datum TEXT, " +
"thuisclub NUMERIC, " +
"bezoekers NUMERIC, " +
"thuistenue TEXT, " +
"bezoekerstenue TEXT, " +
"aftrap TEXT, " +
"begintijd1ehelft TEXT, " +
"eindtijd1ehelft TEXT, " +
"extratijd1ehelft TEXT, " +
"begintijd2ehelft TEXT, " +
"eindtijd2ehelft TEXT, " +
"extratijd2ehelft TEXT, " +
"verlengen NUMERIC, " +
"penalties NUMERIC, " +
"begintijdverlengen TEXT, " +
"eindtijdverlengen TEXT, " +
"totalespeeltijd TEXT, " +
"begintijdpenalties TEXT, " +
"eindtijdpenalties TEXT, " +
"doelpuntenthuis NUMERIC, " +
"doelpuntenuit NUMERIC)";
db.execSQL(CREATE_WEDSTRIJD_TABLE);
// create teams table
String CREATE_TEAM_TABLE = "CREATE TABLE teams ( " +
"id INTEGER PRIMARY KEY, " +
"naam TEXT, " +
"trainer TEXT, " +
"tenue TEXT)";
db.execSQL(CREATE_TEAM_TABLE);
}
要添加数据,请执行以下操作:
db.addTeam(new DatabaseTeams(bezoekersTeam,bezoekersTrainer,bezoekersTenue));
public void addTeam(DatabaseTeams team){
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put(KEY_NAAM_TEAM, team.getTeamNaamThuis());
values.put(KEY_TRAINER_TEAM, team.getTeamTrainerThuis());
values.put(KEY_TENUE_TEAM, team.getTeamTenueThuis());
// 3. insert
long lastId = db.insert(TABLE_TEAM, // table
null, //nullColumnHack
values); // key/value -> keys = column names/ values = column values
Log.d("Insert new team "+lastId+" invoeren", team.toString());
// 4. close
db.close();
}
值为:
// Team Info
// Team table name
private static final String TABLE_TEAM = "teams";
// Team table Columns names
private static final String KEY_ID_TEAM = "id";
private static final String KEY_NAAM_TEAM = "naam";
private static final String KEY_TRAINER_TEAM = "trainer";
private static final String KEY_TENUE_TEAM = "tenue";
private static final String[] COLUMNS_TEAM = {KEY_ID_TEAM,KEY_NAAM_TEAM,KEY_TRAINER_TEAM,KEY_TENUE_TEAM};
答案 0 :(得分:3)
您的问题可能是onCreate()
未被调用。
每次更改数据库结构时,都需要增加数据库版本,即SQLiteOpenHelper中的int DATABASE_VERSION
。您还必须正确实现onUpgrade()
,因为当Android检测到数据库版本已增加时会调用此方法。这样做:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// drop old tables
onCreate(db);
}
有关更详尽的说明和代码示例,请参阅android tutorial on creating an sqlite helper。