Commit d27dee6e by 胡悦

fix(database): Update isColumnExists method to use selectSql for column checks

- Changed the method used in isColumnExists from executeSql to selectSql for improved functionality.
- Added logging for table and column existence checks in DBManager for better debugging.
1 parent f7f88a68
...@@ -168,7 +168,7 @@ class SQLite { ...@@ -168,7 +168,7 @@ class SQLite {
*/ */
async isColumnExists(tableName, columnName) { async isColumnExists(tableName, columnName) {
try { try {
const columnInfo = await this.executeSql(`PRAGMA table_info(${tableName})`) const columnInfo = await this.selectSql(`PRAGMA table_info(${tableName})`)
return columnInfo.some(col => col.name === columnName) return columnInfo.some(col => col.name === columnName)
} catch (error) { } catch (error) {
console.error(`检查字段是否存在失败 error - > ${error.message}`) console.error(`检查字段是否存在失败 error - > ${error.message}`)
......
...@@ -34,10 +34,14 @@ class DBManager { ...@@ -34,10 +34,14 @@ class DBManager {
await this.db.init() await this.db.init()
} }
const isTableExists = await this.db.isTableExists(this.table_name) const isTableExists = await this.db.isTableExists(this.table_name)
console.log(isTableExists, 'isTableExists')
uni.$logger.info(`数据库是否存在isTableExists: ${isTableExists}`)
if (!isTableExists) { if (!isTableExists) {
await this.db.createTable(this.table_name, this.table_columns) await this.db.createTable(this.table_name, this.table_columns)
} else { //如果存在 判断需要新增的字段是否存在 不存在则添加 } else { //如果存在 判断需要新增的字段是否存在 不存在则添加
const isColumnExists = await this.db.isColumnExists(this.table_name, 'carBrand') const isColumnExists = await this.db.isColumnExists(this.table_name, 'carBrand')
console.log(isColumnExists, 'isColumnExists')
uni.$logger.info(`字段是否存在isColumnExists: ${isColumnExists}`)
if (!isColumnExists) { if (!isColumnExists) {
await this.db.addColumns(this.table_name, [ await this.db.addColumns(this.table_name, [
{ name: 'carBrand', definition: 'TEXT DEFAULT ""' }, { name: 'carBrand', definition: 'TEXT DEFAULT ""' },
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!