Sqlite在放入数据时丢失了一列

时间:2014-10-13 16:18:50

标签: android sqlite

我尝试在SQLite android中复制我的数据,但是我收到一条消息,表示缺少一列。以下是代码:

    private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "Arretschoisi";

// Contacts table name
private static final String TABLE_CONTACTS = "arret";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String ID_Trajet="id_trajet";
private static final String ID_TRAJET_PUBLIC="code_publique_ligne";
private static final String DESTINATION="destination_trajet";
private static final String TIME_TO_WAIT="temps_attente";
private static final String TIME="temps_passage_prevu";


public DatabaseStationChoice(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + " ("
            + KEY_ID + " INTEGER PRIMARY KEY," +
            ID_Trajet + " TEXT, "+ 
            ID_TRAJET_PUBLIC + " TEXT, " + 
            DESTINATION + " TEXT, " +
            TIME_TO_WAIT + " TEXT, " +
            TIME + " TEXT" +");";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
    // Create tables again
    onCreate(db);
}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */
//delete all rows in table
public void deleteAll()
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_CONTACTS,null,null);
    //   db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
    //   db.execSQL("TRUNCATE TABLE " + TABLE_CONTACTS);
    db.close();
}

// Adding new contact
void addHorraire(Horraire horaire) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(ID_Trajet, horaire.getID_SQL()); // horaire Id_sql
    values.put(ID_TRAJET_PUBLIC, horaire.getadr_id_pub()); // horaire num public
    values.put(DESTINATION, horaire.getadr_des()); // horaire destination
    values.put(TIME_TO_WAIT, horaire.getadr_2wait()); // horaire d attente
    values.put(TIME, horaire.getadr_time()); // horaire d'arriver

    // Inserting Row
    db.insert(TABLE_CONTACTS, null, values);
    db.close(); // Closing database connection
}

和logcat的错误:

(1) table arret has no column named destination_trajet
Error inserting code_publique_ligne=L-8 destination_trajet=Fin temps_passage_prevu=289 temps_attente=16:20 id_trajet=8
android.database.sqlite.SQLiteException: table arret has no column named destination_trajet (code 1): , while compiling: INSERT INTO arret(code_publique_ligne,destination_trajet,temps_passage_prevu,temps_attente,id_trajet) VALUES (?,?,?,?,?)
android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:886)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:497)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
    at com.example.citybus.DatabaseStationChoice.addHorraire(DatabaseStationChoice.java:84)
    at com.example.citybus.Proximite$sendstationfortime.onPostExecute(Proximite.java:174)
    at com.example.citybus.Proximite$sendstationfortime.onPostExecute(Proximite.java:1)
    at android.os.AsyncTask.finish(AsyncTask.java:631)
    at android.os.AsyncTask.access$600(AsyncTask.java:177)

所以我的数据库的架构如下:

[{\"id_trajet\":8,\"code_publique_ligne\":\"L-8\",\"texte_destination_trajet\":\"Fin\",\"temps_attente\":289,\"temps_passage_prevu\":\"16:20\"}] 感谢

1 个答案:

答案 0 :(得分:1)

如果您已应用新的更改:

  • 将DATABASE_VERSION加1以强制执行onUpgrade()方法
  • 或从设备中删除旧数据库文件