關系型數據庫地址:C:\Users\ASUS\AppData\Local\Temp\HuaweiDevEcoStudioDatabases\rdb
#注冊頁面register.ets
import dataRdb from '@ohos.data.rdb'const STORE_CONFIG = {name: 'weather4.db'
}
const TABLE_NAME = 'weather_info'
const SQL_CREATE_TABLE = `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (id INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT NOT NULL,password TEXT NOT NULL)
`
async function createTable(context) {try {const store = await dataRdb.getRdbStore(context, STORE_CONFIG, 1)await store.executeSql(SQL_CREATE_TABLE)console.info(`? 表 ${TABLE_NAME} 創建成功`)} catch (err) {console.error(`? 創建表失敗: ${JSON.stringify(err)}`)console.error('錯誤詳情:', err)}
}@Entry
@Component
struct WeatherInsertPage {@State username: string = ''@State password: string = ''async insertuser() {const context = getContext(this)createTable(context)const rdbStore = await dataRdb.getRdbStore(context, { name: 'weather4.db' }, 1)const valueBucket: dataRdb.ValuesBucket = {username: this.username,password: this.password,}try {let rowId = await rdbStore.insert('weather_info', valueBucket)console.info(`? 插入成功,行ID: ${rowId}`)} catch (err) {console.error(`? 插入失敗: ${JSON.stringify(err)}`)}}build() {Column() {Text('用戶注冊').fontSize(26).fontWeight(FontWeight.Bold).margin({ bottom: 20 })TextInput({ placeholder: '請輸入用戶名', text: this.username }).onChange(value => this.username = value).margin(10)TextInput({ placeholder: '請輸入密碼', text: this.password }).onChange(value => this.password = value).type(InputType.Password).margin(10)Button('注冊').margin(10).onClick(() => this.insertuser())}.padding(20)}
}
#登錄頁面項目login.ets
import dataRdb from '@ohos.data.rdb'const STORE_CONFIG = {name: 'weather4.db'
}
const TABLE_NAME = 'weather_info'// 建表語句
const SQL_CREATE_TABLE = `CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (id INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT NOT NULL,password TEXT NOT NULL)
`// 創建數據表
async function createTable(context) {const store = await dataRdb.getRdbStore(context, STORE_CONFIG, 1)await store.executeSql(SQL_CREATE_TABLE)
}// 查詢用戶是否存在
async function checkLogin(context, username: string, password: string): Promise<boolean> {const store = await dataRdb.getRdbStore(context, STORE_CONFIG, 1)const predicates = new dataRdb.RdbPredicates(TABLE_NAME).equalTo('username', username).equalTo('password', password)const resultSet = await store.query(predicates, ['id'])const hasRow = resultSet.goToFirstRow()resultSet.close()return hasRow
}// UI 頁面
@Entry
@Component
struct LoginPage {@State username: string = ''@State password: string = ''@State message: string = ''async aboutToAppear() {const context = getContext(this)await createTable(context) // 頁面加載時確保表存在}async login() {const context = getContext(this)const success = await checkLogin(context, this.username, this.password)if (success) {this.message = '? 登錄成功'} else {this.message = '? 用戶名或密碼錯誤'}}build() {Column() {Text('用戶登錄').fontSize(26).fontWeight(FontWeight.Bold).margin({ bottom: 20 })TextInput({ placeholder: '請輸入用戶名', text: this.username }).onChange(value => this.username = value).margin(10)TextInput({ placeholder: '請輸入密碼', text: this.password }).onChange(value => this.password = value).type(InputType.Password).margin(10)Button('登錄').margin(10).onClick(() => this.login())Text(this.message).fontSize(16).margin(10).fontColor(Color.Red)}.padding(20)}
}
運行結果:
注冊頁面:
登錄頁面