当我尝试将数据插入数据库时,出现以下错误:
E/DATABASE OPERATION﹕ Database created/opened...
E/SQLiteLog﹕ (1) table fixture_info has no column named match_opponent
E/SQLiteDatabase﹕ Error inserting match_opponent=you match_venue=here match_time=16:00 match_date=27/12/2015
android.database.sqlite.SQLiteException: table fixture_info has no column named match_opponent (code 1): , while compiling: INSERT INTO fixture_info(match_opponent,match_venue,match_time,match_date) VALUES (?,?,?,?)
这是我的代码:
public class Fixture {
public static abstract class NewFixture {
public static final String MATCH_DATE = "match_date";
public static final String MATCH_TIME = "match_time";
public static final String MATCH_OPPONENT = "match_opponent";
public static final String MATCH_VENUE = "match_venue";
public static final String TABLE_NAME = "fixture_info";
}
}
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "CLUBHUB.DB";
private static final int DATABASE_VERSION = 1;
private static final String CREATE_QUERY = "CREATE TABLE " + Fixture.NewFixture.TABLE_NAME + "(id INTEGER, " +Fixture.NewFixture.MATCH_DATE + " TEXT, " + Fixture.NewFixture.MATCH_TIME + "TEXT, " + Fixture.NewFixture.MATCH_OPPONENT + " TEXT, "
+ Fixture.NewFixture.MATCH_VENUE + " TEXT );";
public DBHelper(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
Log.e("DATABASE OPERATION", "Database created/opened...");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_QUERY);
Log.e("DATABASE OPERATION", "Table create..." + CREATE_QUERY);
}
public static void addMatch(String date, String time, String opponent, String venue, SQLiteDatabase db) {
ContentValues cV = new ContentValues();
cV.put(Fixture.NewFixture.MATCH_DATE, date);
cV.put(Fixture.NewFixture.MATCH_TIME, time);
cV.put(Fixture.NewFixture.MATCH_OPPONENT, opponent);
cV.put(Fixture.NewFixture.MATCH_VENUE, venue);
db.insert(Fixture.NewFixture.TABLE_NAME, null, cV);
Log.e("DATABASE OPERATION", "one row inserted");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public Cursor getInfo(SQLiteDatabase db) {
Cursor cursor;
String[] projections = {Fixture.NewFixture.MATCH_DATE, Fixture.NewFixture.MATCH_TIME, Fixture.NewFixture.MATCH_OPPONENT, Fixture.NewFixture.MATCH_VENUE};
cursor = db.query(Fixture.NewFixture.TABLE_NAME, projections, null, null, null, null, null);
return cursor;
}
}
公共类UpdateSchedule扩展了AppCompatActivity {
EditText addDate;
EditText addTime;
EditText addOpponent;
EditText addVenue;
Button addFixture;
Context context = this;
SQLiteDatabase sqLiteDatabase;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_updateschedule);
addDate = (EditText) findViewById(R.id.fixtureDateEdit);
addTime = (EditText) findViewById(R.id.fixtureTimeEdit);
addOpponent = (EditText) findViewById(R.id.fixtureOpponentEdit);
addVenue = (EditText) findViewById(R.id.fixtureVenueEdit);
addFixture = (Button) findViewById(R.id.addFixtureButton);
}
public void addFixture(View view) {
String matchDate = addDate.getText().toString();
String matchTime = addTime.getText().toString();
String matchOpponent = addOpponent.getText().toString();
String matchVenue = addVenue.getText().toString();
DBHelper dbHelper = new DBHelper(context);
sqLiteDatabase = dbHelper.getWritableDatabase();
DBHelper.addMatch(matchDate, matchTime, matchOpponent, matchVenue, sqLiteDatabase);
Toast.makeText(getBaseContext(), "Data saved", Toast.LENGTH_LONG).show();
dbHelper.close();
非常感谢任何帮助