Здравствуйте! Я хочу обновить свою базу данных до версии 2, но в onUpgrade она не работает и не добавляет мне новый столбец.
Поэтому, когда я вызываю свой метод для записи, это выдает мне следующее сообщение об ошибке:
no such column: signature (code 1): , while compiling: UPDATE invoices SET signature=? WHERE id = ?
Вот часть моего кода:
class InvoiceDatabase(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
companion object {
private const val DATABASE_NAME: String = "invoice.db"
private const val DATABASE_VERSION = 2
private const val TABLE_NAME = "invoices" //Les nom de la base de donnee
private const val COLUMN_ONE = "id" //COlonne UN, c'est le ID qui va sautoincrementer on va pas trop le toucher
private const val COLUMN_TWO = "customercodez"
private const val COLUMN_TREE = "idproducts"
private const val COLUMN_FOR = "amountproducts"
private const val COLUMN_FIVE = "discount"
private const val COLUMN_SIX = "discounttype"
private const val COLUMN_SEVEN = "date"
private const val COLUMN_EIGHT = "signature"
private const val DATABASE_ALTER_SIGNATURE = "ALTER TABLE $TABLE_NAME ADD COLUMN $COLUMN_EIGHT TEXT;"
}
/**
* On creer la table SQLite
*/
override fun onCreate(db: SQLiteDatabase?) {
val query: String = "CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (" +
"${COLUMN_ONE} INTEGER PRIMARY KEY AUTOINCREMENT, " +
"${COLUMN_TWO} TEXT, " +
"${COLUMN_TREE} TEXT, " +
"${COLUMN_FOR} TEXT, " +
"${COLUMN_FIVE} TEXT, " +
"${COLUMN_SIX} TEXT, " +
"${COLUMN_SEVEN} TEXT" +
");"
db?.execSQL(query)
}
и мой onUpgrade:
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
when (oldVersion) {
1 -> {
db!!.execSQL(DATABASE_ALTER_SIGNATURE)
}
2 -> {
//upgrade logic from version 2 to 3
}
3 -> {
//upgrade logic from version 3 to 4
}
else -> throw IllegalStateException("onUpgrade() with unknown oldVersion $oldVersion");
}
}
Метод, который я назвал:
fun addSignature(invoice: Invoice) {
val values = ContentValues()
values.put(COLUMN_EIGHT, invoice.signatureBitmap)
val db: SQLiteDatabase = writableDatabase
db.update(TABLE_NAME, values, "${COLUMN_ONE} = ?", arrayOf(invoice.id.toString()))
db.close()
}
весь мой код:
class InvoiceDatabase(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
companion object {
private const val DATABASE_NAME: String = "invoice.db"
private const val DATABASE_VERSION = 2
private const val TABLE_NAME = "invoices" //Les nom de la base de donnee
private const val COLUMN_ONE = "id" //COlonne UN, c'est le ID qui va sautoincrementer on va pas trop le toucher
private const val COLUMN_TWO = "customercodez"
private const val COLUMN_TREE = "idproducts"
private const val COLUMN_FOR = "amountproducts"
private const val COLUMN_FIVE = "discount"
private const val COLUMN_SIX = "discounttype"
private const val COLUMN_SEVEN = "date"
private const val COLUMN_EIGHT = "signature"
private const val DATABASE_ALTER_SIGNATURE = "ALTER TABLE $TABLE_NAME ADD COLUMN $COLUMN_EIGHT TEXT;"
}
/**
* On creer la table SQLite
*/
override fun onCreate(db: SQLiteDatabase?) {
val query: String = "CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (" +
"${COLUMN_ONE} INTEGER PRIMARY KEY AUTOINCREMENT, " +
"${COLUMN_TWO} TEXT, " +
"${COLUMN_TREE} TEXT, " +
"${COLUMN_FOR} TEXT, " +
"${COLUMN_FIVE} TEXT, " +
"${COLUMN_SIX} TEXT, " +
"${COLUMN_SEVEN} TEXT, " +
"${COLUMN_EIGHT} TEXT" +
");"
db?.execSQL(query)
}
/**
* On insert lobjet creer dans la BDD
*/
fun insert(invoice: Invoice) {
val values = ContentValues()
values.put(COLUMN_TWO, invoice.customerCodeZ)
values.put(COLUMN_TREE, invoice.idProducts)
values.put(COLUMN_FOR, invoice.amountProducts)
values.put(COLUMN_FIVE, invoice.discount)
values.put(COLUMN_SIX, invoice.discountType)
values.put(COLUMN_SEVEN, invoice.date)
val db: SQLiteDatabase = writableDatabase
db.insert(TABLE_NAME, null, values)
db.close()
}
/**
* On Supprime l'objet de la bdd
*/
fun delete(invoice: Invoice) {
val db: SQLiteDatabase = writableDatabase
db.delete(TABLE_NAME, "${COLUMN_ONE} = ?", arrayOf(invoice.id.toString()))
db.close()
}
/**
* On modifie lobjet de la BDD
*/
fun update(invoice: Invoice) {
val values = ContentValues()
values.put(COLUMN_TREE, invoice.idProducts)
values.put(COLUMN_FOR, invoice.amountProducts)
values.put(COLUMN_FIVE, invoice.discount)
values.put(COLUMN_SIX, invoice.discountType)
val db: SQLiteDatabase = writableDatabase
db.update(TABLE_NAME, values, "${COLUMN_ONE} = ?", arrayOf(invoice.id.toString()))
db.close()
}
fun addSignature(invoice: Invoice) {
val values = ContentValues()
values.put(COLUMN_EIGHT, invoice.signatureBitmap)
val db: SQLiteDatabase = writableDatabase
db.update(TABLE_NAME, values, "${COLUMN_ONE} = ?", arrayOf(invoice.id.toString()))
db.close()
}
/**
* On recuperer toute les donnees de la bdd dans un arrayList
*/
val retrieveAllData: ArrayList<Invoice>
get() {
var count = 0
val db = this.readableDatabase
val invoices = ArrayList<Invoice>()
val result = db.rawQuery("SELECT * FROM ${TABLE_NAME} ORDER BY ${COLUMN_ONE} DESC", null)
while (result.moveToNext()) {
invoices.add(count, Invoice(
result.getLong(result.getColumnIndex(COLUMN_TWO)),
result.getString(result.getColumnIndex(COLUMN_TREE)),
result.getString(result.getColumnIndex(COLUMN_FOR)),
result.getDouble(result.getColumnIndex(COLUMN_FIVE)),
result.getString(result.getColumnIndex(COLUMN_SIX)),
result.getLong(result.getColumnIndex(COLUMN_SEVEN))
))
invoices[count].id = result.getInt(result.getColumnIndex(COLUMN_ONE))
count++
}
return invoices
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
when (oldVersion) {
1 -> {
db?.execSQL(DATABASE_ALTER_SIGNATURE)
}
2 -> {
//upgrade logic from version 2 to 3
}
3 -> {
//upgrade logic from version 3 to 4
}
else -> throw IllegalStateException("onUpgrade() with unknown oldVersion $oldVersion");
}
}
}