您好我想将我的数据库更新到版本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");
}
}
}
答案 0 :(得分:0)
首先,当db对象可以为null时,我会使用像:
这样的思考db?.execSQL(DATABASE_ALTER_SIGNATURE)因为这样,如果db为null,则不会执行任何代码,因此不会崩溃。但是用db !!。execSQL(DATABASE_ALTER_SIGNATURE)热潮!!!
现在我看到DATABASE_VERSION = 2,但你在版本1上执行你的alter table
答案 1 :(得分:0)
我提供了java代码,用于在升级数据库后更新表中的某些字段。 改变这段代码..
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
when (newVersion) {
1 -> {
db!!.execSQL(DATABASE_ALTER_SIGNATURE)
}
2 -> {
getWriteDatabase();
mNewDatabase.execSQL("ALTER TABLE TabelName ADD COLUMN COLUMN_EIGHT TEXT");
}
3 -> {
//upgrade logic from version 3 to 4
}
else -> throw IllegalStateException("onUpgrade() with unknown oldVersion $oldVersion");
}
}
此处将一个列值设置为零,以便定义所有ID。
mNewDatabase.execSQL("UPDATE TableName SET columnName = 0 WHERE TableName IN ('10','21','58','63','66','69','78','89','91','92','95','96','97')");
如果你想在表中添加任何列,那么使用alter命令,如下所示..
mNewDatabase.execSQL("ALTER TABLE TabelName ADD COLUMN isActive INTEGER DEFAULT 1");