关系型数据库地址: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)}
}
运行结果:
注册页面:
登录页面